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

As the title says, I want to convert a date string, for example.

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

to

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

I am trying to do the following. This gives me no exceptions, but I get the value as:

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

How can I 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");
+4
source share
6 answers

Data Time Formats Cannot recognize case-insensitive characters on some systems due to their internal settings. Please refer to the code below that works great

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

The above code will look like 2014-06-06T12:24:30.SSSZ

EDIT:

The following snippet will give you milliseconds, and

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

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

, . YYYY, DD, SSS Z, .

, YYYY, DD, fff Z .

DateTime.TryParseExact , :

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"));
}

demonstration on Ideone.

2014-06-06T12:24:30.000+3 , UTC +3 . UTC.

+3

- "o", :

dt.ToString("o");

timestring ISO 8601, :

2014-12-25T11:56:54.9571247Z

ISO 8601 , , ss.fffzzz, :

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

:

2014-12-25T11:56:54.957Z

.

+2

-

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

,

+1

:( yyyy-MM-ddThh: mm: ss.000Z ")

Or manually, by changing the time zone, you can easily use the date time zone ( GMT: 000 (GMT) ) on the settings tab and make sure that the data type is date / time.

+1
source

Use yyyyfor year and ddfor date.

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

Source: https://habr.com/ru/post/1569037/


All Articles