none
Want to convert string in form of "yyyy-MM-ddTHH:mm:ss.fffZ" to DateTime object RRS feed

  • Question

  • I am receiving date time value in string which is in "yyyy-MM-ddTHH:mm:ss.fffZ" format. Now I want to convert it to DateTime object.

    I have used following code to convert it.

    DateTime newDate = DateTime.ParseExact(OriginalDateString, "yyyy-MM-ddTHH:mm:ss.fffZ" ,CultureInfo.InvariantCulture);

    However, this will give me wrong date time. Say, if currently if i will pass "2012-10-01T12:57:00.455Z" as a string it is returning 10/1/2012 6:27:00 PM which is wrong. It should return 10/1/2012 12:57:00 PM

    Let me know which DateTime function I should use to covert this date.

    Thanks,

    Ankur

    Monday, October 1, 2012 7:28 AM

Answers

  • Hi Ankur,

    Welcome to the MSDN Forum.

    Please try this code:

    DateTime newDate= DateTime.ParseExact("2012-10-01T12:57:00.455Z", "yyyy-MM-ddTHH:mm:ss.fffZ", CultureInfo.InvariantCulture);
            Console.WriteLine(newDate.ToUniversalTime);

    Or 

            DateTime newDate = DateTime.ParseExact("2012-10-01T12:57:00.455", "yyyy-MM-ddTHH:mm:ss.fff", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal);
            Console.WriteLine(newDate);

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Wednesday, October 3, 2012 11:50 AM
    Moderator
  • Hi Mike,

    Thanks for the answer. The DateTime value which i am receiving is not local. It is of specified timezone. The problem is I am getting Datetime value in string format. So before converting into the UTC i need to convert it in DateTime object.

    to convert it I have tried below and it is working.

     DateTime newDate = DateTime.ParseExact("2012-10-01T12:57:00.455", "yyyy-MM-ddTHH:mm:ss.fff", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);

    Thanks,

    Ankur

    • Marked as answer by convey2ankur Wednesday, October 10, 2012 5:05 AM
    Wednesday, October 10, 2012 5:05 AM

All replies

  • You just need to add local time.  You are getting the GMT time instead.

    DateTime

    newDate = DateTime.ParseExact(OriginalDateString.ToLocalTime, "yyyy-MM-ddTHH:mm:ss.fffZ", CultureInfo.InvariantCulture);

    jdweng

    Monday, October 1, 2012 12:13 PM
  • Hi Ankur,

    Welcome to the MSDN Forum.

    Please try this code:

    DateTime newDate= DateTime.ParseExact("2012-10-01T12:57:00.455Z", "yyyy-MM-ddTHH:mm:ss.fffZ", CultureInfo.InvariantCulture);
            Console.WriteLine(newDate.ToUniversalTime);

    Or 

            DateTime newDate = DateTime.ParseExact("2012-10-01T12:57:00.455", "yyyy-MM-ddTHH:mm:ss.fff", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal);
            Console.WriteLine(newDate);

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Wednesday, October 3, 2012 11:50 AM
    Moderator
  • Hi Mike,

    Thanks for the answer. The DateTime value which i am receiving is not local. It is of specified timezone. The problem is I am getting Datetime value in string format. So before converting into the UTC i need to convert it in DateTime object.

    to convert it I have tried below and it is working.

     DateTime newDate = DateTime.ParseExact("2012-10-01T12:57:00.455", "yyyy-MM-ddTHH:mm:ss.fff", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);

    Thanks,

    Ankur

    • Marked as answer by convey2ankur Wednesday, October 10, 2012 5:05 AM
    Wednesday, October 10, 2012 5:05 AM