简体   繁体   中英

Convert “M/d/yyyy h:mm:ss tt” to “YYYY-MM-DDThh:mm:ss.SSSZ”

As title suggests, what I want to do is to convert my date-string eg

  • "6/6/2014 12:24:30 PM" (M/d/yyyy h:mm:ss tt) format

to

  • "YYYY-MM-DDThh:mm:ss.SSSZ" format.

I am trying in the following way. It's not giving me any exception, but I am getting the value like :

  • "YYYY-06-DDT12:24:30.SSSZ"

How can I exactly achieve this?

string LastSyncDateTime = "6/6/2014 12:24:30 PM";
DateTime dt = DateTime.ParseExact(LastSyncDateTime, "M/d/yyyy h:mm:ss tt",CultureInfo.InvariantCulture);
string result = dt.ToString("YYYY-MM-DDThh:mm:ss.SSSZ");

The simplest way is to use the "o" date formatter, like this:

dt.ToString("o");

This method will give you a timestring in the ISO 8601 format, something like this:

2014-12-25T11:56:54.9571247Z

but, since that ISO 8601 uses more than 3 decimal digits to define the second , if you only want to stop at milliseconds you can use the full formatting string and write down ss.fffzzz at the end, like this:

dt.ToString("yyyy-MM-ddThh:mm:ss.fffzzz");

and the result will be:

2014-12-25T11:56:54.957Z

For more informations you can refer to THIS LIST of date formatting options.

Data time Formats Can't recognize, the non Case sensitive chars in some systems due to their internal settings. Please refer the below code, which works fine

string result = dt.ToString("yyyy-MM-ddThh:mm:ss.SSSZ");

The above code will result as 2014-06-06T12:24:30.SSSZ

EDIT :

The below snippet will give you milliseconds as well

dt.ToString("yyyy-MM-ddThh:mm:ss.fffZ");

but I am getting the value like " YYYY-06-DDT12:24:30.SSSZ "

Let me explain why you get this result. Since there is no custom date and time format as YYYY , DD , SSS or Z , these characters are copied to the result string unchanged .

I strongly suspect you want to use yyyy , dd , fff and z format specifiers instead.

You can use DateTime.TryParseExact method like;

string s = "6/6/2014 12:24:30 PM";
DateTime dt;
if(DateTime.TryParseExact(s, "M/d/yyyy hh:mm:ss tt",
                          CultureInfo.InvariantCulture,
                          DateTimeStyles.None, out dt))
{
    Console.WriteLine(dt.ToString("yyyy-MM-ddThh:mm:ss.fffz"));
}

Here a demonstration on Ideone .

Result will be 2014-06-06T12:24:30.000+3 on my machine because my time zone is UTC +3 right now. Time zone information might change based on your UTC value as well.

Try this -

dt.ToString("o"); dt.ToString("O");

you can see this link

hope this helps

You can use date in this format :(" yyyy-MM-ddThh:mm:ss.000Z ")

Or in manually by changing the time zone you can easily use the date set time zone to ( GMT:000(Greenwich mean time) ) in your setting tab and make sure data type is date/time.

使用yyyy表示年份,使用dd表示日期

  string result = dt.ToString("yyyy-MM-ddThh:mm:ss.SSSZ");

这是快速解决方案:

DateTime.UtcNow.ToString("yyyy-MM-ddThh:mm:ss.fffZ")

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