1

How can I convert the below Javascript Date object Mon Apr 03 1978 01:00:00 GMT+0100 (GMT Daylight Time)

to

Date string /Date(260409600000)/.

My server returns as JSON string like "DateOfBirth":"\/Date(260409600000)\/" so we need to format the normal Date value to this type to process further.

var myDateObj;//Mon Apr 03 1978 01:00:00 GMT+0100 (GMT Daylight Time)

I tired using Date.parse(myDateObj), but gives only 260409600000

Also calling new Date(260409600000).toJSON() gives 1978-04-03T00:00:00.000Z and not /Date(260409600000)/

4
  • Uhm, so you want 260409600000 and you're getting 260409600000, what exactly is the problem ? Commented Feb 24, 2015 at 12:25
  • So the problem is that you have 260409600000, but you want Date(260409600000) string? Commented Feb 24, 2015 at 12:26
  • @dfsq, I am trying to get the same json formated string value by calling toJSON. But it returns different Commented Feb 24, 2015 at 12:29
  • Just create custom function or redefine toJSON method. Commented Feb 24, 2015 at 12:53

1 Answer 1

1

To convert 260409600000 into /Date(260409600000)/ you can use the + operator:

var output = "/Date(" + 260409600000 + ")/";

Your confusion probably comes from a wrong assumption that JSON has some kind of date/time data type. It doesn't: what you want to get is nothing but a custom string.

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.