How can I get date in application run by node.js?


Question

Do I have to manually run date command using child_process and fetch the result from it to get the date? Is there any other way using node?

1
32
1/22/2013 9:12:55 PM

Accepted Answer

You do that as you would in a browser:

    var datetime = new Date();
    console.log(datetime);

60
1/6/2018 3:10:24 AM

To create a new Date object in node.js, or JavaScript in general, just call it’s initializer

var d = new Date();

var d = new Date(dateString);

var d = new Date(jsonDate);

var d = new Date(year, month, day);

var d = new Date(year, month, day, hour, minute, second, millisecond);

Remember that Date objects can only be instantiated by calling Date or using it as a constructor; unlike other JavaScript object types, Date objects have no literal syntax


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon