扩展JavaScript的Date.parse以允许DD / MM / YYYY(非美国格式的日期)? [英] Extending JavaScript's Date.parse to allow for DD/MM/YYYY (non-US formatted dates)?

查看:70
本文介绍了扩展JavaScript的Date.parse以允许DD / MM / YYYY(非美国格式的日期)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经提出了这个解决方案来扩展JavaScript的 Date.parse 函数,以允许格式化为DD / MM / YYYY的日期(而不是美国标准[和默认] MM / DD / YYYY):

I've come up with this solution to extending JavaScript's Date.parse function to allow for dates formatted in DD/MM/YYYY (rather then the American standard [and default] MM/DD/YYYY):

(function() {
    var fDateParse = Date.parse;

    Date.parse = function(sDateString) {
        var a_sLanguage = ['en','en-us'],
            a_sMatches = null,
            sCurrentLanguage,
            dReturn = null,
            i
        ;

            //#### Traverse the a_sLanguages (as reported by the browser)
        for (i = 0; i < a_sLanguage.length; i++) {
                //#### Collect the .toLowerCase'd sCurrentLanguage for this loop
            sCurrentLanguage = (a_sLanguage[i] + '').toLowerCase();

                //#### If this is the first English definition
            if (sCurrentLanguage.indexOf('en') == 0) {
                    //#### If this is a definition for a non-American based English (meaning dates are "DD MM YYYY")
                if (sCurrentLanguage.indexOf('en-us') == -1 &&      // en-us = English (United States) + Palau, Micronesia
                    sCurrentLanguage.indexOf('en-ca') == -1 &&      // en-ca = English (Canada)
                    sCurrentLanguage.indexOf('en-ph') == -1 &&      // en-ph = English (Philippians)
                    sCurrentLanguage.indexOf('en-bz') == -1         // en-bz = English (Belize)
                ) {
                        //#### Setup a oRegEx to locate "## ## ####" (allowing for any sort of delimiter except a '\n') then collect the a_sMatches from the passed sDateString
                    var oRegEx = new RegExp("(([0-9]{2}|[0-9]{1})[^0-9]*?([0-9]{2}|[0-9]{1})[^0-9]*?([0-9]{4}))", "i");
                    a_sMatches = oRegEx.exec(sDateString);
                }

                    //#### Fall from the loop (as we've found the first English definition)
                break;
            }
        }

            //#### If we were able to find a_sMatches for a non-American English "DD MM YYYY" formatted date
        if (a_sMatches != null) {
            var oRegEx = new RegExp(a_sMatches[0], "i");
                //#### .parse the sDateString via the normal Date.parse function, but replacing the "DD?MM?YYYY" with "YYYY/MM/DD" beforehand
                //####     NOTE: a_sMatches[0]=[Default]; a_sMatches[1]=DD?MM?YYYY; a_sMatches[2]=DD; a_sMatches[3]=MM; a_sMatches[4]=YYYY
            dReturn = fDateParse(sDateString.replace(oRegEx, a_sMatches[4] + "/" + a_sMatches[3] + "/" + a_sMatches[2]));
        }
            //#### Else .parse the sDateString via the normal Date.parse function
        else {
            dReturn = fDateParse(sDateString);
        }

            //#### 
        return dReturn;
    }
})();

在我的实际(dotNet)代码中,我通过以下方式收集a_sLanguage数组:

In my actual (dotNet) code, I'm collecting the a_sLanguage array via:

a_sLanguage = '<% Response.Write(Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"]); %>'.split(',');

现在,我不确定我找到us-en/ etc的方法。是最合适的。几乎只有美国和当前/以前美国影响的地区(帕劳,密克罗尼西亚,菲律宾)+伯利兹&加拿大使用时髦的MM / DD / YYYY格式(我是美国人,所以我可以称之为funky =)。因此,如果Locale不是en-us/ etc,那么可以正确地争辩说。首先,应该使用DD / MM / YYYY。想法?

Now, I'm not certain my approach to locating "us-en"/etc. is the most proper. Pretty much it's just the US and current/former US influenced areas (Palau, Micronesia, Philippines) + Belize & Canada that use the funky MM/DD/YYYY format (I am American, so I can call it funky =). So one could rightly argue that if the Locale is not "en-us"/etc. first, then DD/MM/YYYY should be used. Thoughts?

作为旁注......我在PERL中长大但是自从我在RegEx中做了很多繁重的工作以来,这一直都很小。这个表达对每个人来说都是正确的吗?

As a side note... I "grew up" in PERL but it's been a wee while since I've done much heavy lifting in RegEx. Does that expression look right to everyone?

这似乎很多工作,但根据我的研究,这确实是关于启用DD / MM的最佳方式/ YYYY日期在JavaScript中。是否有更容易/更好的方式?

This seems like a lot of work, but based on my research this is indeed about the best way to go about enabling DD/MM/YYYY dates within JavaScript. Is there an easier/more betterer way?

PS-在提交之前重新阅读这篇文章...我意识到这更多的是一个你可以编码审查这个而不是一个问题(或者,问题中嵌入了一个答案)。当我开始写这篇文章时,我不打算在这里结束=)

推荐答案

我会用 Datejs 。您可以直接加载适合给定ISO语言代码的版本(例如date-en-CA.js或date-en-GB.js)。只有大写不同。

I would use Datejs. You can directly load the version appropriate for a given ISO language code (e.g. date-en-CA.js or date-en-GB.js). Only the capitalization is different.

这篇关于扩展JavaScript的Date.parse以允许DD / MM / YYYY(非美国格式的日期)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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