当前位置: 移动技术网 > IT编程>开发语言>.net > C# DateTime的11种构造函数

C# DateTime的11种构造函数

2018年10月02日  | 移动技术网IT编程  | 我要评论

张木生,消地雷,萝莉情人

别的也不多说没直接贴代码

using system;
using system.collections.generic;
using system.globalization;
using system.linq;
using system.text;
using system.text.regularexpressions;
using system.threading;
using system.threading.tasks;

namespace time20180929_datetime
{
    class program
    {
        static void main(string[] args)
        {
            /*
             * 1.datetime(int64):将 datetime 结构的新实例初始化为指定的刻度数。
             * 2.datetime(int64, datetimekind):将 datetime 结构的新实例初始化为指定的计时周期数以及协调世界时 (utc) 或本地时间。
             * 3.datetime(int32, int32, int32):将 datetime 结构的新实例初始化为指定的年、月和日。
             * 4.datetime(int32, int32, int32, calendar):将 datetime 结构的新实例初始化为指定日历的指定年、月和日。
             * 5.datetime(int32, int32, int32, int32, int32, int32):将 datetime 结构的新实例初始化为指定的年、月、日、小时、分钟和秒。
             * 6.datetime(int32, int32, int32, int32, int32, int32, datetimekind)    :将 datetime 结构的新实例初始化为指定年、月、日、小时、分钟、秒和协调世界时 (utc) 或本地时间。
             * 7.datetime(int32, int32, int32, int32, int32, int32, calendar):将 datetime 结构的新实例初始化为指定日历的年、月、日、小时、分钟和秒。
             * 8.datetime(int32, int32, int32, int32, int32, int32, int32):将 datetime 结构的新实例初始化为指定的年、月、日、小时、分钟、秒和毫秒。
             * 9.datetime(int32, int32, int32, int32, int32, int32, int32, datetimekind):将 datetime 结构的新实例初始化为指定年、月、日、小时、分钟、秒、毫秒和协调世界时 (utc) 或本地时间。
             * 10.datetime(int32, int32, int32, int32, int32, int32, int32, calendar):将 datetime 结构的新实例初始化为指定日历的指定年、月、日、小时、分钟、秒和毫秒。
             * 11.datetime(int32, int32, int32, int32, int32, int32, int32, calendar, datetimekind):将 datetime 结构的新实例初始化为指定日历的指定年、月、日、小时、分钟、秒、毫秒和协调世界时 (utc) 或本地时间。
             */



            /*
             * 1.public datetime (long ticks):
             * 一个日期和时间,以公历 0001 年 1 月 1 日 00:00:00.000 以来所经历的以 100 纳秒为间隔的间隔数来表示。
             * ticks 小于 minvalue 或大于 maxvalue。
             */
            //时间属性minvalue和maxvalue
            long _minval = datetime.minvalue.ticks;
            long _maxval = datetime.maxvalue.ticks;
            datetime _mintime = datetime.minvalue;
            datetime _maxtime = datetime.maxvalue;
            string desc = "minval:{0}, _maxval:{1}, _mintime:{2}, _maxtime:{3}";

            string format = "{0}) the {1} date and time is {2:mm/dd/yy hh:mm:ss tt}";
            datetime dt1 = new datetime(datetime.minvalue.ticks);
            datetime dt2 = new datetime(datetime.maxvalue.ticks);

            //创建一个传统时间为上午2018/9/29 10:39:50基于名称指定的区域性"en-us"并基于布尔值(指定是否使用系统中用户选定的区域性设文化ticks
            long ticks = new datetime(2018, 9, 29, 10, 39, 50, new cultureinfo("en-us", false).calendar).ticks;
            datetime dt3 = new datetime(ticks);

            console.writeline(desc, _minval, _maxval, _mintime, _maxtime);
            console.writeline(format, 1, "minimum", dt1);
            console.writeline(format, 2, "maxmum", dt2);
            console.writeline(format, 3, "custom ", dt3);
            console.writeline("\nthe custom date and time is created from {0:n0} ticks.", ticks);
            console.writeline("\nthe custom date and time is created from {0} ticks.", ticks);

            /*
            minval:0, _maxval:3155378975999999999, _mintime:0001/1/1 0:00:00, _maxtime:9999/12/31 23:59:59
            1) the minimum date and time is 01/01/01 12:00:00 上午
            2) the maxmum date and time is 12/31/99 11:59:59 下午
            3) the custom  date and time is 09/29/18 10:39:50 上午

            the custom date and time is created from 636,738,143,900,000,000 ticks.

            the custom date and time is created from 636738143900000000 ticks.
             */

            /*
             * 2.public datetime (long ticks, datetimekind kind);
             * datetime(int64, datetimekind):将 datetime 结构的新实例初始化为指定的计时周期数以及协调世界时 (utc) 或本地时间。
             * kind:
             * unspecified  0   表示的时间既未指定为本地时间,也未指定为协调通用时间 (utc)。
             * utc          1   表示的时间为 utc。
             * local        2   表示的时间为本地时间
             */
            long ticks2 = new datetime(2018, 9, 29, 10, 39, 50, new cultureinfo("en-us", false).calendar).ticks;
            console.writeline("2018-09-29 10:39:50");
            console.writeline("unspecified:{0}", new datetime(ticks2, datetimekind.unspecified));
            console.writeline("utc:{0}", new datetime(ticks2, datetimekind.utc));
            console.writeline("local:{0}", new datetime(ticks2, datetimekind.local));
            /*
             2018-09-29 10:39:50
            unspecified:2018/9/29 10:39:50
            utc:2018/9/29 10:39:50
            local:2018/9/29 10:39:50
             */
            /*
             * 3.public datetime (int year, int month, int day);
             * datetime(int32, int32, int32)
             * 将 datetime 结构的新实例初始化为指定的年、月和日。
             * year:1-9999,month:0-12,day:1-moth中的天数
             */
            datetime dt4 = new datetime(2018, 9, 18);
            console.writeline("dt4的值为{0}", dt4.tostring());
            /*
             dt4的值为2018/9/18 0:00:00
             */


            /*
             * 4.public datetime (int year, int month, int day, system.globalization.calendar calendar);
             * datetime(int32, int32, int32, calendar):将 datetime 结构的新实例初始化为指定日历的指定年、月和日。
             * year:1-9999,month:0-12,day:1-moth中的天数,calendar用于解释 year、month 和 day 的日历。
             */
            /*
             下面的示例调用datetime(int32, int32, int32, calendar)构造函数两次实例化两个datetime值。 第一次调用实例化datetime通过使用值persiancalendar对象。 由于波斯日历不能指定为区域性的默认日历,显示日期与波斯历需要单独调用其persiancalendar.getmonth, persiancalendar.getdayofmonth,和persiancalendar.getyear方法。 构造函数的第二个调用实例化datetime通过使用值hijricalendar对象。 该示例将当前区域性更改为阿拉伯语 (叙利亚) 和当前区域性的默认日历更改为回历。 因为回历是当前区域性的默认日历,console.writeline方法使用它来设置日期格式。 还原先前的当前区域性 (这在此情况下是英语 (美国)) 时,console.writeline方法使用当前区域性的默认公历日历来设置日期格式。
             */
           console.writeline("using the persian calendar:");
           persiancalendar persian = new persiancalendar();
           datetime dt5 = new datetime(2018,9,29, persian);
           console.writeline(dt5.tostring());
           console.writeline("{0}/{1}/{2}\n", persian.getmonth(dt5),persian.getdayofmonth(dt5),persian.getyear(dt5));

           console.writeline("using the hijri calendar:");
           cultureinfo dftculture = thread.currentthread.currentculture;

           // define hijri calendar.
           hijricalendar hijri = new hijricalendar();
           // make ar-sy the current culture and hijri the current calendar.
           thread.currentthread.currentculture = new cultureinfo("ar-sy");
           cultureinfo current = cultureinfo.currentculture;
           current.datetimeformat.calendar = hijri;
           string dformat = current.datetimeformat.shortdatepattern;
           // ensure year is displayed as four digits.
           dformat = regex.replace(dformat, "/yy$", "/yyyy");
           current.datetimeformat.shortdatepattern = dformat;
           datetime date2 = new datetime(2018,9,29, hijri);
           console.writeline("{0} culture using the {1} calendar: {2:d}", current,getcalendarname(hijri), date2);

           // restore previous culture.
           thread.currentthread.currentculture = dftculture;
           console.writeline("{0} culture using the {1} calendar: {2:d}",cultureinfo.currentculture,getcalendarname(cultureinfo.currentculture.calendar),date2);
           /*
            using the persian calendar:
           2639/12/20 0:00:00
           9/29/2018

           using the hijri calendar:
           ar-sy culture using the hijri calendar: 29/09/2018
           zh-cn culture using the gregorian calendar: 2580/3/16
            */

            /*
             * 5.public datetime (int year, int month, int day, int hour, int minute, int second);
             * datetime(int32, int32, int32, int32, int32, int32)
             * 将 datetime 结构的新实例初始化为指定的年、月、日、小时、分钟和秒。
             * year:1-9999,month:0-12,day:1-moth中的天数,hour:0-23,minuye:0-59,second:0-59
             */
            datetime date1 = new datetime(2018, 9, 29, 11, 20, 26);
            console.writeline(date1.tostring());
            /*2018/9/29 11:20:26*/

            /*
             * 6.public datetime (int year, int month, int day, int hour, int minute, int second, datetimekind kind);
             * datetime(int32, int32, int32, int32, int32, int32, datetimekind)
             * 将 datetime 结构的新实例初始化为指定年、月、日、小时、分钟、秒和协调世界时 (utc) 或本地时间。
             * year:1-9999,month:0-12,day:1-moth中的天数,hour:0-23,minuye:0-59,second:0-59
             *kind:
             *unspecified  0   表示的时间既未指定为本地时间,也未指定为协调通用时间 (utc)。
             *utc          1   表示的时间为 utc。
             *local        2   表示的时间为本地时间
             */
            datetime date9 = new datetime(2010, 8, 18, 16, 32, 0, datetimekind.local);
            console.writeline("日期:{0}\ndatetimekind:{1}", date9, date9.kind);
            /*
             日期:2010/8/18 16:32:00
              datetimekind:local
             */

            /*
             *7.public datetime (int year, int month, int day, int hour, int minute, int second, system.globalization.calendar calendar);
             * datetime(int32, int32, int32, int32, int32, int32, calendar)
             * 将 datetime 结构的新实例初始化为指定日历的年、月、日、小时、分钟和秒。
             * year:1-9999,month:0-12,day:1-moth中的天数,hour:0-23,minuye:0-59,second:0-59,
             * calendar:用于解释 year、month 和 day 的日历。
             */

            /*
             *8.public datetime (int year, int month, int day, int hour, int minute, int second, int millisecond);
             * datetime(int32, int32, int32, int32, int32, int32, int32)
             * 将 datetime 结构的新实例初始化为指定的年、月、日、小时、分钟、秒和毫秒。
             * year:1-9999,month:0-12,day:1-moth中的天数,hour:0-23,minuye:0-59,second:0-59,millisecond:0-999
             */

            datetime date3 = new datetime(2018, 9, 29, 13, 44, 30, 555);
            console.writeline(date3.tostring("mm/dd/yyyy hh:mm:ss.fff tt"));
            /*09/29/2018 13:44:30.555 下午*/

            /*
             * 9.public datetime (int year, int month, int day, int hour, int minute, int second, int millisecond, datetimekind kind);
             * datetime(int32, int32, int32, int32, int32, int32, int32, datetimekind)
             * 将 datetime 结构的新实例初始化为指定年、月、日、小时、分钟、秒、毫秒和协调世界时 (utc) 或本地时间。   
             * year:1-9999,month:0-12,day:1-moth中的天数,hour:0-23,minuye:0-59,second:0-59,millisecond:0-999
             * kind:枚举值之一,该值指示 year、month、day、hour、minute、second 和 millisecond 指定了本地时间、
             * 协调世界时 (utc),还是两者皆未指定。
             */
            datetime date4 = new datetime(2018, 9, 29, 13, 48, 30, 500, datetimekind.local);
            console.writeline("{0:m/dd/yyyy h:mm:ss.fff tt} {1}", date4, date4.kind);
            /*
             * 9/29/2018 1:48:30.500 下午 local
             */

            /*
             * 10.public datetime (int year, int month, int day, int hour, int minute, int second, int millisecond, system.globalization.calendar calendar);
             * datetime(int32, int32, int32, int32, int32, int32, int32, calendar)
             * 将 datetime 结构的新实例初始化为指定日历的指定年、月、日、小时、分钟、秒和毫秒。
             * year:1-9999,month:0-12,day:1-moth中的天数,hour:0-23,minuye:0-59,second:0-59,millisecond:0-999
             * calendar:用于解释 year、month 和 day 的日历
             */

            /*
             下面的示例调用datetime(int32, int32, int32, int32, int32, int32, int32, calendar)构造函数两次实例化两个datetime值。 第一次调用实例化datetime通过使用值persiancalendar对象。 由于波斯日历不能指定为区域性的默认日历,显示日期与波斯历需要单独调用其persiancalendar.getmonth, persiancalendar.getdayofmonth,和persiancalendar.getyear方法。 构造函数的第二个调用实例化datetime通过使用值hijricalendar对象。 该示例将当前区域性更改为阿拉伯语 (叙利亚) 和当前区域性的默认日历更改为回历。 因为回历是当前区域性的默认日历,console.writeline方法使用它来设置日期格式。 还原先前的当前区域性 (这在此情况下是英语 (美国)) 时,console.writeline方法使用当前区域性的默认公历日历来设置日期格式。
             */
            console.writeline("using the persian calendar:");
            persiancalendar persian2 = new persiancalendar();
            datetime date5 = new datetime(1397, 3, 29, 16, 32, 18, 500, persian2);
            console.writeline(date5.tostring("m/dd/yyyy h:mm:ss.fff tt"));
            console.writeline("{0}/{1}/{2} {3}{7}{4:d2}{7}{5:d2}.{6:g3}\n",
                                             persian2.getmonth(date5),
                                             persian2.getdayofmonth(date5),
                                             persian2.getyear(date5),
                                             persian2.gethour(date5),
                                             persian2.getminute(date5),
                                             persian2.getsecond(date5),
                                             persian2.getmilliseconds(date5),
                                             datetimeformatinfo.currentinfo.timeseparator);

            console.writeline("using the hijri calendar:");
            // get current culture so it can later be restored.
            cultureinfo dftculture2 = thread.currentthread.currentculture;

            // define strings for use in composite formatting.
            string dformat2;
            string fmtstring;
            // define hijri calendar.
            hijricalendar hijri2 = new hijricalendar();
            // make ar-sy the current culture and hijri the current calendar.
            thread.currentthread.currentculture = new cultureinfo("ar-sy");
            cultureinfo current2 = cultureinfo.currentculture;
            current2.datetimeformat.calendar = hijri2;
            dformat2 = current.datetimeformat.shortdatepattern;
            // ensure year is displayed as four digits.
            dformat2 = regex.replace(dformat2, "/yy$", "/yyyy") + " h:mm:ss.fff";
            fmtstring = "{0} culture using the {1} calendar: {2:" + dformat2 + "}";
            datetime date6 = new datetime(1431, 9, 9, 16, 32, 18, 500, hijri2);
            console.writeline(fmtstring, current2, getcalendarname(hijri2), date6);

            // restore previous culture.
            thread.currentthread.currentculture = dftculture;
            dformat2 = datetimeformatinfo.currentinfo.shortdatepattern + " h:mm:ss.fff";
            fmtstring = "{0} culture using the {1} calendar: {2:" + dformat2 + "}";
            console.writeline(fmtstring,
                              cultureinfo.currentculture,
                              getcalendarname(cultureinfo.currentculture.calendar),
                              date6);

            /*
             using the persian calendar:
             6/19/2018 4:32:18.500 下午
             3/29/1397 16:32:18.500

             using the hijri calendar:
             ar-sy culture using the hijri calendar: 09/09/1431 16:32:18.500
             zh-cn culture using the gregorian calendar: 2010/8/18 16:32:18.500
             */

            /*
             * 11.public datetime (int year, int month, int day, int hour, int minute, int second, int millisecond, system.globalization.calendar calendar, datetimekind kind);
             * datetime(int32, int32, int32, int32, int32, int32, int32, calendar, datetimekind)
             * 将 datetime 结构的新实例初始化为指定日历的指定年、月、日、小时、分钟、秒、毫秒和协调世界时 (utc) 或本地时间。
             * year:1-9999,month:0-12,day:1-moth中的天数,hour:0-23,minuye:0-59,second:0-59,millisecond:0-999
             * calendar:用于解释 year、month 和 day 的日历
             * kind:枚举值之一,该值指示 year、month、day、hour、minute、second 和 millisecond 指定了本地时间、
             * 协调世界时 (utc),还是两者皆未指定。
             */
            /*
            下面的示例调用datetime(int32, int32, int32, int32, int32, int32, int32, calendar, datetimekind)构造函数两次实例化两个datetime值。 第一次调用实例化datetime通过使用值persiancalendar对象。 由于波斯日历不能指定为区域性的默认日历,显示日期与波斯历需要单独调用其persiancalendar.getmonth, persiancalendar.getdayofmonth,和persiancalendar.getyear方法。 构造函数的第二个调用实例化datetime通过使用值hijricalendar对象。 该示例将当前区域性更改为阿拉伯语 (叙利亚) 和当前区域性的默认日历更改为回历。 因为回历是当前区域性的默认日历,console.writeline方法使用它来设置日期格式。 还原先前的当前区域性 (这在此情况下是英语 (美国)) 时,console.writeline方法使用当前区域性的默认公历日历来设置日期格式。 
            */
            console.writeline("using the persian calendar:");
            persiancalendar persian3 = new persiancalendar();
            datetime date7 = new datetime(1397, 3, 29, 16, 32, 18, 500,
                                          persian3, datetimekind.local);
            console.writeline("{0:m/dd/yyyy h:mm:ss.fff tt} {1}", date7, date7.kind);
            console.writeline("{0}/{1}/{2} {3}{8}{4:d2}{8}{5:d2}.{6:g3} {7}\n",
                                             persian3.getmonth(date7),
                                             persian3.getdayofmonth(date7),
                                             persian3.getyear(date7),
                                             persian3.gethour(date7),
                                             persian3.getminute(date7),
                                             persian3.getsecond(date7),
                                             persian3.getmilliseconds(date7),
                                             date7.kind,
                                             datetimeformatinfo.currentinfo.timeseparator);

            console.writeline("using the hijri calendar:");
            // get current culture so it can later be restored.
            cultureinfo dftculture3 = thread.currentthread.currentculture;

            // define strings for use in composite formatting.
            string dformat3;
            string fmtstring3;
            // define hijri calendar.
            hijricalendar hijri3 = new hijricalendar();
            // make ar-sy the current culture and hijri the current calendar.
            thread.currentthread.currentculture = new cultureinfo("ar-sy");
            cultureinfo current3 = cultureinfo.currentculture;
            current3.datetimeformat.calendar = hijri3;
            dformat3 = current3.datetimeformat.shortdatepattern;
            // ensure year is displayed as four digits.
            dformat3 = regex.replace(dformat3, "/yy$", "/yyyy") + " h:mm:ss.fff";
            fmtstring3 = "{0} culture using the {1} calendar: {2:" + dformat3 + "} {3}";
            datetime date8 = new datetime(1431, 9, 9, 16, 32, 18, 500,
                                          hijri3, datetimekind.local);
            console.writeline(fmtstring3, current3, getcalendarname(hijri3),
                              date8, date8.kind);

            // restore previous culture.
            thread.currentthread.currentculture = dftculture3;
            dformat3 = datetimeformatinfo.currentinfo.shortdatepattern + " h:mm:ss.fff";
            fmtstring3= "{0} culture using the {1} calendar: {2:" + dformat3 + "} {3}";
            console.writeline(fmtstring3,
                              cultureinfo.currentculture,
                              getcalendarname(cultureinfo.currentculture.calendar),
                              date8, date8.kind);
            /*
             using the persian calendar:
             6/19/2018 4:32:18.500 下午 local
             3/29/1397 16:32:18.500 local

             using the hijri calendar:
             ar-sy culture using the hijri calendar: 09/09/1431 16:32:18.500 local
             zh-cn culture using the gregorian calendar: 2010/8/18 16:32:18.500 local
             */
            console.readkey();
        }
        private static string getcalendarname(calendar cal)
        {
            return regex.match(cal.tostring(), "\\.(\\w+)calendar").groups[1].value;
        }
    }
}

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网