Javascript 在Javascript中解析自定义格式的“日期和时间”字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28002261/
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
Parse 'Date & Time' string in Javascript which are of custom format
提问by yuva
I have to parse a date and time string of format "2015-01-16 22:15:00". I want to parse this into JavaScript Date Object. Any help on this?
我必须解析格式为“2015-01-16 22:15:00”的日期和时间字符串。我想将其解析为 JavaScript 日期对象。有什么帮助吗?
I tried some jquery plugins, moment.js, date.js, xdate.js. Still no luck.
我尝试了一些 jquery 插件,moment.js、date.js、xdate.js。仍然没有运气。
回答by moises.vilar
With moment.js you can create a moment object using the String+Format constructor:
使用 moment.js,您可以使用String+Format 构造函数创建一个 moment 对象:
var momentDate = moment('2015-01-16 22:15:00', 'YYYY-MM-DD HH:mm:ss');
Then, you can convert it to JavaScript Date Object using toDate() method:
然后,您可以使用toDate() 方法将其转换为 JavaScript 日期对象:
var jsDate = momentDate.toDate();
回答by yuva
A better solution, I am now using date.js - https://code.google.com/p/datejs/
更好的解决方案,我现在使用 date.js - https://code.google.com/p/datejs/
I included the script in my html page as this -
我将脚本包含在我的 html 页面中,如下所示 -
<script type="text/javascript" src="path/to/date.js"></script>
Then I simply parsed the date string "2015-01-16 22:15:00" with specifying the format as,
然后我简单地解析日期字符串“2015-01-16 22:15:00”并将格式指定为,
var dateString = "2015-01-16 22:15:00";
var date = Date.parse(dateString, "yyyy-MM-dd HH:mm:ss");
回答by dusky
new Date("2015-01-16T22:15:00")
See Date.parse().
请参阅Date.parse()。
The string must be in the ISO-8601 format. If you want to parse other formats use moment.js.
该字符串必须采用 ISO-8601 格式。如果你想解析其他格式,请使用moment.js。
moment("2015-01-16 22:15:00").toDate();
回答by yuva
I was trying to use moment.js guys. But since I was having this error, "ReferenceError: moment is not defined", I had to skip it for now. I am using an temporary workaround for now.
我试图使用moment.js 伙计们。但是由于我遇到了这个错误,“ReferenceError:moment is not defined”,我现在不得不跳过它。我现在正在使用临时解决方法。
function parseDate(dateString) {
var dateTime = dateString.split(" ");
var dateOnly = dateTime[0];
var timeOnly = dateTime[1];
var temp = dateOnly + "T" + timeOnly;
return new Date(temp);
}
回答by JSub
If you are sure it's in the desired format and don't need to error check, you can parse it manually using split (and optionally replace). I needed to do something similar in my project (MM/DD/YYYY HH:mm:ss:sss) and modified my solution to fit your format. Notice the subtraction of 1 in the month.
如果您确定它是所需的格式并且不需要错误检查,您可以使用 split(和可选的替换)手动解析它。我需要在我的项目 (MM/DD/YYYY HH:mm:ss:sss) 中做类似的事情并修改我的解决方案以适应您的格式。 注意当月减 1。
var str = "2015-01-16 22:15:00";
//Replace dashes and spaces with : and then split on :
var strDate = str.replace(/-/g,":").replace(/ /g,":").split(":");
var aDate = new Date(strDate[0], strDate[1]-1, strDate[2], strDate[3], strDate[4], strDate[5]) ;

