Today, we know that datetime. tostring has so many other methods. With this example, it is much easier to get time strings of different formats.
Using system;
Using system. Globalization;
Public class mainclass {
Public static void main (string [] ARGs ){
Datetime dt = datetime. now;
String [] format = {
"D", "D ",
"F", "F ",
"G", "g ",
"M ",
"R ",
"S ",
"T", "T ",
"U", "U ",
"Y ",
"Dddd, Mmmm dd YYYY ",
"DDD, mmm D/" '/"YY ",
"Dddd, Mmmm dd ",
"M/YY ",
"DD-mm-yy ",
};
String date;
For (INT I = 0; I <format. length; I ++ ){
Date = DT. tostring (Format [I], datetimeformatinfo. invariantinfo );
Console. writeline (string. Concat (Format [I], ":", date ));
}
/** output.
* D: 08/17/2000
* D: Thursday, August 17,200 0
* F: Thursday, august 17,200 0 16:32:32
* F: Thursday, August 17,200 0 16:32:32
* G: 08/17/2000
* G: 08/17/2000
* m: august 17
* r: Thu, 17 Aug 2000 23:32:32 GMT
* s: 2000-08-17t16: 32: 32
* T: 16: 32
* T: 16: 32: 32
* u: 2000-08-17 23: 32: 32z
* u: Thursday, august 17,200 0 23:32:32
* Y: August, 2000
* dddd, Mmmm dd yyyy: Thursday, August 17 2000
* DDD, mmm D "'" YY: Thu, Aug 17' 00
* dddd, Mmmm DD: Thursday, August 17
* m/YY: 8/00
* DD-mm-yy: 17-08-00
*/
}< BR >}