async function getSeason(tp) {
let quarter = tp.date.now("[Q]Q", 0, tp.file.title, "YYYY-MM-DD");
let season;
switch (quarter) {
case 'Q1': season = 'Winter'; break;
case 'Q2': season = 'Spring'; break;
case 'Q3': season = 'Summer'; break;
case 'Q4': season = 'Fall'; break;
default: season = 'Error'; break;
}
return season;
}
module.exports = getSeason
In using quarters in your function, for example, Spring gets defined as April/May/Jun. However, meteorologically speaking Spring should be March/April/May.
The function below applies the meteorological definitions to the seasons. (Note, one could also try applying the astronomical meaning of seasons, but that would depend on the dates of the solstices and equinoxes. Since those dates vary by year, astronomical seasons would be a much more difficult function to write.)
function getSeason()
{
const mon = (new Date()).getMonth();
const seasons = ['Winter','Winter','Spring','Spring','Spring','Summer','Summer','Summer','Fall','Fall','Fall','Winter'];
return seasons[mon];
}
The above function gets closer to the astronomical definition of seasons by approximating that the solstices and equinoxes occur on the 20th of their respective months. That’s not a bad estimate and keeps things reasonably simple.
I’d recommend making a couple simplifications to your function, though. First, there’s no need to worry about quarters. Secondly, I’d prefer to avoid the dependency on ‘tp’. Therefore:
function getSeason()
{
const now = new Date;
const mdd = ((now.getMonth() + 1) * 100) + now.getDate();
if (mdd <= 320) { return 'Winter'; }
if (mdd <= 620) { return 'Spring'; }
if (mdd <= 920) { return 'Summer'; }
if (mdd <= 1220) { return 'Autumn'; }
return 'Winter';
}
The earlier function only gives the season for the current date. With a little modification, it can provide the season for any javascript Date object. If this date is not provided, it defaults back to providing the value for the current date. Since the function now allows a parameter, it must also handle being passed something that is not a Date. In this case, it returns ‘Unknown’.
function getSeason(date)
{
if (date === undefined) { date = new Date; }
if (Object.prototype.toString.call(date) !== "[object Date]") { return "Unknown"; }
const mdd = ((date.getMonth() + 1) * 100) + date.getDate();
if (mdd <= 320) { return 'Winter'; }
if (mdd <= 620) { return 'Spring'; }
if (mdd <= 920) { return 'Summer'; }
if (mdd <= 1220) { return 'Autumn'; }
return 'Winter';
}