2

I'm sending the following JSON as part of an Ajax request, where Param #1 is a Date string.

data : JSON.stringify({'localCreatedDate' : new Date(), 
                       'localUserAgent' : navigator.userAgent
                       )});

However, the date I need is the original literal JS string which looks like:

Sun Sep 08 2019 17:33:32 GMT-0400 (Eastern Daylight Time)

But JSON.stringify() is making it:

2019-09-08T21:33:32.016Z

0

2 Answers 2

4

Rather than send the Date object directly, you could call Date#toString() to ensure that localCreatedDate is sent to the server as a string, in the required format:

data : JSON.stringify({
    'localCreatedDate' : new Date().toString(), 
    'localUserAgent' : navigator.userAgent
});
Sign up to request clarification or add additional context in comments.

Comments

1

using JSON.stringify automatically calls the .toJSON method of the Date object. What you should do to get your desired result is to cast the Date object to a string or just call the .toString method on the date object

String

data : JSON.stringify({
            'localCreatedDate' : String(new Date()), 
            'localUserAgent' : navigator.userAgent
       )});

toString

data : JSON.stringify({
            'localCreatedDate' : (new Date()).toString(), 
            'localUserAgent' : navigator.userAgent
       )});

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.