Thursday, November 29, 2012

Convert json date to C#, code for tested for windows 8 and android devices developed with mono and ,


 Convert json date to C#, code for tested for windows 8 and android devices developed with mono and ,
this method uses "TimeZoneInfo.Local" Most of the time it will return in GMT(GreenWich Mean Time format)

public string ConvertJsonDateToDate(string dateString)
{
    string ConvertedDate = null;
    if (string.IsNullOrEmpty(dateString))
    {
        return DateTime.Now.ToString();
    }
    Regex re = new Regex(@"(\d+)([-+]\d{4})");
    Match match = re.Match(dateString);
    long timestamp = 0; int offset = 0;
    if (!string.IsNullOrEmpty(match.Groups[1].Value))
    {
        timestamp = Convert.ToInt64(match.Groups[1].Value);
        offset = Convert.ToInt32(match.Groups[2].Value) / 100;
    }
    ConvertedDate = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1).AddMilliseconds(timestamp), TimeZoneInfo.Local).ToString();
    return ConvertedDate + " GMT";

}


No comments:

Post a Comment