Skip to content

Useful functions

Formatierung verschiedenster Datum- und Zeitangaben

Section titled “Formatierung verschiedenster Datum- und Zeitangaben”
const today = new Date();
function formatDate(myDate) {
const weekdays = [
{ long: 'Sonntag', short: 'So' },
{ long: 'Montag', short: 'Mo' },
{ long: 'Dienstag', short: 'Di' },
{ long: 'Mittwoch', short: 'Mi' },
{ long: 'Donnerstag', short: 'Do' },
{ long: 'Freitag', short: 'Fr' },
{ long: 'Samstag', short: 'Sa' },
];
const monthsOfTheYear = [
{ number: '01', long: 'Jänner', short: 'Jän' },
{ number: '02', long: 'Februar', short: 'Feb' },
{ number: '03', long: 'März', short: 'Mär' },
{ number: '04', long: 'April', short: 'Apr' },
{ number: '05', long: 'Mai', short: 'Mai' },
{ number: '06', long: 'Juni', short: 'Jun' },
{ number: '07', long: 'Juli', short: 'Jul' },
{ number: '08', long: 'August', short: 'Aug' },
{ number: '09', long: 'September', short: 'Sep' },
{ number: '10', long: 'Oktober', short: 'Okt' },
{ number: '11', long: 'November', short: 'Nov' },
{ number: '12', long: 'Dezember', short: 'Dez' },
];
const day = today.getDate();
const year = today.getFullYear();
const weekdayLong = weekdays[today.getDay()].long;
const weekdayShort = weekdays[today.getDay()].short;
const monthLong = monthsOfTheYear[today.getMonth()].long;
const monthShort = monthsOfTheYear[today.getMonth()].short;
const monthNumber = monthsOfTheYear[today.getMonth()].number;
return `Heute ist ${weekdayLong}, der ${day}. ${monthLong} ${year}.`;
}
console.log(formatDate(today));
function isSameTime(time1, time2) {
return time1.getTime() === time2.getTime();
}
function isSameDate(date1, date2) {
return (
date1.getFullYear() === date2.getFullYear() &&
date1.getMonth() === date2.getMonth() &&
date1.getDate() === date2.getDate());
}
function howManyDays(date) {
return parseInt((Date.now() - date.getTime())/1000/60/60/24);
}
const date = new Date(2025, 1, 2);
console.log(howManyDays(date));