【问题标题】:Javascript - Write out date depending on location [duplicate]Javascript - 根据位置写出日期[重复]
【发布时间】:2012-10-30 12:08:12
【问题描述】:

可能重复:
How do I display a date/time in the user’s locale format and time offset?

我们写东西的方式不同,日期就是其中之一。
我想显示一个日期(不是当前日期),并根据他们的国家/地区为每个人设置格式。

我想我可以去编写一个新的 javascript 文件,其中包含所有位置检查,然后格式化给定的字符串。但我很确定某处有一个预先编写的解决方案,它比我能来的要好得多跟上。

【问题讨论】:

  • 这出乎意料的简单。对不起。我应该关闭这个问题吗? (你为什么不发布一个答案?)
  • 下次看看MDN

标签: javascript date formatting geolocation


【解决方案1】:

尝试使用:

  // This would come from the server.
// Also, this whole block could probably be made into an mktime function.
// All very bare here for quick grasping.
d = new Date();
d.setUTCFullYear(2004);
d.setUTCMonth(1);
d.setUTCDate(29);
d.setUTCHours(2);
d.setUTCMinutes(45);
d.setUTCSeconds(26);

alert(d);                        // -> Sat Feb 28 2004 23:45:26 GMT-0300 (BRT)
alert(d.toLocaleString());       // -> Sat Feb 28 23:45:26 2004
alert(d.toLocaleDateString());   // -> 02/28/2004
alert(d.toLocaleTimeString());   // -> 23:45:26

来源:Display date/time in user's locale format and time offset

【讨论】:

  • 不需要剪切和粘贴另一个答案的内容。只需用链接发表评论
猜你喜欢
  • 2015-08-29
  • 2014-03-13
  • 1970-01-01
  • 2013-07-01
  • 2019-08-12
  • 2016-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多