Last active
March 27, 2020 15:10
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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}`); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here it is in action: https://codepen.io/cr8s/full/QWbBNwZ