[英]Parse date with moment.js with locales
I am getting an "Invalid date" error when I translate my dates from Spanish to English with moment.js (with locales). 当我使用moment.js (使用语言环境)将日期从西班牙语翻译为英语时,收到“无效日期”错误。 The weird thing here, is that only fails with some dates.
奇怪的是,只有某些日期会失败。
I have a list of dates, apparently of the same format (they were parsed before using the same library). 我有一个日期列表,显然是相同的格式(在使用相同的库之前已对其进行了解析)。 Then when I parsed it again after change the moment.js locale (To translate my dates to the desired language) I get this:
然后,当我在更改moment.js语言环境后再次对其进行解析(将日期转换为所需的语言)时,我得到了:
Enero 13º 2017, 6:00:02 Am --> Invalid date
Abril 17º 2017, 7:36:03 Pm --> Invalid date
Abril 17º 2017, 6:00:01 Am --> Invalid date
Mayo 12º 2017, 2:04:19 Pm --> May 12th 2017, 2:04:19 Pm
Abril 17º 2017, 11:47:17 Pm --> Invalid date
Parse Method (format is initialized here because in other moments it can get other values): 解析方法(此处初始化了格式,因为在其他时候它可以获取其他值):
format = 'MMMM Do YYYY, h:mm:ss a';
$(".videoDate").each(function(){
var _text = $(this).text();//Extract initial date
var _date = moment(_text, format).format('MMMM Do YYYY, h:mm:ss a');//format
$(this).text(_date);//new date setting
});
http://jsfiddle.net/gr1zdtag/ http://jsfiddle.net/gr1zdtag/
Maybe I am missing something but I don't find the reason yet. 也许我错过了一些东西,但是我还没有找到原因。 Can any help me with this problem?
有什么可以帮助我解决这个问题的吗?
You can specify locale when parsing non-english input. 您可以在解析非英语输入时指定语言环境。 You can use
moment(String, String, String)
: 您可以使用
moment(String, String, String)
:
As of version 2.0.0 , a locale key can be passed as the third parameter to
moment()
andmoment.utc()
.从2.0.0版开始,可以将语言环境键作为第三个参数传递给
moment()
和moment.utc()
。
You can use locale()
function to change locale of a given moment object (while moment.locale()
changes locale globally). 您可以使用
locale()
函数更改给定矩对象的区域设置(而moment.locale()
全局更改区域设置)。
Here a working sample: 这是一个工作示例:
var format = 'MMMM Do YYYY, h:mm:ss a'; $(".videoDate").each(function(){ var _text = $(this).text();//Extract initial date //Parse in spanish and convert it in english var _date = moment(_text, format, 'es') .locale('en') .format('MMMM Do YYYY, h:mm:ss a');//format $(this).text(_date);//new date setting });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.3/moment-with-locales.min.js"></script> <ul> <li class="videoDate">Enero 13º 2017, 6:00:02 Am</li> <li class="videoDate">Abril 17º 2017, 7:36:03 Pm</li> <li class="videoDate">Abril 17º 2017, 6:00:01 Am</li> <li class="videoDate">Mayo 12º 2017, 2:04:19 Pm</li> <li class="videoDate">Abril 17º 2017, 11:47:1</li> </ul>
Mayo 12º 2017, 2:04:19 Pm
is recognized beacuse by default moment parses strings using english locale and Moment's parser is very forgiving . Mayo 12º 2017, 2:04:19 Pm
因为默认情况下会在使用英语语言环境解析字符串的情况下识别Mayo 12º 2017, 2:04:19 Pm
,因此Moment的解析器非常宽容 。 Mayo
contains May
so it is considered a valid month name (using forgiving mode ). Mayo
包含May
因此它被认为是有效的月份名称(使用宽恕模式 )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.