12

I need the current system datetime in the format "yyyy-mm-dd:hh:mm:ss".

https://stackoverflow.com/a/19079030/2663388 helped a lot.

new Date().toJSON() is showing "2014-07-14T13:41:23.521Z"

Can someone help me to extract "yyyy-mm-dd:hh:mm:ss" from "2014-07-14T13:41:23.521Z"?

Community
  • 1
  • 1
anand
  • 187
  • 1
  • 2
  • 13

8 Answers8

11

Seems that there is no good way to do it with original code unless using Regex. There are some modules such as Moment.js though.

If you are using npm:

npm install moment --save

Then in your code:

var moment = require('moment');
moment().format('yyyy-mm-dd:hh:mm:ss');

That may be much easier to understand.

FreeTymeKiyan
  • 309
  • 1
  • 6
  • When I run this on a Node.js server it gives me wrong date/time in a wrong format: `2020-00-Mo-12:00:00` while running Date.toLocaleString() has the correct date/time information. Do you have an idea what might be causing this? – Andreas Jul 13 '20 at 07:14
  • 3
    `mm` is for minutes, not months. Also `hh` is 12-hour time. https://momentjs.com/docs/#/parsing/string-format/ `YYYY-MM-DD hh:mm:ss` – Alex P. Oct 21 '20 at 10:28
  • Given this was correct in 2014. moment is deprecated. moment/luxon is the latest version (with core-js to polyfill for older node and browsers) – TamusJRoyce Jul 28 '22 at 15:28
  • As of 2023, the above format specifier gives something completely unexpected, for example the `dd`-part will print the name of the day of the week like `Mo`, `Tu` etc. https://momentjscom.readthedocs.io/en/latest/moment/04-displaying/01-format/ – Andrey Tyukin Feb 28 '23 at 18:33
4

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

victorkt
  • 13,992
  • 9
  • 52
  • 51
Bernhard
  • 4,855
  • 5
  • 39
  • 70
4

Install moment using

npm install moment --save

And in your code import the moment like this.

var moment = require('moment')
var created = moment().format('YYYY-MM-DD hh:mm:ss')
Lijomon C John
  • 131
  • 2
  • 10
3

Though other answers are helpful I found the following code is working for me.

var d = new Date();
console.log(d.toJSON().slice(0,19).replace('T',':'));

The output on console is: 2014-07-15:06:10:16. I am using Node.js Express on Ubuntu.

Andreas
  • 5,393
  • 9
  • 44
  • 53
anand
  • 187
  • 1
  • 2
  • 13
2

Cleaner version of @Bernhard code using padStart and without deprecated getYear

function getCurrentDateTimeString() {
    const date = new Date();
    return date.getFullYear() + '-' +
        (date.getMonth() + 1).toString().padStart(2, '0') + '-' +
        date.getDate().toString().padStart(2, '0') + ':' +
        date.getHours().toString().padStart(2, '0') + ':' +
        date.getMinutes().toString().padStart(2, '0') + ':' +
        date.getSeconds().toString().padStart(2, '0');
}

console.log(getCurrentDateTimeString());
Alex P.
  • 3,697
  • 9
  • 45
  • 110
1

You can do it using toLocaleString() method in node.js

let date = new Date('2014-07-14T13:41:23.521Z');
console.log(date.toLocaleString('en-US')); 

//Converting current date time without timezone
date = new Date()
console.log(date.toLocaleString('en-US')); 
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
0

Explicity call each part of the Date:

function formatDate(date) {
    return [date.getFullYear(), date.getMonth() + 1, date.getDate()].join('-') + ':' +
        [date.getHours(), date.getMinutes(), date.getSeconds()].join(':');
}

function formatDatePart(part) {
    return part < 10 ? ("0" + part) : part;
}

You can see an example on this fiddle.

lante
  • 7,192
  • 4
  • 37
  • 57
0

Correct way is this

var moment = require('moment');
moment().format('Y-M-D H:m:s');
aman
  • 125
  • 1
  • 11