Date.toString()的输出是否始终采用相同的格式? [英] Is the output of Date.toString() always in the same format?

查看:78
本文介绍了Date.toString()的输出是否始终采用相同的格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,是 Date.toString()的输出(更确切地说, Date.toTimeString())在所有浏览器中始终采用相同的格式?

As the title asks, is the output of Date.toString() (more precisely, Date.toTimeString()) always in the same format, in all browsers?

我问这个,因为 EMCAScript规范说字符串的内容依赖于实现。

I ask this since the EMCAScript specification says that the "contents of the String are implementation-dependent".

我需要确保这一点,因为我需要通过插入HTML span 元素来格式化字符串,如下所示:

I need to make sure of this because I need to format the string by inserting HTML span elements as follows:

(new Date())。toTimeString()。replace(GMT,< span id ='offset'> GMT)+'< ; / span>');

这会产生(在Google Chrome v28.0.1500.95中)类似

This would produce (in Google Chrome v28.0.1500.95) something like

18:19:26< span id =offset> GMT-0700(太平洋夏令时)< / span>

我可以用CSS设置样式。

which I can then style with CSS.

有关更好的输出样式方法的建议也很棒!

Suggestions for better ways to style the output would also be great!

推荐答案

简而言之,不,toString并不总是输出相同的格式。

In brief, no, toString does not always output the same format.

new Date().toString() 

打印


  • 星期六2012年2月11日02:15:10 GMT + 0100在Firefox 10中

  • 星期六2012年2月11日02:18:29 GMT + 0100(欧洲标准时间)在Chrome 16

  • 星期二2月11日02:18:59 UTC + 0100 2012在Internet Explorer 9中

我知道这些是较旧的浏览器版本,但它表明它取决于浏览器。

I know these are older browser version, but it shows that it is browser dependent.

但是,当使用 toTimeString()它似乎始终以小时开始:分钟:秒[空格] ...

However, when using toTimeString() it appears it always starts with hours:minutes:seconds[space]...

因此,您可以改为拆分根据第一个[s步伐]:

Therefore, you could instead split the string into 2 portions based on the first [space] with:

indexOf()

然后用你的范围包装第二部分

Then wrap the second portion with your span

Moment 会对日期进行一些字符串格式化,但它也无法很好地处理您请求的偏移字符串因为它取决于toString()方法。

Moment does some string formatting of dates, but it also does not handle your requested offset string very well as it depends on the toString() method.

我希望有帮助

这篇关于Date.toString()的输出是否始终采用相同的格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆