0

I try to run popup window on my website only during specific hours. So I made this code but I got error in console "Invalid Date". I need to set my popup to London time. What can be wrong?

jQuery(document).ready(function($){

var date = new Date().toLocaleString("en-GB", {timeZone: "Europe/London"});
date = new Date(date);

var curHr = date.getHours();
var minutes = 1;

if ((curHr > 16 && curHr < 24) || (curHr > 0 && curHr < 8)) {
    date.setTime(date.getTime() + (minutes * 60 * 1000));

    var active = Cookies.get('active');
    if (active == 'yes') {
      return false; // cookie active do nothing
    } else { //trigger popup and set a cookie
      setTimeout(function() {
        $(".fancybox").eq(0).trigger('click');
      }, 500);
      $(".fancybox")
        .attr('rel', 'group')
        .fancybox({
          padding: 0,
          width: 530,
          height: 550,
          scrolling: 'auto'
        });
    }
    Cookies.set('active', 'yes', {
      expires: 1 / 1440 // set to 1 minute.
    });
}
console.log(date);
});
Mike
  • 159
  • 10

1 Answers1

1
var date = new Date().toLocaleString("en-GB", {timeZone: "Europe/London"});
date = new Date(date); // Invalid Date since date is like  "13/01/2020, 02:36:57"

The new Date will take a string which is in format YYYY-MM-DD or YYYY-MM-DDTHH:MM:SSZ (sample formats in ISO)

Try to change the String format and try.

var date = new Date().toLocaleString("en-GB", {timeZone: "Europe/London"});
date = new Date(date); // Not working

var date = new Date().toLocaleString("en-US", {timeZone: "Europe/London"});
date = new Date(date); // Working

var date = new Date().toLocaleString("en-UK", {timeZone: "Europe/London"});
date = new Date(date); // Working
Pawan Kumar
  • 1,443
  • 2
  • 16
  • 30
  • Hmmm, I don't get it. If I change timezone to: new Date().toLocaleString("en-US", {timeZone: "America/New_York"}); everything works fine. London not working. – Mike Jan 13 '20 at 02:51