如何将dd / mm / yyyy字符串转换为JavaScript Date对象? [英] How to convert dd/mm/yyyy string into JavaScript Date object?
问题描述
如何将格式 23/10/2015
中的日期转换为
JavaScript日期格式:
How to convert a date in format 23/10/2015
into a
JavaScript Date format:
Fri Oct 23 2015 15:24:53 GMT+0530 (India Standard Time)
推荐答案
MM / DD / YYYY格式
如果你有 / DD / YYYY
格式,这是 JavaScript 的默认格式,您只需将字符串传递到 Date(string)
构造函数。它会为你解析。
MM/DD/YYYY format
If you have the MM/DD/YYYY
format which is default for JavaScript, you can simply pass your string to Date(string)
constructor. It will parse it for you.
var dateString = "10/23/2015"; // Oct 23
var dateObject = new Date(dateString);
document.body.innerHTML = dateObject.toString();
如果您使用此格式,则可以拆分日期以获取日期,年份和年份,然后在另一个构造函数中使用它 - 日期(年,月,日)
:
If you work with this format, then you can split the date in order to get day, month and year separately and then use it in another constructor - Date(year, month, day)
:
var dateString = "23/10/2015"; // Oct 23
var dateParts = dateString.split("/");
var dateObject = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]); // month is 0-based
document.body.innerHTML = dateObject.toString();
有关更多信息,您可以在 Date 的文章Web / JavaScript / Reference / Global_Objects / Date#构造函数> Mozilla开发人员网络。
For more information, you can read article about Date
at Mozilla Developer Network.
这篇关于如何将dd / mm / yyyy字符串转换为JavaScript Date对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!