0

I am trying to compare a string in the Format "HH:MM" with the local time.

I used the following code:

function update(time_string) {
  now_datetime = new Date()


  time_datetime = new Date(time_string)

  var time = time_datetime.getHours() + ":" + time_datetime.getMinutes();
  var today = now_datetime.getHours() + ":" + now_datetime.getMinutes();
   
  console.log(time > today)
}

For some reasons, this produces different oucomes in Chrome and Safari. Safari adds two hours to the original time. Can anyone help me out how to make both browser produce the same results? Many thanks!

kaktus
  • 89
  • 1
  • 1
  • 9
  • 1
    Safai has a little bug in here, looks like: https://stackoverflow.com/questions/51708032/display-hours-and-minutes-works-on-all-browsers-except-safari-javascript-date – Preeti A. Jul 17 '20 at 18:21
  • What is the value of *time_string*? Parsing of unsupported formats is implemnetation dependent, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Jul 17 '20 at 20:08
  • @user2945950—that isn't a bug. '2018-08-06 20:45:00' is not a supported format so parsing is implementation dependent. Safari treats it as malformed ISO 8601 so returns an invalid Date. – RobG Jul 17 '20 at 20:15

0 Answers0