3

What I am trying to do is changing "yyyy-mm-dd HH:mm:ss" string to date value.

Here is the current code

var c = new Date('2019-01-19 23:59:59'.replace(/\s+/g, 'T'))

It returns

  • chrome : Sat Jan 19 2019 23:59:59 GMT+0900 (KST)
  • safari : Sun Jan 20 2019 08:59:59 GMT+0900 (KST)
  • ie11 : Sat Jan 19 2019 23:59:59 GMT+0900 (KST)

What should I do to make it returns all same date?

Thanks.

Juneyoung Oh
  • 7,318
  • 16
  • 73
  • 121

2 Answers2

2

Safari... It does not consider time zone offset when creates an instance with date string.

Add Z to the end is also good point, but if you want to get the same result with other browsers, should calculate timezone offset.

Here is what I have done ...

// Before do this, check navigator.userAgent 
// and execute below logic if it is desktop Safari.

// Add Z is the convention, but you won't get any error even if do not add.
var c = new Date('2019-01-19 23:59:59Z'.replace(/\s+/g, 'T')) 

// It will returns in minute
var timeOffset = new Date().getTimezoneOffset();

// Do not forget getTime, if not, you will get Invalid date         
var d = new Date(c.getTime() + (timeOffset*60*1000))    

Will open this post till tomorrow for waiting better answer. Thanks.

Juneyoung Oh
  • 7,318
  • 16
  • 73
  • 121
  • This adjusts the date by the current timezone offset when it should use the one for the date. **Do not** "*check navigator.userAgent*" and infer behaviour, that is seriously poor practice. You can parse the OP format as local in two lines, which is one less than the code above and doesn't mess with offsets. – RobG Jan 16 '19 at 10:24
  • 2
    BTW, the OP format can be parsed in 1 line using destructuring. Interesting, but not recommended: `new Date(...('2019-01-19 23:59:59'.split(/\D/).map((x,i)=>i==1?x-1:x)))`. :-) – RobG Jan 16 '19 at 23:44
  • 1
    @RobG In my opinion, your answer is much acceptable than mine. The code you wrote is makes the statement like `new Date('2019', '0', '19','23', '59','59')`, I guess.(wondeful bb). However I would like to know why you not recommend this way. Also I would like to know meaning of `OP`. Thanks for the solution! – Juneyoung Oh Jan 17 '19 at 04:20
  • "OP" is "original post" or "original poster". I wouldn't recommend it as it's not particularly maintainable or easy to understand. A simple destructuring assignment is only one more line and a lot clearer. – RobG Jan 17 '19 at 04:50
  • Thanks for quick response and a good answer! – Juneyoung Oh Jan 17 '19 at 04:54
  • @RobG Want to close this post. I think your answer would be the most acceptable one. Would you add as an Answer, if you do not mind.(Or I do not have to choose any answer to close since it is duplicated question?) Thanks. – Juneyoung Oh Jan 28 '19 at 01:26
1

Add the 'Z' to the date string for GMT/UTC timezone

var c = new Date('2019-01-19 23:59:59'.replace(/\s+/g, 'T')+'Z');

ISO dates can be written with added hours, minutes, and seconds (YYYY-MM-DDTHH:MM:SSZ): Date and time is separated with a capital T.

UTC time is defined with a capital letter Z.

If you want to modify the time relative to UTC, remove the Z and add +HH:MM or -HH:MM instead:

for example var d = new Date("2019-01-19T23:59:59-09:00");

  • That is the convention, but Safari will not return same value with others. I found my way to calculate and add a reply. Thanks though – Juneyoung Oh Jan 16 '19 at 06:03