javascript Globalize.js - 如何解析日期和时间而不仅仅是日期

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

Globalize.js - how to parse date and time rather than just date

javascriptdatetimeculturejavascript-globalize

提问by Kevin

Globalize.jsallows you to parse a date string based on the current culture applied

Globalize.js允许您根据应用的当前文化来解析日期字符串

var date = Globalize.parseDate("17/07/2013"); //Wed Jul 17 00:00:00 PDT 2013

What I would like to do is parse a DateTime. The javascript Date object handles this, I'm surprised the Globalize.jslibrary doesn't.

我想做的是解析一个 DateTime。javascript Date 对象处理这个,我很惊讶Globalize.js图书馆没有。

var date = new Date("07/17/2013 11:55 pm"); //Wed Jul 17 23:55:00 PDT 2013
var date = Globalize.parseDate("07/17/2013 11:55 pm"); //null

Am I missing something? I'm leaning towards parsing the time portion myself. Is there another library that extends Globalize.jsthat provides this kind of functionality? I've looked around but haven't found much.

我错过了什么吗?我倾向于自己解析时间部分。是否有另一个Globalize.js提供这种功能的扩展库?我环顾四周,但没有找到太多。

UPDATE w/ accepted answer

使用接受的答案更新

You can parse the date if you know the format that the date is in.

如果您知道日期的格式,则可以解析日期。

    var date = Globalize.parseDate("17/07/2013 11:55 pm", "MM/dd/yyyy hh:mm tt"); 
    //date = null

In my example the date will be null because it expects the time period to be in the format of a.mor p.m.. Once I changed that I was able to parse a datetime.

在我的示例中,日期将为空,因为它期望时间段的格式为a.mor p.m.。一旦我改变了我就能够解析日期时间。

   var date = Globalize.parseDate("17/07/2013 11:55 p.m.", "MM/dd/yyyy hh:mm tt"); 
   //date = Wed Jul 17 23:55:00 PDT 2013

Note: This is only applicable to the deprecatedGlobalize 0.x.

注意:这仅适用于已弃用的Globalize 0.x。

Note 2: Passing a hardcoded pattern is NOTan i18n recommendation.

注 2:传递硬编码模式不是i18n 建议。

采纳答案by Raciel R.

If you know the pattern you are using:

如果您知道正在使用的模式:

var date = Globalize.parseDate("07/17/2013 11:55 pm", "MM/dd/yyyy hh:mm tt");

If you don't know the pattern:

如果你不知道模式:

var date = Globalize.parseDate("07/17/2013 11:55 pm", Globalize.culture().calendar.patterns.d + " " + Globalize.culture().calendar.patterns.t)

The line above is assuming current culture, if you need it for other culture or if you haven't established the local culture by calling Globalize.culture("") then just specify the culture on culture().

上面一行是假设当前文化,如果您需要它用于其他文化,或者如果您还没有通过调用 Globalize.culture("") 建立本地文化,那么只需在culture() 上指定文化。

I just ran on this scenario a few minutes ago, and found this solution, the latest it is messy, I hope there is a cleaner way to do this.

几分钟前我刚刚在这个场景上运行,并找到了这个解决方案,最新的它很混乱,我希望有一个更干净的方法来做到这一点。

Note: This is only applicable to the deprecated Globalize 0.x.

注意:这仅适用于已弃用的 Globalize 0.x。

Note 2: Passing a hardcoded pattern is NOTan i18n recommendation.

注 2:传递硬编码模式不是i18n 建议。

回答by Rafael Xavier

Globalize 1.x is based on CLDR and has a different API now. Follow the new code to accomplish what you need:

Globalize 1.x 基于 CLDR,现在具有不同的 API。按照新代码完成您需要的操作:

Globalize("en").parseDate("5/14/2015, 9:47 AM", {skeleton: "yMdhm"});
// > Thu May 14 2015 09:47:00 GMT-0300 (BRT)

More information and examples.
More information on how to load CLDR.
Notes on how to use CLDR patterns

更多信息和示例
有关如何加载 CLDR 的更多信息
关于如何使用 CLDR 模式的注意事项

回答by dave

I would look into moment.js, with it you can do

我会研究moment.js,有了它你可以做

d = moment("17/07/2013 11:55 pm" , "DD/MM/YYYY HH:mm a"); // parsed as 11:55pm local time
d = d.toDate(); //get it as a native js date object

Unless you specify a timezone offset, parsing a string will create a date in the current users timezone.

除非您指定时区偏移量,否则解析字符串将在当前用户时区中创建日期。

回答by Rhys Stephens

The accepted answer means that all date time fields must have a full date and time. However, it is more than acceptable that some fields will have just a date. So I modified the globalize.culture.en-AU.js to add my custom pattern.

接受的答案意味着所有日期时间字段都必须具有完整的日期和时间。但是,某些字段只有一个日期是完全可以接受的。所以我修改了 globalize.culture.en-AU.js 以添加我的自定义模式。

Globalize.addCultureInfo( "en-AU", "default", {
    name: "en-AU",
    englishName: "English (Australia)",
    nativeName: "English (Australia)",
    numberFormat: {
        currency: {
            pattern: ["-$n","$n"]
        }
    },
    calendars: {
        standard: {
            firstDay: 1,
            patterns: {
                d: "d/MM/yyyy",
                D: "dddd, d MMMM yyyy",
                f: "dddd, d MMMM yyyy h:mm tt",
                F: "dddd, d MMMM yyyy h:mm:ss tt",
                M: "dd MMMM",
                Y: "MMMM yyyy",
                Z: "dd/MM/yyyy hh:mm:ss tt" // This is a custom one for our specifications.
            }
        }
    }
});

It's not the best solution by modifying it, but for my and most others purpose they won't be upgrading the Globalization.js from version 0.x to 1.xanytime soon.

这不是修改它的最佳解决方案,但对于我和大多数其他目的,他们不会很快将 Globalization.js 从 0.x 版升级到 1.x 版

This answer means you can have a date with time, and just a date field together in the same project and on the same page.

这个答案意味着您可以有一个带时间的日期,并且在同一项目和同一页面上只有一个日期字段。