0

I have an object containing a javacsript date. Something like this:

var obj = {
  startTime: new Date()
  ....
}

When Angularjs converts the object to JSON (for example, when sending it over $http), it converts the date to a string such as:

2015-12-20T15:35:15.853Z

I would like to have a timestamp instead of this string. What is the best way to achieve this?

1
  • What do you mean by timestamp? JSON is a string-based notation. It must serialize it to a string in some format. Do you want a different format? ISO 8601 is the default. Commented Dec 20, 2015 at 15:48

1 Answer 1

3

Use the getTime() method to get a unix timestamp from a Date object:

var obj = {
    startTime: (new Date()).getTime()
}
Sign up to request clarification or add additional context in comments.

2 Comments

changing the way the data is saved is not an option, i need to change they way the data is converted to JSON
so convert to timestamp just before sending it? obj.startTime.getTime()

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.