Parse Date and Time in specific format

noob

I am getting timestamp string in the following format -

2015-02-01T12:11:06Z

I need to convert it into DateTime object. I tried the following code for this -

timestamp = DateTime.ParseExact(attr.Value,"yyyy-MM-ddThh:mm:ss",null);

But it's throwing Exception.

System.FormatException: String was not recognized as a valid DateTime.

at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)

at System.DateTime.ParseExact(String s, String format, IFormatProvider provider)

I know that my format is wrong. Any way to make it work?

Update
J. Steen's method worked for me and he was the first one to comment, but Soner Gönül provided much better explanation and a better way, so I am accepting Soner Gönül's answer as mentioned here and as J. Steen agreed with this. Thanks to both of you. I am upvoting all the correct answers anyways. :-)

Soner Gönül

You forget the Z part at the end and change hh to HH;

string s = "2015-02-01T12:11:06Z";
DateTime dt;
if(DateTime.TryParseExact(s, "yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture,
                          DateTimeStyles.None, out dt))
{
    Console.WriteLine(dt); // 01/02/2015 14:11:06
}

Since there is no custom date and time format as Z, parsing methods see it as a character literal. But in such a case, your hour part will be different with OffSet since this operation creates your DateTimeKind as Local. Since my current offset is +02:00 right now, it will generate 14:11:06, not 12:11:06

string s = "2015-02-01T12:11:06";
DateTime dt;
if(DateTime.TryParseExact(s, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture,
                          DateTimeStyles.None, out dt))
{
    Console.WriteLine(dt); // 01/02/2015 12:11:06
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

What is this date and time format?

分類Dev

Format date string in a specific pattern

分類Dev

MySQL date interval with specific date format

分類Dev

Convert GMT date format to only time format

分類Dev

Parse UTC date string and convert to different format

分類Dev

Android date format parse throwing an Unhandled Exception

分類Dev

pandas date-time format VS google sheets date format

分類Dev

Parse date with AEDT and AEST time zone in java

分類Dev

How to select specific date format in column

分類Dev

Date conversion to specific format using Angular pipes

分類Dev

Using momentjs to format a date in a specific locale

分類Dev

How to change Date/Time format to English?

分類Dev

How to get current date time format in SQlite?

分類Dev

Karati Api - Validating date and time format

分類Dev

Inconsistent date time format for German locale

分類Dev

Convert miliseconds to date and time and format by locale

分類Dev

R : how to define date and time format

分類Dev

format a time series as dataframe with julian date

分類Dev

How to change date-time format?

分類Dev

convert text string to date/time format

分類Dev

PHP - Convert this string to another date/time format

分類Dev

Formatter in DateTimeFormatter for ISO 8601 date format of the time

分類Dev

How to parse the date and time depends upon the time zone?

分類Dev

What is the Excel Date Format to retain dates in this specific format?

分類Dev

Get Date with only given specific time - for today

分類Dev

Momentjs timezone - getting date at time in specific timezone

分類Dev

Search for records updated on specific date and time

分類Dev

Having a date take effect at a specific time of day

分類Dev

How to input a date with a specific date format in struts 2.2.3.1?

Related 関連記事

  1. 1

    What is this date and time format?

  2. 2

    Format date string in a specific pattern

  3. 3

    MySQL date interval with specific date format

  4. 4

    Convert GMT date format to only time format

  5. 5

    Parse UTC date string and convert to different format

  6. 6

    Android date format parse throwing an Unhandled Exception

  7. 7

    pandas date-time format VS google sheets date format

  8. 8

    Parse date with AEDT and AEST time zone in java

  9. 9

    How to select specific date format in column

  10. 10

    Date conversion to specific format using Angular pipes

  11. 11

    Using momentjs to format a date in a specific locale

  12. 12

    How to change Date/Time format to English?

  13. 13

    How to get current date time format in SQlite?

  14. 14

    Karati Api - Validating date and time format

  15. 15

    Inconsistent date time format for German locale

  16. 16

    Convert miliseconds to date and time and format by locale

  17. 17

    R : how to define date and time format

  18. 18

    format a time series as dataframe with julian date

  19. 19

    How to change date-time format?

  20. 20

    convert text string to date/time format

  21. 21

    PHP - Convert this string to another date/time format

  22. 22

    Formatter in DateTimeFormatter for ISO 8601 date format of the time

  23. 23

    How to parse the date and time depends upon the time zone?

  24. 24

    What is the Excel Date Format to retain dates in this specific format?

  25. 25

    Get Date with only given specific time - for today

  26. 26

    Momentjs timezone - getting date at time in specific timezone

  27. 27

    Search for records updated on specific date and time

  28. 28

    Having a date take effect at a specific time of day

  29. 29

    How to input a date with a specific date format in struts 2.2.3.1?

ホットタグ

アーカイブ