C # About Time processing

Source: Internet
Author: User
Tags current time datetime local time time interval tostring

A. DateTime

In C #, the system provides a lot of functions about time processing, and we can get the system time through System.DateTime.Now. Of course, you can also use a DateTime class to create a time object. Here are a few examples of how to get the system time.

DateTime DT1 = new DateTime (2004, 10, 19);

Console.WriteLine ("DT1: {0}", DT1);

DateTime DT2 = new DateTime (2004, 10, 19, 22, 47, 35);

Console.WriteLine ("DT2: {0}", DT2);

DateTime DT3 = new DateTime (2004, 10, 19, 22, 47, 35, 259);

Console.WriteLine ("DT3: {0}", DT3);

stringtime1= System.DateTime.Now.ToString ("Yyyy-mm-dd-hh-mm-ss");

Console.WriteLine (TIME1);

The result of the program running is the screenshot

System.DateTime.Now.ToString ("Yyyy-mm-dd-hh-mm-ss") means to get the system's current time and display it in the year-month-day-hour-minute-seconds format.

Then look at the next Tube time class: Timespan, the literal meaning should know its Chinese meaning time interval. First look at an example:

TimeSpan Oneweek = new TimeSpan (4, 12, 0, 0) +

New TimeSpan (2, 12, 0, 0);

The parameters in (4, 12, 0, 0) represent

Days,hours,minutes,seconds.

DateTime now = DateTime.Now;

DateTime past = Now-oneweek;

Console.WriteLine (Oneweek. ToString ());

Console.WriteLine (now. ToString ());

Console.WriteLine (past. ToString ());

When you output the results

. Add x seconds to the current time./minute/hour/day operation.

DateTime time1 = DateTime.Now;

DateTime time2 = DateTime.Now.AddYears (10);

DateTime Time3 = DateTime.Now.AddMonths (11);

DateTime time4 = DateTime.Now.AddDays (7);

DateTime time5 = DateTime.Now.AddHours (12);

Console.WriteLine (time1. ToString ());

Console.WriteLine (time2. ToString ());

Console.WriteLine (Time3. ToString ());

Console.WriteLine (Time4. ToString ());

Console.WriteLine (Time5. ToString ());

The results of the program run are:

DateTime time2 = DateTime.Now.AddYears (10) means that the current time plus 10 years after the assignment to time2;

About local time and standard UTC time: UTC time, which is Greenwich Mean time.

DateTime local=datetime.now;//local time

Console.WriteLine ("{0},{1},{2}", Local.) ToString (), Local. Kind.tostring (), TimeZone.CurrentTimeZone.StandardName);

DateTime utctime=datetime.utcnow;//Coordinated Universal Time;

Console.WriteLine ("{0},{1}", Utctime.) ToString (), Utctime. Kind.tostring ());

Two. Questions relating to the time format

The format of the time display can be 24 or 12 hours, and there are other ways.

DateTime local=datetime.now;//local time

Console.WriteLine ("The Time is{0:hh:mm tt}", local);

Console.WriteLine ("Date is {0:ddd MMM dd, yyyy}", local);

Console.WriteLine ("{0:ddd MMM dd,yyyy}", local);

Console.WriteLine ("{0:hh:mm:ss tt}", local);

Console.WriteLine ("{0:m}", local);

TimeSpan in the process of time processing has a lot of role, such as the calculation of two time between the time between how long, or 1 months after today is the date.

The TimeSpan constructors are:

TimeSpan Oneweek = new TimeSpan (4, 0, 0) + new TimeSpan (2, 12, 0, 0);

Then Oneweek equals 7 days: 4.5 days plus 2.5 days.

TimeSpan TP = new TimeSpan (1,50,20,10);//dd hh mm SS

TimeSpan TP2 = new TimeSpan (1, m)/hh mm SS

TimeSpan TP3 = new TimeSpan (1,1,1,1,1);//dd hh mm SS MSMs

The parameters in (4, 12, 0, 0) represent Days,hours,minutes,seconds respectively.

At the same time, we can use TimeSpan static method to assign value to TimeSpan instance object

Take a look at the following example:

TimeSpan SPA1 = timespan.fromdays (10);

TimeSpan SPA2 = timespan.fromhours (10);

TimeSpan spa3 = timespan.fromminutes (44);

TimeSpan spa4 = timespan.fromseconds (5);

TimeSpan Spa5 = Timespan.frommilliseconds (55);

TimeSpan SPA6 = timespan.fromticks (200);//Time scale

Console.WriteLine (SPA1);

Console.WriteLine (SPA2);

Console.WriteLine (SPA3);

Console.WriteLine (SPA4);

Console.WriteLine (SPA5);

Console.WriteLine (SPA6);

The output results are:

The next section describes the knowledge of array and ArrayList in C #.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.