Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get formatted date time like 2009-05-29 21:55:57 using javascript?

when using new Date,I get something like follows:

Fri May 29 2009 22:39:02 GMT+0800 (China Standard Time)

but what I want is xxxx-xx-xx xx:xx:xx formatted time string

like image 511
omg Avatar asked May 29 '09 14:05

omg


People also ask

How do I format a Date in JavaScript?

The JavaScript toDateString() method returns the date portion of a date object in the form of a string using the following format: First three letters of the week day name. First three letters of the month name. Two digit day of the month, padded on the left a zero if necessary.

What is the time format in JavaScript?

The character "T" is used as the delimiter. HH:mm:ss. sss – is the time: hours, minutes, seconds and milliseconds. The optional 'Z' part denotes the time zone in the format +-hh:mm .

How do you display JavaScript datetime in 24 hour format?

Use the toLocaleString() method to change time formatting to 24 hours, e.g. date. toLocaleString('en-US', {hour12: false}) . The toLocaleString method returns a string that represents the date and time according to the provided locale and options parameters.

How do you get current Date and time in JavaScript?

Current Time in JavaScript Use the following script to get the current time using JavaScript in “H:i:s” format. var today = new Date(); var time = today. getHours() + ":" + today.


1 Answers

Although it doesn't pad to two characters in some of the cases, it does what I expect you want

function getFormattedDate() {     var date = new Date();     var str = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " +  date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();      return str; } 
like image 125
Jonathan Fingland Avatar answered Oct 12 '22 17:10

Jonathan Fingland