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

node.js

node.js Problem Overview


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?

node.js Solutions


Solution 1 - node.js

You do that as you would in a browser:

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

Solution 2 - node.js

    var datetime = new Date();
    console.log(datetime.toISOString().slice(0,10));

Solution 3 - node.js

NodeJS (and newer browsers) have a nice shortcut to get the current time in milliseconds.

var timeInMss = Date.now()

Which has a performance boost compared with

var timeInMss = new Date().getTime()

Because you do not need to create a new object.

Solution 4 - node.js

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

Solution 5 - node.js

You would use the javascript date object:

[MDN documentation for the Date object][1]

var d = new Date();

[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date "MDN documentation for the Date object"

Solution 6 - node.js

GMT -03:00 Example

new Date(new Date()-3600*1000*3).toISOString();

Solution 7 - node.js

Node.js is a server side JS platform build on V8 which is chrome java-script runtime.

It leverages the use of java-script on servers too.

You can use JS Date() function or Date class.

Solution 8 - node.js

I had an experience about dates on NodeJS with MongoDB database and I want to share it on this page as well,

I was setting a default value to a date field on Mongoose Schema as below code :

{ type: Date, default: new Date() }

but after one day, I've realized that default value is still getting date belong to yesterday. it needs to be written as below :

{ type: Date, default: () => { return new Date() } }

Otherwise it always getting process start date , thanks to @lineus's comment on the page below :

https://github.com/Automattic/mongoose/issues/3675

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser482594View Question on Stackoverflow
Solution 1 - node.jsalessioalexView Answer on Stackoverflow
Solution 2 - node.jsAikansh MannView Answer on Stackoverflow
Solution 3 - node.jsharco gijsbersView Answer on Stackoverflow
Solution 4 - node.jsheySushilView Answer on Stackoverflow
Solution 5 - node.jsRavenexView Answer on Stackoverflow
Solution 6 - node.jsRafael XavierView Answer on Stackoverflow
Solution 7 - node.jsAshishView Answer on Stackoverflow
Solution 8 - node.jsBulent BalciView Answer on Stackoverflow