如何将 dd/mm/yyyy 字符串转换为 JavaScript 日期对象?

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

How to convert dd/mm/yyyy string into JavaScript Date object?

javascriptdate

提问by Raja Manickam

How to convert a date in format 23/10/2015into a JavaScript Date format:

如何将格式中的日期转换23/10/2015为 JavaScript 日期格式:

Fri Oct 23 2015 15:24:53 GMT+0530 (India Standard Time)

回答by Yeldar Kurmangaliyev

MM/DD/YYYY format

MM/DD/YYYY 格式

If you have the MM/DD/YYYYformat which is default for JavaScript, you can simply pass your string to Date(string)constructor. It will parse it for you.

如果你有JavaScript 的默认MM/DD/YYYY格式,你可以简单地将你的字符串传递给构造函数。它会为你解析它。 Date(string)

var dateString = "10/23/2015"; // Oct 23

var dateObject = new Date(dateString);

document.body.innerHTML = dateObject.toString();

DD/MM/YYYY format - manually

DD/MM/YYYY 格式 - 手动

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):

如果您使用这种格式,那么您可以拆分日期以分别获取日、月和年,然后在另一个构造函数中使用它 - Date(year, month, day)

var dateString = "23/10/2015"; // Oct 23

var dateParts = dateString.split("/");

// month is 0-based, that's why we need dataParts[1] - 1
var dateObject = new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0]); 

document.body.innerHTML = dateObject.toString();

For more information, you can read article about Dateat Mozilla Developer Network.

欲了解更多信息,您可以阅读有关文章DateMozilla开发者网络

DD/MM/YYYY - using moment.jslibrary

DD/MM/YYYY - 使用moment.js

Alternatively, you can use moment.jslibrary, which is probably the most popular library to parse and operate with date and time in JavaScript:

或者,您可以使用moment.jslibrary,它可能是在 JavaScript 中解析和操作日期和时间的最流行的库:

var dateString = "23/10/2015"; // Oct 23

var dateMomentObject = moment(dateString, "DD/MM/YYYY"); // 1st argument - string, 2nd argument - format
var dateObject = dateMomentObject.toDate(); // convert moment.js object to Date object

document.body.innerHTML = dateObject.toString();
<script src="https://momentjs.com/downloads/moment.min.js"></script>

In all three examples dateObjectvariable contains an object of type Date, which represents a moment in time and can be further converted to any string format.

在所有三个示例中,dateObject变量都包含一个类型为 的对象Date,它代表一个时刻,可以进一步转换为任何字符串格式。

回答by Mustkeem K

You can use toLocaleString(). This is a javascript method.

您可以使用 toLocaleString()。这是一个javascript方法。

var event = new Date("01/02/1993");

var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };

console.log(event.toLocaleString('en', options));

// expected output: "Saturday, January 2, 1993"

Almost all formats supported. Have look on this link for more details.

几乎支持所有格式。查看此链接以获取更多详细信息。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString

回答by Sanjeev Kumar

var date = new Date("enter your  date");//2018-01-17 14:58:29.013

Just one line is enough no need to do any kind of split, join, etc.:

只有一条线路是足够的,不需要做任何形式的splitjoin等:

$scope.ssdate=date.toLocaleDateString();//  mm/dd/yyyy format