-3

how can I convert "/Date(1479250800000)/" String to a C# Datetime?

Thanks in advance

3
  • that's not a javascript date string it came from your back end code in the first place Commented Jan 4, 2017 at 7:53
  • is this jsonresult Commented Jan 4, 2017 at 8:22
  • I know, but I need to store it in a string because this field can contain any type of data. In JS I would create the date using moment("/Date(1479250800000)/") but I don't know how to get the same result in C#. Big thanks Commented Jan 4, 2017 at 8:38

2 Answers 2

1

Assuming the value inside the brackets is number of ticks:

var datstr = "/Date(1479250800000)/";
long ticks = Convert.ToInt64(datstr.Substring(6, 13));
DateTime date = new DateTime(ticks);

There will be a difference between .Net and javascript tick value:

The JavaScript Date type's origin is the Unix epoch: midnight on 1 January 1970. The .NET DateTime type's origin is midnight on 1 January 0001. If by "ticks" you mean something like "milliseconds since the epoch", you can call ".getTime()

There are 621355968000000000 epoch ticks for javascript from Ist Jan 1900 to Ist Jan 1970. And here 10000 are the ticks per milliseconds.

quoted from here. You will need to correct for this. the last line would look like the following:

DateTime date = new DateTime(ticks * 10000 + 621355968000000000);
Sign up to request clarification or add additional context in comments.

1 Comment

Using this code I get a date ( {02/01/0001 17:05:25} ) but the original date was "Wed Nov 16 2016 00:00:00 GMT+0100". Could it be a problem with the date format? I need to use dd/mm/yyyy. Thanks in advance.
0

You'll get the string "/Date(1479250800000)/" as a date value in javascript if you are sending it from C#. But if you sending back the value to a date field in C# then the value will be deserialized as DateTime value. You don't need to convert it explicitly, just get it in a DateTime field.

1 Comment

You're right but I need to deserialize it as String because this field can contain any type of data. I just need to convert that string in a valid DateTime. Big thanks.

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.