Javascript toLocaleDateString() 短格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27939773/
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
toLocaleDateString() short format
提问by GeekPeek
I want to have the short notation of the Date.toLocaleDateString() but in local format. There are a lot of solutions that hard-code the yyyy-mm-dd format but I want it to be dependent on the system that is hosting the page. This is my function thus far:
我想要 Date.toLocaleDateString() 的简短表示法,但采用本地格式。有很多解决方案可以对 yyyy-mm-dd 格式进行硬编码,但我希望它依赖于托管页面的系统。到目前为止,这是我的功能:
function getDate(dateTimeString)
{
var date = getDateTime(dateTimeString);
var options = { year: "numeric", month: "numeric", day: "numeric" };
return date.toLocaleDateString( date.getTimezoneOffset(), options );
}
but this returns it like so: Wednesday, January 28, 2015 which I don't want. Any suggestions/ideas?
但这会像这样返回它:2015 年 1 月 28 日,星期三,这是我不想要的。任何建议/想法?
PS: it's not a browser and there is a pretty real possibility that the person using it does not have interwebs connection; all information is gotten from a local database so I can't use any fany stuff like this How to get visitor's location (i.e. country) using javascript geolocation.
PS:它不是浏览器,使用它的人很可能没有互联网连接;所有信息都是从本地数据库中获取的,所以我不能使用像这样的任何花哨的东西如何使用 javascript geolocation 获取访问者的位置(即国家/地区)。
回答by lostomato
I think the function toLocaleDateString use the default local data on the device.
我认为函数 toLocaleDateString 使用设备上的默认本地数据。
try this code to check the output:
试试这个代码来检查输出:
// America/Los_Angeles for the US
// US English uses month-day-year order
console.log(date.toLocaleDateString('en-US'));
// → "12/19/2012"
// British English uses day-month-year order
console.log(date.toLocaleDateString('en-GB'));
// → "20/12/2012"
// Korean uses year-month-day order
console.log(date.toLocaleDateString('ko-KR'));
// → "2012. 12. 20."
// Arabic in most Arabic speaking countries uses real Arabic digits
console.log(date.toLocaleDateString('ar-EG'));
// → "???/???/????"
// chinese
console.log(date.toLocaleDateString('zh-Hans-CN'));
// → "2012/12/20"
回答by Nigel Scott
You could try something like:
你可以尝试这样的事情:
var date = new Date(Date.UTC(2015, 0, 28, 4, 0, 0));
console.log(date.toLocaleDateString("nl",{year:"2-digit",month:"2-digit", day:"2-digit"}));
Which gives me "28-01-15" in Chrome (48.0.2564.116) at least.
这至少在 Chrome (48.0.2564.116) 中给了我“28-01-15”。
Firefox just returns "01/28/2015", and phantomJS returns "28/01/2015" regardless of the locale.
Firefox 只返回“01/28/2015”,而 phantomJS 返回“28/01/2015”,无论语言环境如何。
回答by dSkech
Yes. Its pretty straightforward. You can use the date object as follows:
是的。它非常简单。您可以按如下方式使用日期对象:
var d = new Date();
var mm = d.getMonth() + 1;
var dd = d.getDate();
var yy = d.getFullYear();
Then you should have the numbers you need to form a string in whatever format you need.
然后,您应该拥有以您需要的任何格式形成字符串所需的数字。
var myDateString = yy + '-' + mm + '-' + dd; //(US)
Note this will give something like 2015-1-2 if the digits are in single figures, if you need 2015-01-02 then you will need further converting.
请注意,如果数字是个位数,这将给出类似 2015-1-2 的结果,如果您需要 2015-01-02,则需要进一步转换。
Also please note this will only give the 'client' date, ie. the date on the users system. This should be in their local time. If you need server time then you will have to have some kind of api to call.
另请注意,这只会给出“客户”日期,即。用户系统上的日期。这应该是他们的当地时间。如果您需要服务器时间,那么您将不得不调用某种 API。
回答by Donato
Note that NodeJS will only ship with the locale format of the device and so when you specify an argument toLocaleDateString like:
请注意,NodeJS 将只提供设备的语言环境格式,因此当您指定参数 toLocaleDateString 时,例如:
new Date("1983-March-25").toLocaleDateString('fr-CA', { year: 'numeric', month: '2-digit', day: '2-digit' })
'03/25/1983'
Notice you expected 'fr-CA' to give you YYYY-MM-DD, but it did not. That's because it is only ever using the US locale since my Node instance is running in the US locale.
请注意,您希望 'fr-CA' 为您提供 YYYY-MM-DD,但事实并非如此。那是因为它只使用美国语言环境,因为我的 Node 实例在美国语言环境中运行。
There's actually a bug report on Node github account delineating the issue and solution:
实际上有一个关于 Node github 帐户的错误报告描述了问题和解决方案:
https://github.com/nodejs/node/issues/8500
https://github.com/nodejs/node/issues/8500
The solution provided is installing the full-icumodule.
提供的解决方案是安装full-icu模块。
回答by kmandov
Apparently Date.prototype.toLocaleDateString() is inconsistent across browsers. You can implement different variations of a short date format, as explained here: How format JavaScript Date with regard to the browser culture?
显然 Date.prototype.toLocaleDateString() 跨浏览器不一致。您可以实现短日期格式的不同变体,如下所述: 如何根据浏览器文化格式化 JavaScript 日期?

