繁体   English   中英

根据今天的日期获取下一个或最后一次出现的日期和月份

[英]Get the next or last occurrence of a day and month based on today's date in Javascript

我需要确定自己是在每年的春季,夏季还是秋季,然后根据该日记显示日记。 周期如下:

Spring: 14th January - 6th May 
Summer: 7th May - 7th October
Autumn: 8th October - 13th January

所以今天(2月7日),我正处于春季(2019年1月14日至2019年5月6日)并展示2019年春季刊。 但是,我们假设它是2019年圣诞节。我将进入秋季(2019年10月8日至2020年1月13日),因此我将展示2019年秋季刊,这意味着下一个春季和夏季刊物将于2020年出版。

我一直在处理JS日期和矩对象,以尝试根据今天建立当前/下一个周期,我将在此处发布代码,但是我几乎确定这都是没用的。 那是秋天的一年中的变化,这让我感到震惊。 有没有人提供任何可能有用的库或建议? 还是我想得太多?

答案从这里修改。 只需传入当前季节对象和今天的日期即可(可以是一个简单的var date = new Date() )。 我所知道的所有图书馆都不会自动为您提供季节性日期。 您必须添加某种按计划运行的代码,或者在每年的特定季节执行的代码,以更新或重建日期对象,以便它们具有正确的年份。

如果您正在寻找更复杂的方法而不必更新日期,请查看这篇文章

var spring = {};
spring.startDate = "02/01/2019"
spring.endDate = "02/09/2019"
var today = "02/07/2019"

function checkSeason(season, currentDate) {
  console.log(season.startDate)
  console.log(currentDate)

  var d1 = season.startDate.split("/");
  var d2 = season.endDate.split("/");
  var c = currentDate.split("/");

  var from = new Date(d1[2], parseInt(d1[1]) - 1, d1[0]); // -1 because months are from 0 to 11
  var to = new Date(d2[2], parseInt(d2[1]) - 1, d2[0]);
  var check = new Date(c[2], parseInt(c[1]) - 1, c[0]);

  if (check > from && check < to) {
    console.log("its in the season")
  }
}


checkSeason(spring, today)

这是一个非常简单的解决方案,没有库。 仅在每年的日期相同的情况下,才能按原样解决所有未来年份的问题。

如果您计划每年有更多的问题并不断运行多次,则可以用不同的方式重写。

// you can completely ignore autumn because if it's not spring or summer it will be Autumn. 
// But I still put it to 2019 just in case you planned to use it with this method.
let issues = { "Spring": [new Date(2019, 0, 14), new Date(2019, 4, 6)],
               "Summer": [new Date(2019, 4, 7), new Date(2019, 9, 7)],
               "Autumn": [new Date(2019, 9, 8), new Date(2019, 0, 13)]
             }

// today is the day we are trying to find the correct issue for.
// Any date can be put here, just setFullYear to 2019, 2019 is arbitrary
let today = new Date().setFullYear(2019); 

// console.log the appropriate issue for today
if (issues.Summer[0] <= today && issues.Summer[1] >= today) {
    console.log("Summer");
} else if (issues.Spring[0] <= today && issues.Spring[1] >= today) {
    console.log("Spring");
} else {
    console.log("Autumn");
}

正如Sam所说,您可以通过比较当年的其他季节来避免年份过渡。 如果不是其他之一,则必须是一个过渡。

 function getSeason(d) { // Default to today's date d = d || new Date(); // Zero time part for comparison d.setHours(0,0,0,0); // Use the current year for comparisons var year = d.getFullYear(); // Default season if others fail var season = 'Autumn'; // Data for seasons, use ECMAScript month numbering var seasons = [ {name: 'Spring', start: [0, 14], // Jan 14 end: [4, 6]}, // May 06 {name: 'Summer', start: [4, 7], // May 07 end: [9, 7]} // Oct 07 ]; // Set season seasons.forEach(s => { let sStart = new Date(year, ...s.start); let sEnd = new Date(year, ...s.end); if (d >= sStart && d <= sEnd) { season = s.name; } }) return season; } console.log('Today is : ' + getSeason()); [new Date(2019,0, 1), // 1 Jan Autumn new Date(2019,0,21), // 21 Jan Spring new Date(2019,5,30) // 30 Jun Summer ].forEach(d => { console.log(d.toDateString()+ ': ' + getSeason(d)); }); 

使用新功能,代码可以更加简洁:

 function getSeason(d = new Date()) { d.setHours(0,0,0,0); let year = d.getFullYear(); let seasons = [ {name: 'Spring', start: [0, 14], end: [4, 6]}, {name: 'Summer', start: [4, 7], end: [9, 7]} ]; return (seasons.find(s => d >= new Date(year, ...s.start) && d <= new Date(year, ...s.end)) || {name:'Autumn'}).name; } console.log('Today is : ' + getSeason()); [new Date(2019,0, 1), // 1 Jan Autumn new Date(2019,0,21), // 21 Jan Spring new Date(2019,5,30) // 30 Jun Summer ].forEach(d => { console.log(d.toDateString()+ ': ' + getSeason(d)); }); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM