Conversion of dates and times

Source: Internet
Author: User
Tags date datetime iso 8601 local time string format
Convert the DateTime data type also contains a Format method and a ToString method that return a string-formatted representatio N of the original value. The format method can is used to Format a value in one of fifteen common ways, while the ' ToString ' can only be used To the format a value in one way.

The Format method takes the following form, where XXX is the name of the numeric base data-type:

Virtual XXX Format (string* format,iserviceobjectprovider* sp)
Similar to the Format methods found into the other. NET Framework data types, the DateTime Format method takes a string format c Haracter and an Iserviceobjectprovider object. The Iserviceobjectprovider object specifies the culture. The method defaults to the ' culture if null (in Visual Basic nothing) is passed.

The following table shows the valid format strings.


Format character Description Default return format
D Short Date Pattern mm/dd/yyyy
D Long Date pattern dddd, MMMM dd, yyyy
F Full (long date + short time) dddd, MMMM dd, yyyy hh:mm
F Full Date (long date + long) dddd, MMMM dd, yyyy HH:mm:ss
G General (Short date + short time) mm/dd/yyyy hh:mm
G General (Short date + long time) mm/dd/yyyy HH:mm:ss
M,m Month Day Pattern MMMM DD
R,r RFC1123 pattern ddd, dd MMM yyyy HH ': ' mm ': ' SS ' GMT '
s sortable date time pattern:conforms to ISO 8601 yyyy-mm-dd HH:mm:ss
T short time pattern hh:mm
T Long Time Pattern HH:mm:ss
U Similar to "s" but uses universal Time instead of the local time. YYYY-MM-DD HH:mm:ss
U Universal Sortable Date time pattern dddd, MMMM dd, yyyy HH:mm:ss
Y,y Year Month pattern MMMM, yyyy


Given a DateTime object, MyDate, representing 12:01 AM Sunday, January 1, Watts, the following example illustrates some of The formatting options available:

[C #]
Mydate.format ("D", null);
Returns the string "01/01/2000"

Mydate.format ("D", null);
Returns the string "Sunday, January 1, 2000"

Mydate.format ("F", NULL);
Returns the string "Sunday January 1, 12:01 AM"
The ToString method would quickly convert a DateTime type value into a string. Similar to the other. NET Framework base types, it requires no arguments. Unlike the "Format method", the ToString method was unaffected by the current culture and only takes one form. The following example converts a DateTime value into a string value:

[C #]

DateTime mydate = new DateTime (2000, 01, 01)
Mydate.tostring ();
Returns "01/01/2000 00:00:00"




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.