在javascript中更改粒子if is winter



我有两种粒子js的配置(json(。其中一个是冬天用的,所以它应该从21岁开始活动。12月至21日。前进我尝试了很多事情,但仍然没有成功(不是因为仍然是十一月:(我最后试过的是这个

var today = new Date();
var tDate = today.getDate();
var tMonth = today.getMonth();
var startingDate = new Date();
var sDate = startingDate.setDate(1);
var sMonth = startingDate.setMonth(10);
var endingDate = new Date();
var eDate = endingDate.setDate(21);
var eMonth = endingDate.setMonth(2);
if (tMonth >= sMonth && tMonth <= eMonth) {
if (tDate >= sDate && tDate <= eDate) {
particlesJS.load('particles', '../../Content/Scripts/particles/particles-config-winter.json');
}
}
else {
particlesJS.load('particles', '../../Content/Scripts/particles/particles-config.json');
}

这没有道理,因为开始和结束日期都是21。我试着用字符串进行比较,但没有成功。我不知道如何在javascript中比较两个日期。有什么帮助吗?

您可以使用以下函数:

function isWinter(dt) {
const m = dt.getMonth();
return m == 11 ? dt.getDate() >= 21 : m == 2 ? dt.getDate() < 21 : m < 2;
}

今天的日期可以这样称呼:

if (isWinter(new Date())) {  /*....*/ }

尝试中的一个问题是,setMonthsetDate方法以毫秒数(而不仅仅是月或日的一部分(返回修改后的日期,并更改调用该方法的日期。

功能说明:

三元表达式使用以下逻辑:

  • 如果月份是12月(11(,则在该月的日期至少为21时返回true,否则返回false:这就是dt.getDate() >= 21的计算结果(true或false(
  • 否则,如果月份是三月(2(,则当该月的日期小于21时返回true,否则返回false:与上述原理相同,但相反
  • 否则,当月份在三月之前时返回true(<2(,否则返回false:这就是m < 2的计算结果

日期操作总是一场斗争。如果你不介意库为你处理这个问题,我建议你看看MomentJs,它可以很容易地操作日期和时间。然而,您添加的每个库都会添加不必要的代码,而这些代码可能并不需要。因此,如果你只需要做一些日期操作,这可能不值得,如果你真的不需要MomentJs,trincot的答案是完美的。

按照trincot的回答,这就是你在MomentJs:中做同样事情的方式

function isWinter(date){
date.year(moment().year()); //Normalize year.
let winterStart = moment('21-12', 'DD-MM');
let winterEnd = moment('21-03', 'DD-MM');
return !date.isBetween(winterEnd, winterStart, 'day')
}
//Tests:
let today = moment('20-12', 'DD-MM');
alert(isWinter(today)); //false
today = moment('21-12', 'DD-MM');
alert(isWinter(today)); //true
today = moment('21-03', 'DD-MM');
alert(isWinter(today)); //true
today = moment('22-03', 'DD-MM');
alert(isWinter(today)); //false
today = moment('22-03-2019', 'DD-MM-YYYY');
alert(isWinter(today)); //false
today = moment('21-03-2019', 'DD-MM-YYYY');
alert(isWinter(today)); //true
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

解释代码不起作用的原因:

var today = new Date();
var tDate = today.getDate();
var tMonth = today.getMonth();
var startingDate = new Date();
// This sets the date to the first of the month, and sets sDate to a number
// that is the time value of the adjusted date
var sDate = startingDate.setDate(1);
// This sets the month to November and sMonth to the adjusted time value
var sMonth = startingDate.setMonth(10);
var endingDate = new Date();
// As above, this sets endingDate to 21 March and sets eDate and eMonth to numbers
var eDate = endingDate.setDate(21);
var eMonth = endingDate.setMonth(2);
// This will never be true after 1970-01-01 as you're comparing a months to a time values
if (tMonth >= sMonth && tMonth <= eMonth) {
if (tDate >= sDate && tDate <= eDate) {

即使使用了正确的值,逻辑也不起作用,因为即使对于该范围内的日期,您也要比较日期(日期(,而只有当月份是3月或11月,而不是任何其他月份时才应该进行比较(根据Trincot的回答(。

如果您在当前年份中创建3月21日和12月21日的日期,然后查看当前日期是否在该范围内,会更容易。如果是这样,那就不是冬天了。如果没有,那就是冬天。我假设这个范围是包容性的,如果不是这样,请调整日期。

function isWinter(date = new Date()) {
var start = new Date(date.getFullYear(), 2, 21);
var end = new Date(date.getFullYear(), 11, 21);
return date > start && date < end;
}
[new Date(2018, 0, 1), //  1 Jan 2018, winter
new Date(2018, 2,21), // 21 Mar 2018, winter
new Date(2018, 5, 1), //  1 Jun 2018, not winter
new Date(2018,11,21), // 21 Dec 2018, winter
new Date()            // Today ...
].forEach(d => console.log(`${d} is ${isWinter(d)?'not ':''}winter`));

最新更新