【问题标题】:Invalid Date - JavaScript GMT time无效日期 - JavaScript GMT 时间
【发布时间】:2020-04-29 18:33:09
【问题描述】:

我尝试仅在特定时间在我的网站上运行弹出窗口。所以我编写了这段代码,但在控制台“无效日期”中出现错误。我需要将弹出窗口设置为伦敦时间。有什么问题?

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);
});

【问题讨论】:

  • 删除日期 = 新日期(日期);
  • 这会返回错误:“Uncaught TypeError: date.getHours is not a function”

标签: javascript jquery time gmt


【解决方案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"

新日期将采用YYYY-MM-DDYYYY-MM-DDTHH:MM:SSZ 格式的字符串(ISO 中的示例格式)

尝试更改字符串格式并尝试。

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

【讨论】:

  • 嗯,我不明白。如果我将时区更改为: new Date().toLocaleString("en-US", {timeZone: "America/New_York"});一切正常。伦敦不工作。
猜你喜欢
  • 2021-03-25
  • 1970-01-01
  • 2012-10-07
  • 1970-01-01
  • 1970-01-01
  • 2018-04-13
  • 1970-01-01
  • 1970-01-01
  • 2021-05-02
相关资源
最近更新 更多