What about:
new Date().toString().replace(/T/, ':').replace(/\.\w*/, '');
Returns for me:
2014-07-14:13:41:23
But the more safe way is using Date
class methods which works in javascript (browser) and node.js:
var date = new Date();
function getDateStringCustom(oDate) {
var sDate;
if (oDate instanceof Date) {
sDate = oDate.getYear() + 1900
+ ':'
+ ((oDate.getMonth() + 1 < 10) ? '0' + (oDate.getMonth() + 1) : oDate.getMonth() + 1)
+ ':' + oDate.getDate()
+ ':' + oDate.getHours()
+ ':' + ((oDate.getMinutes() < 10) ? '0' + (oDate.getMinutes()) : oDate.getMinutes())
+ ':' + ((oDate.getSeconds() < 10) ? '0' + (oDate.getSeconds()) : oDate.getSeconds());
} else {
throw new Error("oDate is not an instance of Date");
}
return sDate;
}
alert(getDateStringCustom(date));
Returns in node.js:
/usr/local/bin/node date.js 2014:07:14:16:13:10
And in Firebug:
2014:07:14:16:14:31