-3

I have got a time format from a file in this format "03:45:00 AM CST" from a column record. Additionally I have to convert this into utc and then to est time zone or local system's time. I have tried this approach so far but all in vein. Any help would be appreciated.

//Converting Time in CST to UTC 
f= "03:45:00 AM CST"
var fetchtime = f.Substring(0,12);
var converttimetostring = Convert.ToDateTime(fetchtime).ToString();
var timeconvert = DateTime.ParseExact(converttimetostring,"HH:mm:ss",CultureInfo.InvariantCulture);

DateTime timeUtc = DateTime.UtcNow;
TimeZoneInfo cst = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
DateTime cstTime = TimeZoneInfo.ConvertTimeToUtc(timeconvert,cst);
Console.WriteLine(cstTime);

//Converting time FROM UTC to EST(East. Standard Time)
DateTime localtime = cstTime.ToLocalTime();
Console.WriteLine(localtime);
David Specht
  • 7,784
  • 1
  • 22
  • 30
Shreya Rawal
  • 182
  • 3
  • 14
  • Can to explain what's the issue here? What's the output you are getting? What's the output you are expecting? – Chetan Jul 23 '20 at 19:20
  • This is the input : 03:45:00 AM CST and the expected output should be 2:45 AM -- This is est time zone. Right now it is giving me an error System.Format.Exception: String was not recognized as a valid datetime. – Shreya Rawal Jul 23 '20 at 19:23
  • I'm not a Noda time expert at all, but often the answer to questions like this is "Have you looked at Noda Time?" – Flydog57 Jul 23 '20 at 19:25
  • Why did you parse string to date and then back? `DateTime.ParseExact` will throw an exception in my end – Pavel Anikhouski Jul 23 '20 at 19:26
  • Do you have to convert to UTC? What do you do in the summer (when Daylight Saving Time is in effect)? Could you take advantage of the fact that the difference between Eastern Time and Central Time is always 1 hour (well, other than, I think, one hour in the fall and one hour in the spring during the change into and out of Daylight Saving Time)? – Flydog57 Jul 23 '20 at 19:28
  • @PavelAnikhouski because if I dont do that it gives me an error saying that 'cannot convert from System.DateTime' to 'string'. – Shreya Rawal Jul 23 '20 at 19:30
  • Which line in the code throws the exception? – Chetan Jul 23 '20 at 19:32
  • @ChetanRanpariya var timeconvert = DateTime.ParseExact(converttimetostring,"HH:mm:ss",CultureInfo.InvariantCulture); This one. – Shreya Rawal Jul 23 '20 at 19:33
  • Why not pass `fetchtime`to `DateTime.ParseExact` directly? Your current call also throws an exception – Pavel Anikhouski Jul 23 '20 at 19:33
  • I think you should just do `var timeconvert= Convert.ToDateTime(fetchtime);` to get the DateTime. You some need `var timeconvert = DateTime.ParseExact(....` line in the code – Chetan Jul 23 '20 at 19:35
  • @PavelAnikhouski It still shows the System.FormatException on doing that. – Shreya Rawal Jul 23 '20 at 19:36
  • Then your question is how to parse string to time, not about timezones – Pavel Anikhouski Jul 23 '20 at 19:37
  • Yes you can consider that as my subquestion @PavelAnikhouski – Shreya Rawal Jul 23 '20 at 19:38

1 Answers1

0

I have used the following code snippet for getting the output as "04:45:00 AM" --which is one hour difference between cst and est.

//Converting from cst to UTC
f="03:45:00 AM CST";
var fetchtime = f.Substring(0,12);
DateTime timeUtc = DateTime.UtcNow;
TimeZoneInfo cst = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
var a = DateTime.Parse(fetchtime);
DateTime cstTime = TimeZoneInfo.ConvertTimeToUtc(a,cst);
Console.WriteLine(cstTime);

//Converting time FROM UTC to EST(East. Standard Time)
DateTime localtime = cstTime.ToLocalTime();
Console.WriteLine(localtime);
Shreya Rawal
  • 182
  • 3
  • 14