使用带有语言环境的moment.js解析日期 [英] Parse date with moment.js with locales

查看:113
本文介绍了使用带有语言环境的moment.js解析日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 moment.js (使用语言环境).奇怪的是,只有某些日期会失败.

I am getting an "Invalid date" error when I translate my dates from Spanish to English with moment.js (with locales). The weird thing here, is that only fails with some dates.

我有一个日期列表,显然是相同的格式(在使用相同的库之前已对其进行了解析).然后,当我在更改moment.js语言环境后再次对其进行解析(将日期转换为所需的语言)时,我得到了:

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:

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方法(此处初始化了格式,因为在其他时候它可以获取其他值):

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/

也许我错过了一些东西,但是我还没有找到原因.有什么可以帮助我解决这个问题的吗?

Maybe I am missing something but I don't find the reason yet. Can any help me with this problem?

推荐答案

您可以在解析非英语输入时指定语言环境.您可以使用 moment(String, String, String) :

You can specify locale when parsing non-english input. You can use moment(String, String, String):

从版本 2.0.0 开始,可以将语言环境键作为第三个参数传递给moment()moment.utc().

As of version 2.0.0, a locale key can be passed as the third parameter to moment() and moment.utc().

您可以使用 locale() 函数来更改给定的语言环境瞬间对象( moment.locale() 全局更改语言环境).

You can use locale() function to change locale of a given moment object (while moment.locale() changes locale globally).

这里是一个工作示例:

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被认为是默认情况下的原因,因为它会立即使用英语语言环境解析字符串,而 Moment的解析器非常宽容. Mayo包含 May ,因此它被认为是有效的月份名称(使用宽恕模式).

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 contains May so it is considered a valid month name (using forgiving mode).

这篇关于使用带有语言环境的moment.js解析日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆