2

I'm consuming a REST service that is returning date's in the following format:

{
   "uploadedOn" : "\/Date(1416421811000-0800)\/"
}

Trying to figure out how i can convert this to c# DateTime. Tested a few off the wall ideas that got close but no dice. I thought possibly the -0800 was the GMT offset and parsing the value excluding that but that did not work. Thanks.

2
  • 1
    Do you have any indication of what date the value in parens corresponds to? Commented Dec 1, 2014 at 19:58
  • Yep. This is the value returned by the same service as xml which is easily parsed. 2014-11-19T10:30:11 Commented Dec 1, 2014 at 20:02

1 Answer 1

4

What you're getting back looks like a Java timestamp with an offset of -8 hours, which will require some work to parse (unfortunately none of the native parsing methods will work out of the box).

Something like this (adjust as needed) should work for you:

public class Program
{

    public static void Main()
    {   
        var offsetParsedDate = GetDateTimeOffset("1416421811000-0800");

        Console.WriteLine(offsetParsedDate);

    }

    static DateTimeOffset GetDateTimeOffset(string inDate)
    {
        string delimiter = "-";

        if(inDate.IndexOf("+") != -1)
        {
            delimiter = "+";
        }

        string[] dateParts = inDate.Split(new string[] { delimiter }, 2, System.StringSplitOptions.None);

        DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Unspecified);

        var parsedDate = epoch.AddMilliseconds(Convert.ToDouble(dateParts[0]));

        var offset = TimeSpan.ParseExact(dateParts[1], "hhmm", null, delimiter == "-" ? System.Globalization.TimeSpanStyles.AssumeNegative : System.Globalization.TimeSpanStyles.None);

        return new DateTimeOffset(parsedDate, offset);      
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.