0

How can I convert a date in the following format to a date in the India time zone (UTC+5:30) using C#?

2012-09-13T05:08:03.151Z
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254

2 Answers2

1

How about

DateTime dt = DateTime.ParseExact("2012-09-13T05:08:03.151Z",
                                  "yyyy-MM-dd HH:mm:ssK",
                                  CultureInfo.InvariantCulture)

Then

var indianTime = TimeZoneInfo.ConvertTime (dt,
                     TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"));
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
1

Try this:

DateTime dt = DateTime.ParseExact("your current date string","your current date string format",null);
string IndianDT = dt.ToString("dd/MM/yyy");

Now in your IndianDT string you will have your Date desired format.

Edit:

In my above code:

replace "your current date string fromat" with "yyyy-MM-ddThh:mm:ss.fffZ"

  • String was not recognized as a valid Date Time.I got that error in using this code – user1648773 Sep 13 '12 at 12:17
  • please mention your 'current date format' string you used in my code. –  Sep 13 '12 at 12:21
  • Use following : DateTime dt = DateTime.ParseExact("your current date string","yyyy-MM-ddThh:mm:ss.sssZ",null); –  Sep 13 '12 at 12:33
  • DateTime pattern 's' appears more than once with different values. – user1648773 Sep 13 '12 at 12:47
  • can you give me an example of what these s h t Z etc mean.... EDITED can you please provide me some reference to support you answers... thanks dude, i appreciate your patience. – user1648773 Sep 13 '12 at 12:48
  • s-second, h-hour, t to specify time in UTC format and Z is to define zone as per standards. Did solution worked for you....??? –  Sep 13 '12 at 12:54
  • No Again error - this time it was "DateTime pattern 's' appears more than once with different values." – user1648773 Sep 13 '12 at 12:57
  • Try this one : "yyyy-MM-ddThh:mm:ss.SSSZ". –  Sep 13 '12 at 13:11
  • can u help me in 2012-09-13T05:08:03.151Z this one come on which time zone – user1648773 Sep 14 '12 at 05:30
  • do google to get zone and you need to hchange a code little bit from "yyyy-MM-ddThh:mm:ss.SSSZ" to "yyyy-MM-ddThh:mm:ss.fffZ". In dot.net fraction of seconds are denoted by fff not SSS which was mentioned in my earlier code. –  Sep 14 '12 at 06:03
  • I have a date field in this here it give me kind unspecify how can i change it in c# – user1648773 Sep 14 '12 at 09:25