12 September 2016

Convert time-stamp in UTC format to local date time

Some times it is required to convert a time-stamp in UTC format to your local system data time.

A UTC time-stamp format looks something like this - 2016-08-23T11:59:00Z

This time-stamp is in UTC zone and following C# code can be used to convert this into local date time. As an example, here I am converting it to 'Central Standard Time (CST)'. Depending on your time zone, you can pass a different value.

public static string GetLocalDateTime(string timeStamp)
{
    TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
    DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(Convert.ToDateTime(timeStamp), 
                                                       cstZone);
    return cstTime.ToString();
}


An example call will be - 

var localDateTime = GetLocalDateTime("2016-08-23T11:59:00Z");


Output will be -  8/23/2016 6:59:00 AM

No comments: