Javascript 基于用户';s地区

Javascript 基于用户';s地区,javascript,momentjs,Javascript,Momentjs,我正在尝试使用moment.js解析、验证和重新格式化HTML表单中的日期。由于该网站正在国际上使用,我想将日期重新格式化为D MMM YYYY格式(例如:2018年4月1日),以尽量减少输入日期后的混淆 我的代码中的文本框定义如下: <input type="text" class="txt-date" name="foo" /> $(function () { console.log("initial locale: " + moment.locale());

我正在尝试使用moment.js解析、验证和重新格式化HTML表单中的日期。由于该网站正在国际上使用,我想将日期重新格式化为D MMM YYYY格式(例如:2018年4月1日),以尽量减少输入日期后的混淆

我的代码中的文本框定义如下:

<input type="text" class="txt-date" name="foo" />
$(function () {

    console.log("initial locale: " + moment.locale());
    console.log("initial date format:= " + moment.localeData().longDateFormat('L'));

    var locale = window.navigator.userLanguage || window.navigator.language;
    console.log("changing locale to: " + locale);
    moment.locale(locale);

    console.log("updated locale: " + moment.locale());
    console.log("updated date format = " + moment.localeData().longDateFormat('L'));

    $('input.txt-date').on('change', function() {
        var v = $(this).val();
        if (!v) return; // No value in the textbox

        v = moment(v); // Parse the date
        if (!v.isValid) {
            alert("Invalid Date!");
            return;
        }

        // Re-format the date
        $(this).val(v.format("D MMM YYYY"));
    });
});
$(function () {

    // Configure moment to use the user's locale
    moment.locale(window.navigator.userLanguage || window.navigator.language);

    $('input.txt-date').on('change', function () {
        var v = $(this).val();
        if (!v) return; // No value in the textbox

        // Parse the date specified
        var fmt = moment.localeData().longDateFormat('L');
        v = moment(v, [fmt, "D MMM YY", "D MMM YYYY"]);

        // If the date is invalid, warn the user
        if (!v.isValid()) {
            alert("Invalid Date!");
            return;
        }

        // Re-format the date
        $(this).val(v.format("D MMM YYYY"));
    });
});
以下是浏览器日志中的输出:

初始语言环境=en

初始日期格式=MM/DD/YYYY

将区域设置更改为:en GB

更新地区:en

更新日期格式=MM/DD/YYYY

本质上,moment.js似乎认为我应该使用英语(美国)区域设置,而不是英语(英国)。根据用户语言更改区域设置(如图所示)似乎没有什么不同

我所要做的就是能够以用户的本地/区域格式(如果他们以这种方式输入,则为D MMM yyy)解析日期字符串。有人能提供一些建议吗


解决方案

谢谢帕尼的帮助。为了实现这一点,您需要将时刻包含在locales.js文件中(该文件可在“”中找到,但也包含在NuGet包中)

更新后的JavaScript如下所示:

<input type="text" class="txt-date" name="foo" />
$(function () {

    console.log("initial locale: " + moment.locale());
    console.log("initial date format:= " + moment.localeData().longDateFormat('L'));

    var locale = window.navigator.userLanguage || window.navigator.language;
    console.log("changing locale to: " + locale);
    moment.locale(locale);

    console.log("updated locale: " + moment.locale());
    console.log("updated date format = " + moment.localeData().longDateFormat('L'));

    $('input.txt-date').on('change', function() {
        var v = $(this).val();
        if (!v) return; // No value in the textbox

        v = moment(v); // Parse the date
        if (!v.isValid) {
            alert("Invalid Date!");
            return;
        }

        // Re-format the date
        $(this).val(v.format("D MMM YYYY"));
    });
});
$(function () {

    // Configure moment to use the user's locale
    moment.locale(window.navigator.userLanguage || window.navigator.language);

    $('input.txt-date').on('change', function () {
        var v = $(this).val();
        if (!v) return; // No value in the textbox

        // Parse the date specified
        var fmt = moment.localeData().longDateFormat('L');
        v = moment(v, [fmt, "D MMM YY", "D MMM YYYY"]);

        // If the date is invalid, warn the user
        if (!v.isValid()) {
            alert("Invalid Date!");
            return;
        }

        // Re-format the date
        $(this).val(v.format("D MMM YYYY"));
    });
});

这种组合在我所能抛出的每个有效日期都有效。

问题是因为
时刻区域设置丢失或未导入。导入区域设置后,可以更改区域设置。您可以找到从CDN导入的本地语言环境的时刻(如下)

请注意,我已根据您的需要将区域设置硬编码为
en gb

$(函数(){
console.log(矩.locales())
log(“初始区域设置:+moment.locale());
log(“初始日期格式:=”+矩.localeData().longDateFormat('L'));
var locale=window.navigator.userLanguage | | window.navigator.language;
log(“将区域设置更改为:“+locale”);
地点(“en gb”);
log(“更新的区域设置:+moment.locale());
console.log(“updated date format=“+moment.localeData().longDateFormat('L'));
$('input.txt date')。在('change',函数(){
var v=$(this.val();
if(!v)return;//文本框中没有值
v=时刻(v);//解析日期
如果(!v.isValid){
警报(“无效日期!”);
返回;
}
//重新格式化日期
$(this.val)(v.format(“D MMM YYYY”);
});
});


添加locales文件似乎解决了报告日期格式的问题,该格式现在恢复为DD/MM/YYYY。不幸的是,解析仍然使用US格式-输入2018年9月1日的结果将在2018年1月9日出现,尽管moment.locale()报告“en gb”得到了它!我需要将格式添加到矩(v)方法中。我会在问题中提出解决方案-谢谢你的帮助!请不要将答案编辑到问题中;相反,在下面添加一个答案。这是一种非常常见的奖励方式,通过将他们的回答标记为答案来奖励让我成功90%的人,同时以一种不会被忽略的方式显示我问题的最终解决方案。