gpt4 book ai didi

javascript - 将特定于区域的日期格式设置为 dd-MMM-yyyy?

转载 作者:行者123 更新时间:2023-12-02 15:58:37 24 4
gpt4 key购买 nike

我们有一个本地 SharePoint 安装,其中 vendor 公司编写了一个 JavaScript 函数来解析给定列表中的所有日期字段并以 d-MMM-yyyy 格式显示它们(例如 24-Dec- 2015)页面加载。但是,当用户将其区域设置从 美国(英语) 更改为 英国(英语) 时,日期解析将会失败,因为日期格式从 更改为月/日/年日/月/年。对于其他语言环境也是如此。

继续使用此 JavaScript 解决方案,根据动态区域设置解析日期的最佳方法是什么?

目前,我能够获取用户选择的 LCID 并检索区域性代码(例如 en-US)并将其传递给 SugarJS像这样的库:

var currentCulture = "";

function configureCulture(){
//To accurately retrieve user culture, scrape the Regional Settings page for LCID
//This is a terrible hack, but couldn't find any other solution
var regionalSettingsUrl = ctx.HttpRoot+"/_layouts/regionalsetng.aspx?Type=User";

jQuery.ajax({
url: regionalSettingsUrl,
async: false,
dataType: 'html',
success: function(data) {
var lcid = jQuery(data).find("select[name$='LCID'] option:selected")[0].value;
currentCulture = LCIDUtils.getCultureName(lcid);
},
error: function(jqXHR, textStatus, errorThrown){
//debugMessage(errorThrown);
}
});
}

//passedDate param is a string representation of a date
//actual date varies based on culture, so must parse appropriately
//e.g. 12/3/2014 is 3-Dec-2014 in United States
//but 12-Mar-2014 in United Kingdom
function ApplyDateFormat(passedDate) {
if(currentCulture == ""){
configureCulture();
}

//Uses the SugarJs libary to create a date based on current culture code
return Date.create(passedDate, currentCulture).format('d-MMM-yyyy');
}

还有更好的解决办法吗?请注意,这只是为了在标准 View 中显示列表,并且基础数据不受影响。

最佳答案

查看 Date 对象的 toLocaleString/toLocaleFormat 方法。也许它们在这里很有用。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString

关于javascript - 将特定于区域的日期格式设置为 dd-MMM-yyyy?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31386853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com