I have date in string format as following
Tue Jun 30 2015 00:00:00 GMT+0530 (India Standard Time)
I want to convert it to DateTime
in c#.
I am getting this date from telerik datepicker using javascript.
I have date in string format as following
Tue Jun 30 2015 00:00:00 GMT+0530 (India Standard Time)
I want to convert it to DateTime
in c#.
I am getting this date from telerik datepicker using javascript.
Since your string has UTC offset, I would parse it to DateTimeOffset
instead. And there is no way to parse your GMT
and (India Standard Time)
parts without using literal string delimiter. Remember, neither DateTime
nor DateTimeOffset
are timezone aware. DateTimeOffset
is little bit better at least since this knows about a UTC instant and an offset from that.
var s = "Tue Jun 30 2015 00:00:00 GMT+0530 (India Standard Time)";
DateTimeOffset dto;
if (DateTimeOffset.TryParseExact(s, "ddd MMM dd yyyy HH:mm:ss 'GMT'K '(India Standard Time)'",
CultureInfo.InvariantCulture,
DateTimeStyles.None, out dto))
{
Console.WriteLine(dto);
}
Now you have a DateTimeOffset
as {30.06.2015 00:00:00 +05:30}
As an alternative (and better option), Nodatime has ZonedDateTime
structure which is;
A
LocalDateTime
in a specific time zone and with a particular offset to distinguish between otherwise-ambiguous instants.