Javascript日期使用正斜杠vs连字符 [英] Javascript Date using forward-slashes vs hyphens
问题描述
当我创建一个新的Date对象并使用连字符传递日期
When I'm creating a new Date object and pass in a date using hyphens
new Date("2015-07-02") // I get Thu Jul 02 2015 01:00:00 GMT+0100 (IST)
当我使用正斜杠
new Date("2015/07/02") // I get Thu Jul 02 2015 00:00:00 GMT+0100 (IST)
注意时差:01:00: 00为连字符和00:00:00为正斜杠
notice the time difference: 01:00:00 for hyphens and 00:00:00 for forward slashes
这打破了我的代码:(
为什么这样正在发生吗?
任何解决方法?(使用连字符时,是否应该将时间设置为00:00:00?)
Why this is happening? Any workaround for this? (Should I just set time to 00:00:00 when using hyphens?)
我需要能够比较具有斜杠与日期的连字符的日期,我不知道我可能需要将日期与其他符号进行比较。
I need to be able to compare dates that have forward-slashes with dates that have hyphens and I'm not sure I might need to compare dates with some other symbols.
这是只发生在连字符?
谢谢。
推荐答案
>最近的浏览器可以 inter将日期字符串设为 ISO-8601
- 将执行。
If a recent browser can interpret the date string as ISO-8601
- it will do it.
示例:
YYYY (eg 1997)
YYYY-MM (eg 1997-07)
YYYY-MM-DD (eg 1997-07-16)
YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
使用此格式,您的日期/时间字符串解释为 UTC (!!
您应该坚持YYYY / MM / DD
为您的日期字符串只要有可能
You should Stick to "YYYY/MM/DD"
for your date strings whenever possible
这篇关于Javascript日期使用正斜杠vs连字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!