c#实现输出本月的月历

格式要求:


代码如下:

SU MO TU WE TH FR SA
         01 02 03 04
05 06 07 08 09 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30

代码:


代码如下:

class Interview1
    {
        static void Main()
        {
            PrintCalender(2011, 10);
        }
        public static void PrintCalender(int year, int month)
        {
            formatDate fd = new formatDate(year, month);
            string calender =
           @"SU MO TU WE TH FR Sa
            {0} {0} {0} {0} {0} {0} {0} {0}
            {0} {0} {0} {0} {0} {0} {0} {0}
            {0} {0} {0} {0} {0} {0} {0} {0}
            {0} {0} {0} {0} {0} {0} {0} {0}
            {0} {0} {0} {0} {0} {0} {0} {0}";

calender = string.Format(calender, fd).TrimEnd();
            Console.WriteLine(calender);
        }
    }
    class formatDate : IFormattable
    {
        int num;
        int max;
        public formatDate(int year, int month)
        {
            DateTime dt = new DateTime(year, month, 1);
            num = (int)dt.DayOfWeek * -1;
            max = DateTime.DaysInMonth(year, month);
        }
        public string ToString(string format,IFormatProvider formatProvider)
        {
            return num++ < 0 || num > max ? "  " : num.ToString("00");
        }
    }

(0)

相关推荐

  • C#实现带阴历显示的日期代码

    本文实例讲述了C#实现带阴历显示的日期代码,分享给大家供大家参考.具体方法如下: 这是一个用于酒店预定功能的带日期控件,类似去哪网酒店预定,由于需要设置节假日不同时期内的价格,因此需要自己写个时间控件.在此分享下写时间控件过程中用到的农历显示类. 复制代码 代码如下: public class CnCalendar { static ChineseLunisolarCalendar cCalendar = new ChineseLunisolarCalendar(); public static

  • C#实现的阴历阳历互相转化类实例

    本文实例讲述了C#实现的阴历阳历互相转化类.分享给大家供大家参考,具体如下: 最近郁闷地发现网上现有的相当一部分万年历上干支纪年的算法都是错误的.因为干支纪年是针对阴历而言的,而生肖属相又跟地支对应,所以元旦和春节之间那段时间在干支纪年法中应该归上一年,以阳历2007年2月9日为例,当日的阴历日期是二〇〇六年十二月廿二日,是丙戌年,即狗年,但是浏览一下目前的万年历,相当一部分都显示成了丁亥年,猪年,比较郁闷-- 然后就写了一个阴历阳历互相转化的类. 相关代码如下: /// <summary>

  • C#简单输出日历的方法

    本文实例讲述了C#简单输出日历的方法.分享给大家供大家参考.具体如下: 用C#输出日历,此功能可用于Ajax方式列出计划日程相关的内容,由于是C#控制输出,可以方便加上自己需要的业务处理逻辑. 1.控制台输出: using System; namespace 控制台日历 { class Program { public static void Main(string[] args) { string s = " "; Console.WriteLine("输入年份:"

  • C#由当前日期计算相应的周一和周日的实例代码

    复制代码 代码如下: /// <summary>  /// 计算本周起始日期(礼拜一的日期)  /// </summary>  /// <param name="someDate">该周中任意一天</param>  /// <returns>返回礼拜一日期,后面的具体时.分.秒和传入值相等</returns>  public static DateTime CalculateFirstDateOfWeek(Date

  • C# 日历类功能的实例代码

    C# 日历类的实现代码,具体如下所示: using System; namespace DotNet.Utilities { /// <summary> /// 农历属性 /// </summary> public class CNDate { /// <summary> /// 农历年(整型) /// </summary> public int cnIntYear = 0; /// <summary> /// 农历月份(整型) /// <

  • C#实现农历日历的方法

    本文实例讲述了C#实现农历日历的方法.分享给大家供大家参考. 具体实现方法如下: 复制代码 代码如下: //天干  private  static string []TianGan =   {"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};     //地支  private

  • C# 常用日期时间函数(老用不熟)

    --DateTime 数字型 System.DateTime currentTime=new System.DateTime(); 1.1 取当前年月日时分秒 currentTime=System.DateTime.Now; 1.2 取当前年 int 年=currentTime.Year; 1.3 取当前月 int 月=currentTime.Month; 1.4 取当前日 int 日=currentTime.Day; 1.5 取当前时 int 时=currentTime.Hour; 1.6 取

  • c#的时间日期操作示例分享(c#获取当前日期)

    1.给定时间戳返回指定的时间格式 复制代码 代码如下: private string StampToDate(string timeStamp,string format){ DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); long lTime = long.Parse(timeStamp + "0000000"); TimeSpan toNow = new TimeS

  • C#日期控件datetimepicker保存空值的三种方法

    方法一(推荐): 设置datetimepicker的属性ShowCheckBox为true 在窗口初始化时候,添加代码this.datetimepicker1.Checked = false; 保存日期值入库的时候,就可以根据if(this.datetimepicker1.Checked ==false),保存空值. 方法二: 在窗口初始化函数中添加: 复制代码 代码如下: this.dateTimePicker1.Format=DateTimePickerFormat.Custom; this

  • C#获取上个月第一天和最后一天日期的方法

    本文实例讲述了C#获取上个月第一天和最后一天日期的方法.分享给大家供大家参考. 具体实现代码如下: 复制代码 代码如下: int year = DateTime.Now.Year;//当前年  int mouth = DateTime.Now.Month;//当前月    int beforeYear = 0;  int beforeMouth = 0;  if (mouth <= 1)//如果当前月是一月,那么年份就要减1  {      beforeYear = year - 1;     

  • C#日历样式的下拉式计算器实例讲解

    本文介绍了如何在Visual Studio中创建用户控件来显示下拉式计算器,弹出效果类似于日历控件. 介绍 如果我们正在做一个类似于库存控制和计费系统的项目,有些部分可能必须手动计算数值.因此,用户就不得不使用计算器得到结果,再填入到输入字段中,或者在工作窗口上单独打开一个计算器窗口.总之,各种不便和麻烦. 这篇文章主要描述的是如何添加下拉式计算器到DataGridView单元格中,如下图: 使用代码 第一步,我们必须先创建一个函数计算器,并且能够使用控件.因此,不妨先创建一个Visual St

随机推荐