通过 Javascript 将 DOMTimeStamp 转换为本地化的 HH:MM:SS MM-DD-YY
The W3C Geolocation API (among others) uses DOMTimeStamp for its time-of-fix.
This is "milliseconds since the start of the Unix Epoch".
What's the easiest way to convert this into a human readable format and adjust for the local timezone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Date
构造函数的一个版本将“自 Unix 纪元开始以来的毫秒数”作为其第一个也是唯一的参数。假设您的时间戳位于名为
domTimeStamp
的变量中,以下代码会将此时间戳转换为本地时间(假设用户在她/他的计算机上设置了正确的日期和时区)并打印人类可读的文本日期版本:其他内置日期格式化方法包括:
假设您的要求是打印“HH:MM:SS MM-DD-YY”的确切模式,您可以执行以下操作:
One version of the
Date
constructor takes the number of "milliseconds since the start of the Unix Epoch" as its first and only parameter.Assuming your timestamp is in a variable called
domTimeStamp
, the following code will convert this timestamp to local time (assuming the user has the correct date and timezone set on her/his machine) and print a human-readable version of the date:Other built-in date-formatting methods include:
Assuming your requirement is to print the exact pattern of "HH:MM:SS MM-DD-YY", you could do something like this:
然后您可以按照自己喜欢的方式格式化它。
您可能会找到 datejs,特别是它的 toString 格式,很有帮助。
You can then format it however you like.
You may find datejs, particularly its toString formatting, helpful.