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
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.
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 .
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.
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.
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With