Add the season of the year to your daily note metadata

In the template: <% tp.user.getSeason(tp) %>

In the script file:

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
1 Like

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];
}
1 Like

Improved version:

async function getSeason(tp) {


	const quarter = Number(tp.date.now("Q", 0, tp.file.title, "YYYY-MM-DD"));
	console.log("quarter: " + quarter);
	let season = '';
	const sn = Number(tp.date.now("MDD", 0, tp.file.title, "YYYY-MM-DD"));
	console.log("season number: " + sn);

	if (quarter === 1) { 
		if (sn <= 320) { season = 'season: Winter'; } else { season = 'season: Spring'; }
		} 
	else if (quarter === 2) {
		if (sn <= 620) { season = 'season: Spring'; } else { season = 'season: Summer'; }
		} 
	else if (quarter === 3) {
		if (sn <= 920) { season = 'season: Summer'; } else { season = 'season: Autumn'; }
		}
	else if (quarter === 4) {
		if (sn <= 1220) { season = 'season: Autumn'; } else { season = 'season: Winter'; }
	}
	
	return season;	
}
module.exports = getSeason

The previous version was only approximating the season by looking at the quarter of the year.

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';
}
1 Like

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';
}

Example usage:

console.log(getSeason());
//Spring  [today is 2022-06-17]
console.log(getSeason("someday"));
//Unknown
console.log(getSeason(new Date("2022-01-01")));
//Winter
console.log(getSeason(new Date("2022-04-01")));
//Spring
console.log(getSeason(new Date("2022-07-01")));
//Summer
console.log(getSeason(new Date("2022-10-01")));
//Autumn
console.log(getSeason(new Date("2022-12-25")));
//Winter