more date fixes

This commit is contained in:
Dobromir Popov
2024-05-05 22:39:22 +02:00
parent 1394c5ba4d
commit 27a65b7dc0
4 changed files with 76 additions and 23 deletions

View File

@ -370,6 +370,8 @@ exports.getDateFormatedShort = function (date) {
}
/*Todo: remove:
toISOString
slice(0, 10)
@ -527,7 +529,7 @@ exports.fuzzySearch = function (publishers, searchQuery, distanceThreshold = 0.9
}
exports.getCurrentNonthFormatted = function () {
exports.getCurrentMonthFormatted = function () {
const getCurrentYearMonth = () => {
const currentDate = new Date();
const year = currentDate.getFullYear();
@ -546,11 +548,28 @@ exports.getCurrentYearMonth = () => {
// new date FNs
// Utility to handle date parsing consistently
/**
* Parses an input into a Luxon DateTime object, setting the timezone to 'Europe/Sofia' while keeping the local time.
* @param {string|Date} input - The input date string or JavaScript Date object.
* @returns {DateTime} - A Luxon DateTime object with the timezone set to 'Europe/Sofia', preserving the local time.
*/
const parseDate = (input) => {
return (typeof input === 'string' || input instanceof Date)
? DateTime.fromJSDate(new Date(input), { zone: 'Europe/Sofia' })
: DateTime.now({ zone: 'Europe/Sofia' });
let dateTime;
if (input instanceof DateTime) {
// If input is already a Luxon DateTime, we adjust the zone only.
dateTime = input.setZone('Europe/Sofia');
} else if (typeof input === 'string' || input instanceof Date) {
// Create a DateTime from the input assuming local timezone to preserve local time when changing the zone.
dateTime = DateTime.fromJSDate(new Date(input), { zone: 'local' });
dateTime = dateTime.setZone('Europe/Sofia');
} else {
// Use the current time if no input is given, considered as local time.
dateTime = DateTime.local().setZone('Europe/Sofia');
}
// Set the timezone to 'Europe/Sofia' while keeping the original local time.
return dateTime.setZone('Europe/Sofia', { keepLocalTime: true });
};
exports.parseDate = parseDate;
@ -560,6 +579,18 @@ exports.setTimezone = (input) => {
dateTime = dateTime.setZone('Europe/Sofia', { keepLocalTime: true });
return dateTime.toJSDate();
};
exports.setTime = (baseDateTime, timeDateTime) => {
// Ensure both inputs are DateTime objects
baseDateTime = parseDate(baseDateTime);
timeDateTime = parseDate(timeDateTime);
return baseDateTime.set({
hour: timeDateTime.hour,
minute: timeDateTime.minute,
second: timeDateTime.second,
millisecond: timeDateTime.millisecond
});
};
// Format date to a specified format, defaulting to 'HH:mm'
exports.getTimeFormatted = (input, format = 'HH:mm') => {
@ -571,7 +602,7 @@ exports.getTimeFormatted = (input, format = 'HH:mm') => {
exports.setTimeHHmm = (input, timeString) => {
let dateTime = parseDate(input);
const [hour, minute] = timeString.split(':').map(Number);
dateTime = dateTime.set({ hour, minute, second: 0, millisecond: 0 });
dateTime = dateTime.set({ hour, minute });
return dateTime.toJSDate();
};
@ -579,9 +610,25 @@ exports.setTimeHHmm = (input, timeString) => {
exports.parseTimeHHmm = (timeString) => {
const dateTime = DateTime.now({ zone: 'Europe/Sofia' });
const [hour, minute] = timeString.split(':').map(Number);
return dateTime.set({ hour, minute, second: 0, millisecond: 0 }).toJSDate();
return dateTime.set({ hour, minute }).toJSDate();
};
function isTimeBetween(startTime, endTime, checkTime) {
const start = new Date(0, 0, 0, startTime.getHours(), startTime.getMinutes());
const end = new Date(0, 0, 0, endTime.getHours(), endTime.getMinutes());
const check = new Date(0, 0, 0, checkTime.getHours(), checkTime.getMinutes());
// If the end time is less than the start time, it means the time range spans midnight
if (end < start) {
// Check time is between start and midnight or between midnight and end
return (check >= start && check <= new Date(0, 0, 1, 0, 0, 0)) || (check >= new Date(0, 0, 0, 0, 0, 0) && check <= end);
} else {
return check >= start && check <= end;
}
}
exports.isTimeBetween = isTimeBetween;
// ToDo: update all uses of this function to use the new one
// exports.getTimeFormatted = function (date) {