Last active
August 24, 2023 12:46
-
-
Save Suppenterrine/b951af64656f911869b9d0d9463543d7 to your computer and use it in GitHub Desktop.
JS Date from today and with the first and the last day of the specified month
This file contains 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
const fs = require('fs'); | |
class MonthRange { | |
/** | |
* @param {number} range - Range of months. | |
* @param {string} language - Language code. | |
* @param {string} hemisphere - Hemisphere. | |
* @param {Date} startDate - Start date. | |
* @param {Date} endDate - End date. | |
* @returns {object} - Object with month details. | |
* @example const monthRange = new MonthRange(4); // const monthRange = new MonthRange(4, 'en-GB', 'southern'); | |
*/ | |
constructor(range = 0, language = 'de-DE', hemisphere = 'northern', startDate = new Date(), endDate = new Date('2020-12-31')) { | |
this.range = range; | |
this.startDate = startDate; | |
this.endDate = endDate; | |
this.language = language; | |
this.hemisphere = hemisphere; | |
this.translations = JSON.parse(fs.readFileSync('translations.json'))[language]; | |
} | |
getRelativeDate(date) { | |
const diffInDays = Math.round((this.startDate - date) / (1000 * 60 * 60 * 24)); | |
const isCurrentMonth = this.startDate.getMonth() === date.getMonth() && this.startDate.getFullYear() === date.getFullYear(); | |
if (isCurrentMonth) return this.translations.relativeDates.thisMonth; | |
if (diffInDays < 30) return this.translations.relativeDates.daysAgo.replace('{days}', diffInDays); | |
if (diffInDays < 60) return this.translations.relativeDates.lastMonth; | |
return this.translations.relativeDates.monthsAgo.replace('{months}', Math.round(diffInDays / 30)); | |
} | |
generateMonths() { | |
const result = []; | |
for (let i = 0; i < this.range; i++) { | |
const completeMonth = new Date(this.startDate.getFullYear(), this.startDate.getMonth() - i); | |
const sortDate = new Date(completeMonth.getFullYear(), completeMonth.getMonth() + 1); // Create a copy for sorting | |
if (completeMonth > this.endDate) { | |
const firstDay = `${completeMonth.getFullYear()}-${String(completeMonth.getMonth() + 1).padStart(2, '0')}-01`; | |
const lastDay = new Date(Date.UTC(completeMonth.getFullYear(), completeMonth.getMonth() + 1, 0)).toISOString().slice(0, 10); | |
const currentMonthName = new Intl.DateTimeFormat(this.language, { month: 'long' }).format(completeMonth); | |
const totalDays = (new Date(completeMonth.getFullYear(), completeMonth.getMonth() + 1, 0)).getDate(); | |
const firstDayName = new Intl.DateTimeFormat(this.language, { weekday: 'long' }).format(new Date(firstDay)); | |
const lastDayName = new Intl.DateTimeFormat(this.language, { weekday: 'long' }).format(new Date(lastDay)); | |
// Determine the season (Northern Hemisphere) | |
const season = this.getSeason(completeMonth); | |
// Calculate the number of weekends | |
const weekendDays = this.getWeekendCount(completeMonth); | |
const weekendPairs = this.getWeekendCount(completeMonth) / 2; | |
result.push({ | |
sortDate: sortDate, | |
month: currentMonthName.charAt(0).toUpperCase() + currentMonthName.slice(1), | |
year: completeMonth.getFullYear(), | |
days: { first: firstDay, last: lastDay, today: this.startDate.toISOString().slice(0, 10), totalDays, firstDayName, lastDayName }, | |
relativeDate: this.getRelativeDate(completeMonth), | |
season, | |
weekendDays, | |
weekendPairs, | |
isLeapYear: (completeMonth.getFullYear() % 4 === 0 && (completeMonth.getFullYear() % 100 !== 0 || completeMonth.getFullYear() % 400 === 0)) | |
}); | |
} | |
} | |
return result; | |
} | |
getSeason(date) { | |
const month = date.getMonth(); | |
if (this.hemisphere === 'northern') { | |
// Determine the season in English | |
let season = "Winter"; | |
if (month < 2 || month === 11) season = 'Winter'; | |
if (month < 5) season = 'Spring'; | |
if (month < 8) season = 'Summer'; | |
if (month < 11) season = 'Autumn'; | |
// Translate to the chosen language | |
return this.translations.seasons[season]; | |
} else { | |
// Determine the season in English | |
let season = "Spring"; | |
if (month < 2 || month === 11) season = 'Summer'; | |
if (month < 5) season = 'Autumn'; | |
if (month < 8) season = 'Winter'; | |
// Translate to the chosen language | |
return this.translations.seasons[season]; | |
} | |
} | |
getWeekendCount(date) { | |
let count = 0; | |
for (let i = 1; i <= (new Date(date.getFullYear(), date.getMonth() + 1, 0)).getDate(); i++) { | |
const dayOfWeek = new Date(date.getFullYear(), date.getMonth(), i).getDay(); | |
if (dayOfWeek === 0 || dayOfWeek === 6) count++; | |
} | |
return count; | |
} | |
getMonthDetails(targetDate) { | |
const completeMonth = targetDate; | |
const firstDay = `${completeMonth.getFullYear()}-${String(completeMonth.getMonth() + 1).padStart(2, '0')}-01`; | |
const lastDay = new Date(Date.UTC(completeMonth.getFullYear(), completeMonth.getMonth() + 1, 0)).toISOString().slice(0, 10); | |
const currentMonthName = new Intl.DateTimeFormat(this.language, { month: 'long' }).format(completeMonth); | |
const totalDays = (new Date(completeMonth.getFullYear(), completeMonth.getMonth() + 1, 0)).getDate(); | |
const firstDayName = new Intl.DateTimeFormat(this.language, { weekday: 'long' }).format(new Date(firstDay)); | |
const lastDayName = new Intl.DateTimeFormat(this.language, { weekday: 'long' }).format(new Date(lastDay)); | |
const season = this.getSeason(completeMonth); | |
const weekendDays = this.getWeekendCount(completeMonth); | |
const weekendPairs = this.getWeekendCount(completeMonth) / 2; | |
return { | |
sortDate: completeMonth, | |
month: currentMonthName.charAt(0).toUpperCase() + currentMonthName.slice(1), | |
year: completeMonth.getFullYear(), | |
days: { first: firstDay, last: lastDay, today: this.startDate.toISOString().slice(0, 10), totalDays, firstDayName, lastDayName }, | |
relativeDate: this.getRelativeDate(completeMonth), | |
season, | |
weekendDays, | |
weekendPairs, | |
isLeapYear: (completeMonth.getFullYear() % 4 === 0 && (completeMonth.getFullYear() % 100 !== 0 || completeMonth.getFullYear() % 400 === 0)) | |
}; | |
} | |
} | |
const monthRange = new MonthRange(4); | |
// const monthRange = new MonthRange(4, 'en-GB', 'southern'); // englische version | |
const months = monthRange.generateMonths(); | |
console.log(months); | |
const specificDateDetails = monthRange.getMonthDetails(new Date('2023-08-02')); | |
console.log("\nDETAILS\n======="); | |
console.log(specificDateDetails); | |
const languageFile = JSON.parse(fs.readFileSync('translations.json')); | |
const keys = Object.keys(languageFile); | |
console.log("\nPOSSIBLE LANGUAGES\n=================="); | |
console.log(keys); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment