admin管理员组文章数量:1310263
My simple test:
var ds = "2018/2/28 15:59";
console.log(ds);
var da = Date(ds);
console.log(da);
ds = "2018-2-28 15:59";
console.log(ds);
var da = Date(ds);
console.log(da);
My simple test:
var ds = "2018/2/28 15:59";
console.log(ds);
var da = Date(ds);
console.log(da);
ds = "2018-2-28 15:59";
console.log(ds);
var da = Date(ds);
console.log(da);
The results are
2018/2/28 15:59
Thu Feb 01 2018 17:26:57 GMT+0800 (+08)
2018-2-28 15:59
Thu Feb 01 2018 17:26:57 GMT+0800 (+08)
Even given the time "2018/2/28 15:59" is in a different time zone, it is still very puzzling as the minutes and seconds are different: 59:00 versus 26:57. Timezone differences are in multiples of 30 minutes.
Share Improve this question edited Feb 1, 2018 at 9:39 Mike Donkers 3,6992 gold badges22 silver badges34 bronze badges asked Feb 1, 2018 at 9:32 user3098791user3098791 391 gold badge2 silver badges6 bronze badges 5- Why don't you use "new Date(...)" instead of just "Date(...)"? w3schools./jsref/jsref_obj_date.asp w3schools./js/js_dates.asp – D. Braun Commented Feb 1, 2018 at 9:40
-
Notice how the date is wrong too. Your argument is being ignored and you would get the same result from a plain
Date()
. (Date()
acts differently when not used as a constructor, like innew Date()
. The latter isn’t guaranteed to parse many formats either, though, so just avoid it entirely.) – Ry- ♦ Commented Feb 1, 2018 at 9:40 - Why don't you use moment.js for parsing and dormatting Dates: momentjs. – D. Braun Commented Feb 1, 2018 at 9:41
- Look at your wrist watch. Does it match the result you get? – Álvaro González Commented Feb 1, 2018 at 9:41
- Regarding "Timezone differences are in multiples of 30", not necessarily. Some are 15 or 45 minutes, e.g. Nepal Standard Time is UTC+05:45 and Chatham Standard Time is UTC+12:45. Once you add "new", your next question will be answered by Why does Date.parse give incorrect results? – RobG Commented Feb 1, 2018 at 12:38
4 Answers
Reset to default 6You forgot to add new
before Date()
.
This means you were just calling a function called Date()
which (by default) retuns the current date and time.
var ds = "2018/2/28 15:59";
console.log(ds);
var da = new Date(ds);
console.log(da);
ds = "2018-2-28 15:59";
console.log(ds);
var da = new Date(ds);
console.log(da);
An addition to AuxTacos answer, the proper way to init. your date:
var da = new Date(2018, (2-1), 28, 15, 59); // x-1 because 0=Jan,1=Feb...
console.log(date);
In addition to zeropublix's answer (you forgot the new
), your date strings are invalid. The proper way to encode "15 hours and 59 minutes past the midnight marking the beginning of the 28th day of February, 2018 CE" is "2018-02-28T15:59Z"
. Your system (and mine) might recognize "2018/2/28 15:59"
as a valid date string, but that's implementation-dependent and prone to failure. The only format recognized in the specification is a simplification of ISO 8601.
I try this.
var ds = "2018/2/28 15:59";
var da = new Date(ds);
it gives me Date 2018-02-28T15:59:00.000Z. I got right time and date.
var da = Date(ds);
here Date gives the current time even if you pass parameter.
try this.
var ds = "2018/2/28 15:59";
var da = new Date(ds);
which gives 2018-02-28T10:29:00.000Z
本文标签: JavaScript Date() returns the wrong timeStack Overflow
版权声明:本文标题:JavaScript Date() returns the wrong time - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741842578a2400589.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论