我需要确定我处于任何给定年份的春季,夏季或秋季,并根据此显示日记。周期如下:
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年出版。 / p>
我一直在处理JS日期和矩对象,以尝试根据今天建立当前/下一个周期,我将在此处发布代码,但是我几乎确定这都是没用的。那是秋天的一年中的变化,这让我感到震惊。有没有人提供任何可能有用的库或建议?还是我想得太多?
答案 0 :(得分:0)
根据here修改的答案。只需传入当前季节对象和今天的日期即可(可以是简单的var date = new Date()
)。我所知道的所有图书馆都不会自动为您提供季节性日期。您必须添加某种在时间表上运行或在每年的特定季节执行的代码,以更新或重建日期对象,以便它们具有正确的年份。
如果您正在寻找更复杂的方法而不必更新日期,请查看this post
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)
答案 1 :(得分:0)
这是一个非常简单的解决方案,没有库。仅在每年的日期相同的情况下,才能按原样解决所有未来年份的问题。
如果您计划每年有更多的期刊并不断运行多次,则可以用不同的方式重写。
// 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");
}
答案 2 :(得分:0)
就像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));
});