0

I get a date from MySQL in the following format:

Sat Dec 10 1988 19:40:16 GMT+0200 (South Africa Standard Time)

I want to convert this to a Javascript date so that I can easily supply JSON timestamps for data readings (Probably in the format YYYY-MM-DD:HH:mm:ss). How can I do that?

5
  • 1
    which programming language are you using ? and what is the problem with current format ? Commented Dec 24, 2018 at 7:34
  • Possible duplicate of Converting a string to a date in JavaScript Commented Dec 24, 2018 at 7:34
  • I'm using Javascript, and I want to get it into a Javascript Date object. Commented Dec 24, 2018 at 7:35
  • You can't access db using plain javascript AFAIK. You should have some server side programming Commented Dec 24, 2018 at 7:35
  • I access MySQL using Javascript's MySQL library, but that is irrelevant. I want to store the String date format I posted into a Javascript Date object. Commented Dec 24, 2018 at 7:37

2 Answers 2

1

All you need to do is create a new Date with this string as the parameter for the constructor:

new Date('Sat Dec 10 1988 19:40:16 GMT+0200 (South Africa Standard Time)');

This works because it's a format compatible with RFC2822. See also: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Sign up to request clarification or add additional context in comments.

Comments

1

This will give you exactly what you want.

var d = new Date('Sat Dec 10 1988 19:40:16 GMT+0200 (South Africa Standard Time)');


formated_date  = [d.getMonth()+1,d.getDate(),d.getFullYear()].join('/')+' '+
                 [d.getHours(),d.getMinutes(),d.getSeconds()].join(':');

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.