made-data-converters
Version:
This package includes:
87 lines • 3.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.setTime = exports.endOfDay = exports.startOfDay = exports.toNearestHour = exports.toDaySuffix = exports.toCurrentAge = void 0;
/**
* Gets the current age in years based on the specified starting date and today's date.
* @param {Date} startingDate - The starting date.
* @return {number} A numeric value representing the age in years.
*/
function toCurrentAge(startingDate) {
const currentDate = new Date();
var yearDiff = currentDate.getFullYear() - startingDate.getFullYear();
var compareDate = new Date(startingDate.setFullYear(startingDate.getFullYear() + yearDiff));
if (currentDate < compareDate) {
yearDiff--;
}
return yearDiff;
}
exports.toCurrentAge = toCurrentAge;
/**
* Gets the day suffix for the specified date, i.e. st, nd, rd, or th.
* @param {Date} date - The date to get a day suffix for.
* @return {string} The day suffix as a string.
*/
function toDaySuffix(date) {
const day = date.getDate();
switch (day) {
case 1:
case 21:
case 31:
return "st";
case 2:
case 22:
return "nd";
case 3:
case 23:
return "rd";
default:
return "th";
}
}
exports.toDaySuffix = toDaySuffix;
/**
* Rounds the date value to its nearest hour determined by the half hour of each hour.
* @param {Date} date - The date to round to the nearest hour.
* @return {Date} The date rounded to the nearest whole hour.
*/
function toNearestHour(date) {
var hour = date.getMinutes() < 30
? date.getHours()
: date.getHours() + 1;
return hour == 24
? new Date(date.setHours(0, 0, 0, 0))
: new Date(date.setHours(hour, 0, 0, 0));
}
exports.toNearestHour = toNearestHour;
/**
* Gets the start of the day represented by the specified date.
* @param {Date} date - The date.
* @return {Date} A new date with the same date with the time set to midnight.
*/
function startOfDay(date) {
return new Date(date.setHours(0, 0, 0, 0));
}
exports.startOfDay = startOfDay;
/**
* Gets the end of the day represented by the specified date.
* @param {Date} date - The date.
* @return {Date} A new date with the same date with the time set to just before midnight of the next day.
*/
function endOfDay(date) {
return new Date(date.setHours(23, 59, 59, 999));
}
exports.endOfDay = endOfDay;
/**
* Sets the time value of a date.
* @param {Date} date - The date value to set the time of.
* @param {number} hours - The hours to set.
* @param {number} minutes - The minutes to set.
* @param {number} seconds - The seconds to set.
* @param {number} milliseconds - The milliseconds to set.
* @return {Date} A new date with the time set.
*/
function setTime(date, hours, minutes, seconds, milliseconds) {
return new Date(date.setHours(hours, minutes, seconds, milliseconds));
}
exports.setTime = setTime;
//# sourceMappingURL=index.js.map