jquery/javascript 将日期字符串转换为日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2974496/
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
jquery/javascript convert date string to date
提问by sadmicrowave
I have a date string "Sunday, February 28, 2010" that I would like to convert to a js date object formatted @ MM/DD/YYYY but don't know how. Any suggestions?
我有一个日期字符串“Sunday, February 28, 2010”,我想将其转换为格式为@ MM/DD/YYYY 的js 日期对象,但不知道如何转换。有什么建议?
采纳答案by Segfault
回答by Jono Wilkinson
If you're running with jQuery you can use the datepicker UI library's parseDate function to convert your string to a date:
如果您使用 jQuery,您可以使用 datepicker UI 库的 parseDate 函数将您的字符串转换为日期:
var d = $.datepicker.parseDate("DD, MM dd, yy", "Sunday, February 28, 2010");
and then follow it up with the formatDate method to get it to the string format you want
然后用 formatDate 方法跟进它以得到你想要的字符串格式
var datestrInNewFormat = $.datepicker.formatDate( "mm/dd/yy", d);
If you're not running with jQuery of course its probably not the best plan given you'd need jQuery core as well as the datepicker UI module... best to go with the suggestion from Segfault above to use date.js.
如果你没有使用 jQuery,当然它可能不是最好的计划,因为你需要 jQuery 核心以及 datepicker UI 模块......最好按照上面 Segfault 的建议使用 date.js。
HTH
HTH
回答by RoToRa
var stringDate = "Sunday, February 28, 2010";
var months = ["January", "February", "March"]; // You add the rest :-)
var m = /(\w+) (\d+), (\d+)/.exec(stringDate);
var date = new Date(+m[3], months.indexOf(m[1]), +m[2]);
The indexOfmethod on arrays is only supported on newer browsers (i.e. not IE). You'll need to do the searching yourself or use one of the many libraries that provide the same functionality.
indexOf数组上的方法仅在较新的浏览器(即不支持 IE)上受支持。您需要自己进行搜索或使用提供相同功能的众多库之一。
Also the code is lacking any error checking which should be added. (String not matching the regular expression, non existent months, etc.)
此外,该代码缺少应添加的任何错误检查。(字符串不匹配正则表达式,不存在的月份等)
回答by stallingOne
If you only need it once, it's overkill to load a plugin.
如果您只需要它一次,那么加载插件就太过分了。
For a date "dd/mm/yyyy", this works for me:
对于日期“dd/mm/yyyy”,这对我有用:
new Date(d.date.substring(6, 10),d.date.substring(3, 5)-1,d.date.substring(0, 2));
Just invert month and day for mm/dd/yyyy, the syntax is
new Date(y,m,d)
只需反转 mm/dd/yyyy 的月和日,语法是
new Date(y,m,d)
回答by MUlferts
I used the javascript date funtion toLocaleDateString to get
我使用 javascript 日期函数 toLocaleDateString 来获取
var Today = new Date();
var r = Today.toLocaleDateString();
The result of r will be
r 的结果将是
11/29/2016
More info at: http://www.w3schools.com/jsref/jsref_tolocaledatestring.asp
更多信息请访问:http: //www.w3schools.com/jsref/jsref_tolocaledatestring.asp
回答by Vijay Maheriya
Use moment js for any date operation.
使用 moment js 进行任何日期操作。
console.log(moment("Sunday, February 28, 2010").format('MM/DD/YYYY'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

