简体   繁体   English

识别客户端计算机中的日期格式

[英]Identifying the date format in the client's machine

I have a requirement in my project to show a date in the client machine format. 我的项目中要求以客户端计算机格式显示日期。 I am aware of the property in javascript window.navigator.userLanguage or window.navigator.language .. But it is only returning the language of the client machine like 'en-US', 'en-GB' etc. 我知道javascript window.navigator.userLanguagewindow.navigator.language ..中的属性,但是它仅返回客户端计算机的语言,例如“ en-US”,“ en-GB”等。

If the user customizes the date format of the machine for eg:-dd-MMM-yyyy, is there any way to get that format in Javascript? 如果用户自定义机器的日期格式,例如:-dd-MMM-yyyy,有什么办法可以用Javascript获得该格式?

Thanks in advance 提前致谢

suhaib 苏海卜

You can use the .toLocaleDateString() method : 您可以使用.toLocaleDateString()方法

var yourDate = new Date();
alert(yourDate.toLocaleDateString());

This doesn't tell your code what the user's selected date format is, but it lets you display a date in whatever the user's format is. 这不会告诉您的代码用户选择的日期格式是什么,但是可以让您以用户的格式显示日期。

On my PC the above alerts "Tuesday, 26 June 2012". 在我的电脑上,上述警报“ 2012年6月26日,星期二”。

The .toLocaleTimeString() method does the equivalent thing for the time. .toLocaleTimeString()方法在时间上等效。

The .toLocaleString() method displays date and time. .toLocaleString()方法显示日期时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM