简体   繁体   中英

How to get datetime.now format into this datetime fomat? C#

Im trying to have an app which finds the shortest time I have tried for a bit to get the datetime.now format into this example format which my code uses:

Comparetime.Add(new DateTime(2000, 1, 1, 3, 29, 0));

I need the Datetime.Now code be in this string format. I have tried with a similar string:

testbox.Text = DateTime.UtcNow.ToString("YYYY-MMM-DD hh:mm:ss TT");

But that fails to produce the same format as (2000, 1, 1, 3, 29, 0)

This works for me:

testbox.Text = DateTime.UtcNow.ToString("yyyy-MMM-dd hh:mm:ss tt");

Only the month designator should be capitalized.

GetDatTimeFormats() gets a string[] of the formats. Choose the one you want.

I guess this is wat you are looking for:

string[] listOfAvbDateTimeFormats = DateTime.UtcNow.GetDateTimeFormats();
string dateTimeFormatted = Convert.ToString(listOfAvbDateTimeFormats[92]);

Cheers!

If you use following format string s = DateTime.UtcNow.ToString("yyyy-MM-dd hh:mm:ss tt");

it gives you string like: "2013-08-29 06:16:18 PM"

however, if you use 'MMM' in the above format, it gives you month as a word.

string s = DateTime.UtcNow.ToString("yyyy-MMM-dd hh:mm:ss tt"); "2013-Aug-29 06:16:59 PM"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM