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?

Cornel Verster
  • 1,664
  • 3
  • 27
  • 55
  • 1
    which programming language are you using ? and what is the problem with current format ? – Ravi Dec 24 '18 at 07:34
  • Possible duplicate of [Converting a string to a date in JavaScript](https://stackoverflow.com/questions/5619202/converting-a-string-to-a-date-in-javascript) – Herohtar Dec 24 '18 at 07:34
  • I'm using Javascript, and I want to get it into a Javascript Date object. – Cornel Verster Dec 24 '18 at 07:35
  • You can't access db using plain javascript AFAIK. You should have some server side programming – Ravi Dec 24 '18 at 07: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. – Cornel Verster Dec 24 '18 at 07:37

2 Answers2

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

Brad
  • 159,648
  • 54
  • 349
  • 530
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(':');
Deepak Preman
  • 113
  • 1
  • 11