在 JavaScript 中将字符串转换为日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14197175/
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-08-24 15:58:53  来源:igfitidea点击:

Convert string to date in JavaScript

javascriptdate

提问by Bartosz

I have a string looking like so:

我有一个看起来像这样的字符串:

"2013/1/16"

Is there any way I could quickly convert it into date object in javascript, something like:

有什么方法可以快速将其转换为 javascript 中的日期对象,例如:

convertToDateTime("2013/1/16", "yyyy/MM/dd")

回答by Cerbrus

It's pretty simple:

这很简单:

var myDate = new Date("2013/1/16");

That should do the trick.
Have a look at the documentation for the Date object

这应该够了吧。
查看Date 对象文档