Javascript与桌面Safari的兼容性

时间:2019-08-08 20:45:07

标签: javascript date datetime safari

我编写了以下代码,用于根据日期显示不同的图像(现在此示例只是控制台记录一条消息)。该代码在Mac上的Chrome和Firefox上可以正常运行,但在Safari上无法正常工作或出现任何错误(在Safari中,消息不会更改,具体取决于日期,只是表示相同)。 Safari如何对此进行不同处理?我如何才能在最小的更改下使其在Safari上正常工作?

Here's a working repl

代码如下:

/* change these dates */
var ddt = new Date("2019, 8, 22");
var pre = new Date("2019, 8, 23");
var ton = new Date("2019, 8, 26");
var post = new Date("2019, 8, 27");

// todays date 
var currDate = new Date();
var mm = currDate.getMonth() + 1;
var dd = currDate.getDate();
var yyyy = currDate.getFullYear();

// Get the date parts
var ddtDay = ddt.getDate();
var ddtMonth = ddt.getMonth() + 1;
var ddtYear = ddt.getFullYear();
//console.log(ddtYear, ddtMonth, ddtDay);
var preDay = pre.getDate();
var preMonth = pre.getMonth() + 1;
var preYear = pre.getFullYear();
//console.log(preYear, preMonth, preDay);
var tonDay = ton.getDate();
var tonMonth = ton.getMonth() + 1;
var tonYear = ton.getFullYear();
//console.log(tonYear, tonMonth, tonDay);
var postDay = post.getDate();
var postMonth = post.getMonth() + 1;
var postYear = post.getFullYear();
//console.log(postYear, postMonth, postDay);

// format the date parts
if (ddtDay < 10) {
  ddtDay = '0' + ddtDay;
}
if (ddtMonth < 10) {
  ddtMonth = '0' + ddtMonth;
}
if (preDay < 10) {
  preDay = '0' + preDay;
}
if (preMonth < 10) {
  preMonth = '0' + preMonth;
}
if (tonDay < 10) {
  tonDay = '0' + tonDay;
}
if (tonMonth < 10) {
  tonMonth = '0' + tonMonth;
}
if (postDay < 10) {
  postDay = '0' + postDay;
}
if (tonMonth < 10) {
  postMonth = '0' + postMonth;
}
if (dd < 10) {
  dd = '0' + dd;
}
if (mm < 10) {
  mm = '0' + mm;
}

var ddtF = (ddtYear + '-' + ddtMonth + '-' + ddtDay);
var preF = (preYear + '-' + preMonth + '-' + preDay);
var tonF = (tonYear + '-' + tonMonth + '-' + tonDay);
var postF = (postYear + '-' + postMonth + '-' + postDay);
var today = (yyyy + '-' + mm + '-' + dd);
console.log(ddtF);
console.log(preF);
console.log(tonF);
console.log(postF);
console.log(today);

// logic
if (today >= postF) {
  console.log('post');
} else if (today === tonF) {
  console.log('ton');
} else if (today < tonF && today >= preF) {
  console.log('pre');
} else if (today <= ddtF) {
  console.log('ddt');
}

1 个答案:

答案 0 :(得分:1)

"2019, 8, 22"不是可移植的日期格式。 Date构造函数具有可移植的调用序列,在该序列中,您将日期的每个部分作为单独的参数给出,因此请使用

var ddt = new Date(2019, 7, 22);

,其他所有变量也是如此。

请记住,在JavaScript中,月份是从0开始计算的,因此您需要从month参数中减去1(8月为7)。

/* change these dates */
var ddt = new Date(2019, 7, 22);
var pre = new Date(2019, 7, 23);
var ton = new Date(2019, 7, 26);
var post = new Date(2019, 7, 27);

// todays date 
var currDate = new Date();
var mm = currDate.getMonth() + 1;
var dd = currDate.getDate();
var yyyy = currDate.getFullYear();

// Get the date parts
var ddtDay = ddt.getDate();
var ddtMonth = ddt.getMonth() + 1;
var ddtYear = ddt.getFullYear();
//console.log(ddtYear, ddtMonth, ddtDay);
var preDay = pre.getDate();
var preMonth = pre.getMonth() + 1;
var preYear = pre.getFullYear();
//console.log(preYear, preMonth, preDay);
var tonDay = ton.getDate();
var tonMonth = ton.getMonth() + 1;
var tonYear = ton.getFullYear();
//console.log(tonYear, tonMonth, tonDay);
var postDay = post.getDate();
var postMonth = post.getMonth() + 1;
var postYear = post.getFullYear();
//console.log(postYear, postMonth, postDay);

// format the date parts
if (ddtDay < 10) {
  ddtDay = '0' + ddtDay;
}
if (ddtMonth < 10) {
  ddtMonth = '0' + ddtMonth;
}
if (preDay < 10) {
  preDay = '0' + preDay;
}
if (preMonth < 10) {
  preMonth = '0' + preMonth;
}
if (tonDay < 10) {
  tonDay = '0' + tonDay;
}
if (tonMonth < 10) {
  tonMonth = '0' + tonMonth;
}
if (postDay < 10) {
  postDay = '0' + postDay;
}
if (tonMonth < 10) {
  postMonth = '0' + postMonth;
}
if (dd < 10) {
  dd = '0' + dd;
}
if (mm < 10) {
  mm = '0' + mm;
}

var ddtF = (ddtYear + '-' + ddtMonth + '-' + ddtDay);
var preF = (preYear + '-' + preMonth + '-' + preDay);
var tonF = (tonYear + '-' + tonMonth + '-' + tonDay);
var postF = (postYear + '-' + postMonth + '-' + postDay);
var today = (yyyy + '-' + mm + '-' + dd);
console.log(ddtF);
console.log(preF);
console.log(tonF);
console.log(postF);
console.log(today);

// logic
if (today >= postF) {
  console.log('post');
} else if (today === tonF) {
  console.log('ton');
} else if (today < tonF && today >= preF) {
  console.log('pre');
} else if (today <= ddtF) {
  console.log('ddt');
}