1

Is there a simple way to convert a date object for the current time into YYYY-MM-DD HH:MM:SS format? I've tried new Date().toLocaleString() but that gives me a human readable date.

What are my options using jquery or javascript in order to do this?

Joe Scotto
  • 10,936
  • 14
  • 66
  • 136
  • No, there's not, you'd do `date.getFullYear() + '-' + date.getMonth() + ...` etc – adeneo May 14 '17 at 15:45
  • You can use moment.js library. It has easy way to do this. otherwise, no easy way same as @adeneo said. – Tareq May 14 '17 at 15:47

1 Answers1

1

Unfortunately using plain JavaScript you don't have a really quick way to do that. If you are going to struggle with dates a lot, my suggestion is to include the moment.js library to your project, which will allow you do that really easily. In this way for example you can output the current date in the specified format:

moment().format('MMMM Do YYYY, h:mm:ss a'); // May 14th 2017, 4:50:42 pm

Here the link to the moment.js library:

https://momentjs.com/

quirimmo
  • 9,800
  • 3
  • 30
  • 45
  • 1
    Recommending a library should be a comment, not an answer. There are many such libraries, e.g. [*fecha.js*](https://www.npmjs.com/package/fecha). – RobG May 14 '17 at 23:27