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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8559147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:54:56  来源:igfitidea点击:

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

node.js

提问by user482594

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

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

回答by alessioalex

You do that as you would in a browser:

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

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

回答by Aikansh Mann

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

回答by harco gijsbers

NodeJS (and newer browsers) have a nice shortcutto 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.

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

回答by heySushil

To create a new Date object in node.js, or JavaScriptin 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 Dateor using it as a constructor; unlike other JavaScript object types, Date objects have no literal syntax

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

回答by Ravenex

You would use the javascript date object:

您将使用 javascript 日期对象:

MDN documentation for the Date object

Date 对象的 MDN 文档

var d = new Date();

回答by Ashish

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

Node.js 是一个基于 V8 的服务器端 JS 平台,它是 chrome java-script 运行时。

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

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

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

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

回答by Rafael Xavier

GMT -03:00 Example

格林威治标准时间 -03:00 示例

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