代码之家  ›  专栏  ›  技术社区  ›  panzer

如果在javascript中是冬季,则更改粒子

  •  2
  • panzer  · 技术社区  · 6 年前

    对于粒子js,我有两种配置(json)。其中一个是在冬季,所以从12月21日到3月21日应该是活跃的。我尝试了很多事情,但仍然没有成功(不是因为现在仍然是11月:)

    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中比较两个日期。有什么帮助吗?

    3 回复  |  直到 6 年前
        1
  •  2
  •   trincot Jakube    6 年前

    您可以使用此功能:

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

    在今天的约会中这样称呼它:

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

    您尝试中的一个问题是 setMonth setDate 方法以毫秒数(不仅仅是一个月或一天部分)返回修改后的日期,并改变调用方法的日期。

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

    • 如果月份是12月(11日),则在该月的日期至少为21时返回true,否则返回false:这是什么 dt.getDate() >= 21
    • 否则,当月份在3月之前时返回true(<2)否则为假:这是什么 m < 2 评估为。
        2
  •  0
  •   jonaslagoni    6 年前

    MomentJs 这样就很容易操纵日期和时间。但是,您添加的每个库都会添加可能不需要的不必要代码。因此,如果你只需要做几次约会操作,那可能就不值得了 trincot's answer

    按照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>
        3
  •  0
  •   RobG    6 年前

    要解释代码不起作用的原因,请执行以下操作:

    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的回答)。

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