I have a UTC timestamp coming in UTC (string type). I want this timestamp to be converted to Eastern Time. The problem is when i call DateTime.Parse or Convert.ToDateTime on the UTC timestamp, it is converting it to my local time, which is Central Time.
How can I take a string timestamp and convert it to Eastern Time regardless of the local time of the server its running on?
I will try to dissect your question, but next time please show some code so everyone can see exactly what you mean more clearly.
I have a UTC timestamp coming in UTC (string type).
Ok, I assume you mean something like this:
string utcString = "2014-02-25T12:34:56.000Z";
This is an ISO-8601 UTC timestamp. If it's in some other format, let me know in comments and I will update the answer accordingly.
... The problem is when I call
DateTime.Parse
orConvert.ToDateTime
on the UTC timestamp, it is converting it to my local time, which is Central Time.
DateTime utcDateTime = DateTime.Parse(utcString,
CultureInfo.InvariantCulture,
DateTimeStyles.RoundtripKind);
The RoundtripKind
style tells the parser to look for "kind" information in the input string, such as the Z
that indicates UTC. The resulting DateTime
will have the original value, and it's .Kind
property set to DateTimeKind.Utc
.
... I want this timestamp to be converted to Eastern Time.
Now that you have a UTC DateTime
, you can convert it easily using the TimeZoneInfo
class.
TimeZoneInfo easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById(
"Eastern Standard Time");
DateTime easternDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime,
easternTimeZone);
That's it. The result is a DateTime
containing the local time in the US Eastern time zone. It's kind will be Unspecified
, as the time zone information is not carried along with the DateTime
object.
Also note that "Eastern Standard Time" refers to the entire US Eastern time zone, inclusive of both EST and EDT.
Take a look here at the documentation.
Basically you need to use TimeZoneInfo.FindSystemTimeZoneById("<Timezone>");
Then use that as the 3rd parameter for the ConvertTime
function. Parameter 2 being your current timezone.
DateTime hwTime = new DateTime(2007, 02, 01, 08, 00, 00);
try
{
TimeZoneInfo hwZone = TimeZoneInfo.FindSystemTimeZoneById("Hawaiian Standard Time");
Console.WriteLine("{0} {1} is {2} local time.",
hwTime,
hwZone.IsDaylightSavingTime(hwTime) ? hwZone.DaylightName : hwZone.StandardName,
TimeZoneInfo.ConvertTime(hwTime, hwZone, TimeZoneInfo.Local));
}
catch (TimeZoneNotFoundException)
{
Console.WriteLine("The registry does not define the Hawaiian Standard Time zone.");
}
catch (InvalidTimeZoneException)
{
Console.WriteLine("Registry data on the Hawaiian STandard Time zone has been corrupted.");
}
I found a convenient solution:
var e_europe = TimeZoneInfo.FindSystemTimeZoneById("E. Europe Standard Time");
var utcnow = DateTime.UtcNow;
Console.WriteLine(utcnow.ToString());
Console.WriteLine(TimeZoneInfo.ConvertTimeFromUtc(utcnow, e_europe).ToString());
it prints
2/26/2014 1:03:43 AM
2/26/2014 3:03:43 AM
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With