0

I am reading an API documentation that says I have to pass current time to it in following format:

{
   "date": "\/Date(1508262132936)\/"
}

And the one-line explanation of this format says: "Date in UTC format". I tried passing DateTime.Now; DateTime.Now.UtcNow; DateTime.Now.ToUniversalTime(); but all of those output BadRequest reply. What is the correct format here?

astralmaster
  • 2,344
  • 11
  • 50
  • 84
  • 1
    To be clear, you're calling an **external** API, and they said what format to send it? – krillgar Oct 18 '17 at 11:42
  • I am calling an external API (HTTP REST), the only explanation I have is "Date in UTC format" (what they told me) – astralmaster Oct 18 '17 at 11:43
  • 2
    That format is far from being related to UTC. – Camilo Terevinto Oct 18 '17 at 11:45
  • 3
    The format they are asking for is sometimes known as "Microsoft format". How to generate it depends on which JSON library you are using, e.g. https://www.newtonsoft.com/json/help/html/DatesInJSON.htm. I suspect Microsoft's `JavaScriptSerializer` class generates that format by default, hence the name "Microsoft format". – Joe Oct 18 '17 at 11:46
  • I am using RestSharp's built-in JSON serializer – astralmaster Oct 18 '17 at 11:47
  • @astralmaster - I bet googling for "RestSharp json date format" will give you some hints. – Joe Oct 18 '17 at 11:49
  • @Joe that's the first thing I tried but they all output different formats including the ones I posted in my question – astralmaster Oct 18 '17 at 11:51
  • @astralmaster - you might consider using the Newtonsoft library, which is very popular. Otherwise, you can generate milliseconds since 01/01/1970 00:00, and manually decorate it. – Joe Oct 18 '17 at 11:52
  • 1
    You can check how [NewtonSoft.Json does it](https://github.com/JamesNK/Newtonsoft.Json/blob/92d170a0783bc962f52faf02acfcecbe21d3b683/Src/Newtonsoft.Json/Utilities/DateTimeUtils.cs#L646) – Camilo Terevinto Oct 18 '17 at 11:56
  • @Joe Am I right to assume that the API engine uses some 3rd party library as well? Since this doesn't seem to be a 'standard' ? – astralmaster Oct 18 '17 at 11:57
  • sadly I only have javascript code that goes the other way ... I think Joe and Camilo Terevinto have the right answer. – kpollock Oct 18 '17 at 12:18

1 Answers1

2

Looks like a Unix epoch time, use the ToUnixTimeSeconds method on DateTimeOffset:

var date = DateTime.UtcNow;
new DateTimeOffset(date).ToUnixTimeSeconds()
Kevin Smith
  • 13,746
  • 4
  • 52
  • 77