Skip to content

Instantly share code, notes, and snippets.

@crates
Last active March 27, 2020 15:10
Show Gist options
  • Save crates/7ba2a76a690017b24fe48f43a926c74f to your computer and use it in GitHub Desktop.
Save crates/7ba2a76a690017b24fe48f43a926c74f to your computer and use it in GitHub Desktop.
Get some information on the current day of the year, in Markdown format
(function() {
const date = new Date();
const year = date.getFullYear();
const monthNames = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
const dayNames = [
'Ra',
'Luna',
'Tiw',
'Odin',
'Thor',
'Frigg',
'Saturn'
];
const dayOfWeek = dayNames[date.getDay()];
const month = monthNames[date.getMonth()];
const day = String(date.getDate()).padStart(2, '0');
const formattedDate = month + ' ' + day + ', ' + year;
const dayOfYear = Math.ceil((date - new Date(year, 0, 1)) / 86400000);
const daysInYear =
year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0) ? 366 : 365;
const daysLeftInYear = daysInYear - dayOfYear;
const daysInQuarter = daysInYear / 4;
const currentMonth = Math.floor(date.getMonth() / 3) + 2;
const currentQuarter = Math.floor((date.getMonth() + 3) / 3);
const percentThruYear = ((100 * dayOfYear) / daysInYear).toFixed(1);
let quarterEnd = new Date(date);
quarterEnd.setMonth(
quarterEnd.getMonth() + 3 - (quarterEnd.getMonth() % 3),
0
);
const daysLeftInQuarter = Math.floor((quarterEnd - date) / 8.64e7);
const percentThruQuarter = Number(
(100 * (daysInQuarter - daysLeftInQuarter)) / daysInQuarter
).toFixed(1);
const janFirst = new Date(date.getFullYear(), 0, 1);
const weekNumber = Math.ceil(
((date - janFirst) / 86400000 + janFirst.getDay()) / 7
);
const outputLine1 = `# Day of ${dayOfWeek}: ${formattedDate}`;
const outputLine2 = `## Week ${weekNumber}/52: Day ${dayOfYear} of ${daysInYear} (${daysLeftInYear} left)`;
const outputLine3 = `### ${percentThruYear}% thru ${year}; ${percentThruQuarter}% thru Q${currentQuarter}`;
console.log(`${outputLine1}\n${outputLine2}\n${outputLine3}`);
})();
@crates
Copy link
Author

crates commented Mar 19, 2020

I use this Javascript code to update a daily log of my current activities. This generates the header to those journal entries.

@crates
Copy link
Author

crates commented Mar 19, 2020

Here it is in action: https://codepen.io/cr8s/full/QWbBNwZ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment