如何在node.js运行的应用程序中获取日期?

时间:2021-09-23 23:08:30

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?

我是否必须使用child_process手动运行date命令并从中获取结果以获取日期?有没有其他方式使用节点?

5 个解决方案

#1


48  

You do that as you would in a browser:

您可以像在浏览器中那样执行此操作:

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

#2


6  

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

要在node.js或JavaScript中创建一个新的Date对象,只需调用它的初始化程序即可

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

请记住,Date对象只能通过调用Date或将其用作构造函数来实例化;与其他JavaScript对象类型不同,Date对象没有文字语法

#3


2  

You would use the javascript date object:

您将使用javascript日期对象:

MDN documentation for the Date object

Date对象的MDN文档

var d = new Date();

#4


2  

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

NodeJS(和更新的浏览器)有一个很好的快捷方式来获取当前时间(以毫秒为单位)。

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.

因为您不需要创建新对象。

#5


1  

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

Node.js是在V8上构建的服务器端JS平台,它是chrome java脚本运行时。

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

它也利用了服务器上的java脚本。

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

您可以使用JS Date()函数或Date类。

#1


48  

You do that as you would in a browser:

您可以像在浏览器中那样执行此操作:

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

#2


6  

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

要在node.js或JavaScript中创建一个新的Date对象,只需调用它的初始化程序即可

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

请记住,Date对象只能通过调用Date或将其用作构造函数来实例化;与其他JavaScript对象类型不同,Date对象没有文字语法

#3


2  

You would use the javascript date object:

您将使用javascript日期对象:

MDN documentation for the Date object

Date对象的MDN文档

var d = new Date();

#4


2  

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

NodeJS(和更新的浏览器)有一个很好的快捷方式来获取当前时间(以毫秒为单位)。

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.

因为您不需要创建新对象。

#5


1  

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

Node.js是在V8上构建的服务器端JS平台,它是chrome java脚本运行时。

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

它也利用了服务器上的java脚本。

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

您可以使用JS Date()函数或Date类。