-
-
Save shibayan/2650881 to your computer and use it in GitHub Desktop.
Convert to DateTimeOffset from Json Date(Unix epoch)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Convert to DateTimeOffset from Json Date(Unix epoch) | |
/// </summary> | |
/// <param name="jsondate">ex: "/Date(1324707957994+0900)/"</param> | |
/// <returns></returns> | |
public Nullable<DateTimeOffset> ConvertToDateTimeOffsetFromJsonDate(string jsondate) | |
{ | |
if (String.IsNullOrEmpty(jsondate)) return null; | |
var match = Regex.Match(jsondate, @"^\/date\((\d*)([\+\-]?\d*)\)\/+$", RegexOptions.IgnoreCase); | |
if (!match.Success) throw new ArgumentException("No match."); | |
var ticks = Int64.Parse(match.Groups[1].Value); | |
var stroffset = match.Groups[2].Value; | |
DateTimeOffset offset; | |
DateTimeOffset.TryParseExact(stroffset, "zzz", DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out offset); | |
return new DateTimeOffset(new DateTime(1970, 1, 1, 0, 0, 0, 0).AddMilliseconds(ticks).Add(offset.Offset), offset.Offset); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment