I'm not familiar with time operations in javascript.
I tried new Date();
which gives the result in wrong format:
Thu Dec 24 2009 14:24:06 GMT+0800
How to get the time in format of 2009-12-24 14:20:57
?
In JavaScript, we can easily get the current date or time by using the new Date() object. By default, it uses our browser's time zone and displays the date as a full text string, such as "Fri Jun 17 2022 10:54:59 GMT+0100 (British Summer Time)" that contains the current date, time, and time zone.
The string format should be: YYYY-MM-DDTHH:mm:ss. sssZ , where: YYYY-MM-DD – is the date: year-month-day. The character "T" is used as the delimiter.
There is no cross browser Date.format() method currently. Some toolkits like Ext have one but not all (I'm pretty sure jQuery does not). If you need flexibility, you can find several such methods available on the web. If you expect to always use the same format then:
var now = new Date();
var pretty = [
now.getFullYear(),
'-',
now.getMonth() + 1,
'-',
now.getDate(),
' ',
now.getHours(),
':',
now.getMinutes(),
':',
now.getSeconds()
].join('');
<script type="text/javascript">
function formatDate(d){
function pad(n){return n<10 ? '0'+n : n}
return [d.getUTCFullYear(),'-',
pad(d.getUTCMonth()+1),'-',
pad(d.getUTCDate()),' ',
pad(d.getUTCHours()),':',
pad(d.getUTCMinutes()),':',
pad(d.getUTCSeconds())].join("");
}
var d = new Date();
var formattedDate = formatDate(d);
</script
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