C # (C Sharp) string and time conversion to each other

Source: Internet
Author: User
Tags datetime

C # (C sharp ) The conversion of strings and times.

One, datetime–> string

The time type is converted to a string type, which is fairly simple, and calls the ToString () method directly. Such as:

DateTime dt = DateTime.Now;

String dtstr = dt. ToString ();

If you want to format the output, you can write this:

Dt.        ToString ("YYYY year mm month DD Day"); November 5, 2005
Dt.                      ToString ("Yyyy-mm-dd"); 2005-11-5

String.                  Format ("{0:d}", DT); 2005-11-5
String.                  Format ("{0:d}", DT); November 5, 2005

The Time type format (a character type) can be in two ways: 1, custom time format. own definition of the composition and representation of time; 2. Standard Time format. A limited representation provided by the standard library. (Detailed situation reference list)

Second, string-> DateTime

String dtstring = "2009-10-12 00:12:05";

DateTime dt = DateTime.Parse (DTSTR); Way One

DateTime DT2 = Convert.todatetime (DTSTR); Mode two

Of course datetime also has a variety of representations (unformatted to string type), such as:

Dt.                  TOFILETIMEUTC (); 127756704859912816
Dt.                    ToLocalTime (); 2005-11-5 21:21:25
Dt.            Tolongdatestring (); November 5, 2005

Of course, if you want to print it out eventually, you need ToString () to convert to a string type.

Other methods of time, attributes and operations

Dt. Year//2005 dt. Date//2005-11-5 0:00:00 dt. DayOfWeek//saturday dt. DayOfYear//309 dt. Hour//13 dt.millisecond//441 dt. Minute//30 dt. Month//11 dt. Second//28 dt. Ticks//632667942284412864 dt. TimeOfDay//13:30:28.4412864 DT//2005-11-5 13:47:04 operation: dt. Addyears (1)//2006-11-5 13:47:04 dt. AddDays (1.1)//2005-11-6 16:11:04 dt. AddHours (1.1)//2005-11-5 14:53:04 dt. Addmilliseconds (1.1)//2005-11-5 13:47:04 dt. Addmonths (1)//2005-12-5 13:47:04 dt. AddSeconds (1.1)//2005-11-5 13:47:05 dt. AddMinutes (1.1)//2005-11-5 13:48:10 dt. Addticks (1000)//2005-11-5 13:47:04 dt.compareto (DT)//0 dt. Equals ("2005-11-6 16:11:04")//false dt. Equals (DT)//true dt. GetHashCode ()//1474088234 dt. GetType ()//system.datetime dt. GetTypeCode ()//datetime

Reference:

Custom DateTime Formatting

There are following custom format specifiers y (year), M (month), D (Day), H (hour), H (Hour), M (minute), S (second ), F (second fraction), F (second fraction, trailing zeroes, are trimmed), t (p.m or a.m) and Z (time zone).

Following examples demonstrate how are the format specifiers rewritten to the output.

Create Date Time 2008-03-09 16:05:07.123 datetime dt = new DateTime (2008, 3, 9, 16, 5, 7, 123); String.Format ("{0:y yy yyy yyyy}", DT); "8 008 2008" Year String.Format ("{0:m MM MMM MMMM}", DT); "3 Mar March" Month String.Format ("{0:d dd ddd dddd}", DT); "9 Sun Sunday" Day String.Format ("{0:h hh H hh}", DT); "4" Hour 12/24 String.Format ("{0:m mm}", DT); "5" Minute String.Format ("{0:s ss}", DT); "7 Modified" second String.Format ("{0:f ff FFF FFFF}", DT); "1 123 1230" Sec.fraction String.Format ("{0:f FF FFF FFFF}", DT); "1 123 123" Without zeroes String.Format ("{0:t tt}", DT); "P PM" a.m. or p.m. String.Format ("{0:z zz zzz}", DT); " -6-06-06:00" time zone

can use also date separator /(slash) and time sepatator : (colon). These characters is rewritten to characters defined in the current Datetimeformatinfo.dateseparator and datetimefor Matinfo.timeseparator.

Date separator in German culture is "." (So "/" changes to ".") String.Format ("{0:d/m/yyyy HH:mm:ss}", DT); "9/3/2008 16:05:07"-中文版 (en-US) String.Format ("{0:d/m/yyyy HH:mm:ss}", DT); "9.3.2008 16:05:07"-German (De-de)

Here are some examples of the custom date and time formatting:

[C #]

Month/day numbers Without/with leading zeroes String.Format ("{0:m/d/yyyy}", DT); "3/9/2008" String.Format ("{0:mm/dd/yyyy}", DT); "03/09/2008"//Day/month names String.Format ("{0:ddd, MMM D, yyyy}", DT); "Sun, Mar 9, 2008" String.Format ("{0:dddd, MMMM D, yyyy}", DT); "Sunday, March 9, 2008"//two/four digit year String.Format ("{0:mm/dd/yy}", DT); "03/09/08" String.Format ("{0:mm/dd/yyyy}", DT); "03/09/2008"

Standard DateTime Formatting

In DateTimeFormatInfo there are defined standard to patterns for the current culture. For example property Shorttimepattern are string that contains value h:mm tt for en -US culture and value hh:mm for de-de culture.

Following table shows patterns defined in DateTimeFormatInfo and their values for en-US culture. The contains format specifiers to the String.Format method.

specifier

DateTimeFormatInfo Property

pattern value (for en-US culture)

T

Shorttimepattern

h:mm tt

D

Shortdatepat Tern

m/d/yyyy

T

Longtimepattern

H:mm:ss tt

D

Longdatepattern

dddd, MMMM dd, yyyy

F

(combination of D and T)

dddd, MMMM dd, yyyy h:mm TT

F

Fulldatetimepattern

dddd, MMMM dd, yyyy h:mm:ss TT

td>

G

(combination of-

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.