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