如何在由 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
How can I get date in application run by 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
回答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
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();

![node.js Mongoose:CastError:Cast to ObjectId 在路径“_id”处的值“[object Object]”失败](/res/img/loading.gif)