Date对象为我的日期字符串返回

Date对象为我的日期字符串返回

本文介绍了Javascript Date对象为我的日期字符串返回'invalid date'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用这个字符串在JavaScript中创建一个Date对象 04/21/2014 12:00p
传递给构造函数( new Date('04 / 21/2014 12:00p')),它返回无效日期

I want to create a Date object in Javascript using this string 04/21/2014 12:00pWhen passed to the constructor (new Date('04/21/2014 12:00p')), it returns Invalid Date.

我已经看过其他的操作字符串的帖子,以满足有效的dateString的要求,但这不是我想要的。我想要Javascript来识别日期格式( m / dd / yy h:mmt )。在Java中,类似的东西很简单,我想像在Javascript中会有类似的方式。

I've seen other posts which manipulate the string in order to fulfill the requirements of a valid dateString, however that is not what I want. I want Javascript to recognize my date format (m/dd/yy h:mmt). In Java, something like that is simple, I imagine that there would be a similar way in Javascript.

我如何获取Date对象来识别我的格式? p>

How can I get the Date object to recognize my format?

推荐答案

只有当使用像:

var dt = moment("04/21/2014 12:00p","MM/DD/YYYY h:mma").toDate();

否则,你会有相当的字符串操作。另外,您必须向世界上使用d / m / y或其他格式而不是输入字符串的y / m / d格式化的用户进行交易。

Otherwise, you would have considerable string manipulation to do. Also you would have to account for users in parts of the world that use d/m/y or other formatting instead of the y/m/d formatting of your input string.

如果这个字符串是从某些后端进程发送的,您可以考虑将格式更改为标准的交换格式,如。例如2014-04-21T12:00:00

If this string is being sent from some back-end process, you might consider changing the format to a standard interchange format like ISO-8601 instead. Ex. "2014-04-21T12:00:00"

这篇关于Javascript Date对象为我的日期字符串返回'invalid date'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 23:36