A few useful extensions for the javascript object Date
- Js-Date-Extensions
Set the language
// example
Date.setLocale('de');
Fetches the time units in milliseconds
@return {object}
// example
Date.getUnits();
// { year: 31536000000, month: 2628000000, week: 604800000, day: 86400000, hour: 3600000, minute: 60000, second: 1000 }
@param {boolean} abbreviation Default false
@returns {string[]}
// example
Date.getDayNames(true); // [ "Mo", "Di", "Mi", "Do", "Fr", "Sa", "So" ]
@param {boolean} abbreviation Default false
@returns {string[]}
// example
Date.getMonthNames(true); // [ "Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"]
Adds a specified number of days to the date
@param {number} days
@return {Date}
// example
let date = new Date('2022-11-01');
date.addDays(4); // 2022-11-05
Subtracts a specified number of days from the date
@param {number} days
@returns {Date}
// example
let date = new Date('2022-11-01');
date.subDays(4); // 2022-10-28
Adds a specified number of months to the date
@param {number} months
@returns {Date}
// example
let date = new Date('2022-11-01');
date.addMonths(4); // 2023-03-01
Subtracts a specified number of months from the date
@param {number} months
@returns {Date}
// example
let date = new Date('2022-11-01');
date.subMonths(1); // 2022-10-01
@returns {Date}
// example
let date = new Date('2022-11-01');
let cloneDate = date.clone(); // 2022-11-01
Returns a new instance of the Date object (alias of clone
)
@returns {Date}
// example
let date = new Date('2022-11-01');
let copyDate = date.copy(); // 2022-11-01
Determine the first day of the current month
@returns {Date}
// example
let date = new Date('2022-11-10');
let firstOfMonth = date.getFirstDayOfMonth(); // 2022-11-01
Determine the last day of the current month
@returns {Date}
// example
let date = new Date('2022-11-10');
let firstOfMonth = date.getLastDayOfMonth(); // 2022-11-30
Determine the previous Monday of the current date
@returns {Date}
// example
let date = new Date('2022-11-10');
let prevMonday = date.getFirstDayOfWeek(); // 2022-11-07
Determine the Sunday of the current date
@returns {Date}
// example
let date = new Date('2022-11-10');
let nextSunday = date.getLastDayOfWeek(); // 2022-11-13
Checks if the year of the date is a leap year
@returns {boolean}
// example
let date = new Date('2024-11-01');
if (date.isLeapYear()) // true
{
// do something
}
Checks the date for a weekday
@returns {boolean}
// example
let date = new Date('2022-11-01');
date.isMonday(); // false
date.isTuesday(); // true
date.isWednesday(); // false
date.isThursday(); // false
date.isFriday(); // false
date.isSaturday(); // false
date.isSunday(); // false
date.isWeekend(); // false
Determine the number of days in the current month
@returns {number}
// example
let date = new Date('2022-11-01');
date.getDaysInMonth(); // 30
Determines the calendar week of the date
@returns {number}
// example
let date = new Date('2022-11-01');
date.getWeek(); // 44
Determines the number of weeks between two dates
@param {Date} toDate
@returns {number}
// example
let date = new Date('2022-11-01');
let toDate = new Date('2022-11-14');
date.getCountWeeks(toDate); // 2
Determines the number of days between two dates
@param {Date} toDate
@returns {number}
// example
let date = new Date('2022-11-01');
let toDate = new Date('2022-11-14');
date.getCountDays(toDate); // 14
let date = new Date('2022-11-06 23:54:00');
// Now: 2022-11-07 05:55:00
date.fromNow(); // vor 6 Stunden
let date = new Date('2022-11-06 23:54:00');
// Now: 2022-11-07 05:55:00
date.getDayName(); // Sonntag
let date = new Date('2022-11-06 23:54:00');
// Now: 2022-11-07 05:55:00
date.getMonthName(); // November
Returns all data of one month as array
@return {*[]}
// example
let date = new Date('2022-11-01');
let result = date.getMonthCalendar();
// output result
[
{
"week": 44,
"days": [
"2022-10-31T23:00:00.000Z",
"2022-11-01T23:00:00.000Z",
"2022-11-02T23:00:00.000Z",
"2022-11-03T23:00:00.000Z",
"2022-11-04T23:00:00.000Z",
"2022-11-05T23:00:00.000Z",
"2022-11-06T23:00:00.000Z"
]
},
{
"week": 45,
"days": [
"2022-11-07T23:00:00.000Z",
"2022-11-08T23:00:00.000Z",
"2022-11-09T23:00:00.000Z",
"2022-11-10T23:00:00.000Z",
"2022-11-11T23:00:00.000Z",
"2022-11-12T23:00:00.000Z",
"2022-11-13T23:00:00.000Z"
]
},
{
"week": 46,
"days": [
"2022-11-14T23:00:00.000Z",
"2022-11-15T23:00:00.000Z",
"2022-11-16T23:00:00.000Z",
"2022-11-17T23:00:00.000Z",
"2022-11-18T23:00:00.000Z",
"2022-11-19T23:00:00.000Z",
"2022-11-20T23:00:00.000Z"
]
},
{
"week": 47,
"days": [
"2022-11-21T23:00:00.000Z",
"2022-11-22T23:00:00.000Z",
"2022-11-23T23:00:00.000Z",
"2022-11-24T23:00:00.000Z",
"2022-11-25T23:00:00.000Z",
"2022-11-26T23:00:00.000Z",
"2022-11-27T23:00:00.000Z"
]
},
{
"week": 48,
"days": [
"2022-11-28T23:00:00.000Z",
"2022-11-29T23:00:00.000Z",
"2022-11-30T23:00:00.000Z",
"2022-12-01T23:00:00.000Z",
"2022-12-02T23:00:00.000Z",
"2022-12-03T23:00:00.000Z",
"2022-12-04T23:00:00.000Z"
]
}
]
Returns all data of one week as array
@return {*[]}
// example
let date = new Date('2023-02-09');
let result = date.getWeekCalendar();
// output result
[
"2023-02-06T23:00:00.000Z",
"2023-02-07T23:00:00.000Z",
"2023-02-08T23:00:00.000Z",
"2023-02-09T23:00:00.000Z",
"2023-02-10T23:00:00.000Z",
"2023-02-11T23:00:00.000Z",
"2023-02-12T23:00:00.000Z",
]