Search code examples
c#datetime-conversionstring-to-datetime

How can I convert this "2012-08-16T19:20:30.456+08:00" string to DateTime using C#


I want to convert string datetime to Datetime using C#. I am going to store datetime in sql database


Solution

  • The string in your example has an offset component so you can use DateTimeOffset:

    var dateTimeOffset = DateTimeOffset.Parse("2012-08-16T19:20:30.456+08:00", CultureInfo.InvariantCulture);
    

    From the linked docs:

    The DateTimeOffset structure includes a DateTime value, together with an Offset property that defines the difference between the current DateTimeOffset instance's date and time and Coordinated Universal Time (UTC).