JavaScript - 获取系统短日期格式

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

JavaScript - Get system short date format

javascriptdatedatetime

提问by WooDzu

Is there any way to get system short date format in JavaScript?

有没有办法在 JavaScript 中获取系统短日期格式?

For example whether system's short date is in American format eg. m/d/Y or in european eg. d/m/Y

例如系统的短日期是否为美国格式,例如。m/d/Y 或在欧洲,例如。日/米/年

Please note:This is not question about formatting date or calculating it based on geolocation, but about getting the format from the OS/system

请注意:这不是关于格式化日期或根据地理位置计算日期的问题,而是关于从操作系统/系统获取格式的问题

回答by WooDzu

After a pinch of research I concluded that technically it's not possible to get regional settings -and by this, date format- but you can do several other things. Pick one of these options:
a) The already mentioned -and outdated- "toLocaleString()" function:

经过一番研究,我得出的结论是,从技术上讲,不可能获得区域设置——通过这种方式,日期格式——但你可以做其他几件事。选择以下选项之一:
a) 已经提到的 - 和过时的 - “toLocaleString()”函数:

var myDate = new Date(1950, 01, 21, 22, 23, 24, 225);
var myDateFormat = myDate.toLocaleString();
alert (myDateFormat);

ISSUES:
1) You can't "myDateFormat.replace" to get the date mask as month is not stored as "01", "02", etc in the string but as text instead, based on locale (like "February" in English but it's "Φεβρου?ριο?" in Greek and who knows what in e.g. Klingon).
2) Different behavior on different browsers
3) Different behavior on different OS and browser versions...

b) Use the toISOString()function instead of toLocaleString(). You won't get the locale date mask but get a date from which you can tell where's which part of the date (ie where "month" or "day" is in that string). You can also work with getUTCDate(), getUTCMonth()and getUTCDay()functions. You still can't tell what date format the client uses, but can tell which Year/Month/Day/etc you work with when you grab a date; use the codeabove to test the functions I mentioned here to see what you can expect.

c) Read Inconsistent behavior of toLocaleString() in different browserarticle and use the (IMHO great) solution described there

问题:
1) 您不能通过“myDateFormat.replace”来获取日期掩码,因为月份不是在字符串中存储为“01”、“02”等,而是作为文本存储,基于区域设置(如“二月”英语,但它是希腊语中的“Φεβρου?ριο?”,谁知道克林贡语中是什么)。
2) 不同浏览器上的不同行为
3) 不同操作系统和浏览器版本上的不同行为...

b) 使用toISOString()函数代替toLocaleString()。您不会获得区域设置日期掩码,但会获得一个日期,您可以从中知道日期的哪一部分(即“月”或“日”在该字符串中的位置)。您还可以使用getUTCDate(),getUTCMonth()getUTCDay()函数。您仍然无法分辨客户端使用什么日期格式,但是当你约会时可以知道你是在哪一年/哪月/哪天/等等工作;使用code上面测试我在这里提到的功能,看看你能期待什么。

c) 阅读 不同浏览器文章中 toLocaleString() 的不一致行为,并使用那里描述的(恕我直言,很棒)解决方案

回答by Reno

It is not possible. You can get culture from user browser and use some js libraries to convert to correct date format. http://code.google.com/p/datejs/

这不可能。您可以从用户浏览器获取文化并使用一些 js 库转换为正确的日期格式。http://code.google.com/p/datejs/

回答by Hodeifa Baswel

I made a function to determine the client date format. The function determine the date format separator, and also determine the 1st, 2nd and third part of the date format.

我做了一个函数来确定客户端日期格式。该函数确定日期格式分隔符,并确定日期格式的第一、第二和第三部分。

getDateFormat(){
    // initialize date value "31st January 2019"
    var my_date = new Date(2019,0,31);
    console.log(my_date.toLocaleDateString());
    // Initialize variables
    var separator="";
    var first="";
    var second="";
    var third="";
    var date_parts = [];

    // get separator : "-", "/" or " ", format based on toLocaleDateString function        
    if (my_date.toLocaleDateString().split("-").length==3){
        separator = " - ";
        date_parts = my_date.toLocaleDateString().split("-");
    } 
    if (my_date.toLocaleDateString().split("/").length == 3) {
        separator = " / ";
        date_parts = my_date.toLocaleDateString().split("/");
    } 
    if (my_date.toLocaleDateString().split(" ").length == 3) {
        separator = " ";
        date_parts = my_date.toLocaleDateString().split(" ");
    } 

    // get first part        
    if (date_parts[0]==2019){
        first ="yyyy";
    } else if (date_parts[0] == 31){
        first = "dd";
    } else{
        if (date_parts[0].length<=2){
            first ="mm";
        }
        else{
            first="mmm";
        }
    }

    // get second part        
    if (date_parts[1] == 2019) {
        second = "yyyy";
    } else if (date_parts[1] == 31) {
        second = "dd";
    } else {
        if (date_parts[1].length <= 2) {
            second = "mm";
        }
        else {
            second = "mmm";
        }
    }

    // get third part        
    if (date_parts[2] == 2019) {
        third = "yyyy";
    } else if (date_parts[2] == 31) {
        third = "dd";
    } else {
        if (date_parts[2].length <= 2) {
            third = "mm";
        }
        else {
            third = "mmm";
        }
    }

    // assembly
    var format = first + separator + second + separator + third;
    console.log(format);
    return format;
}

回答by Mahdy Aslamy

for my case i used a custom date that i know what number is day, what is month and what is year so it can possible with a simple replace statement.

就我而言,我使用了一个自定义日期,我知道什么是日,什么是月,什么是年,因此可以使用简单的替换语句。

let customDate = new Date(2222, 11, 18);
let strDate = customDate.toLocaleDateString();
let format = strDate
    .replace("12", "mm")
    .replace("18", "dd")
    .replace("2222", "yyyy");

回答by Ben

Use Date.CultureInfo.formatPatterns.shortDate

使用 Date.CultureInfo.formatPatterns.shortDate