48 lines
1.3 KiB
JavaScript
48 lines
1.3 KiB
JavaScript
// js/utils/time-utils.js
|
|
|
|
// Helper function to convert time string to minutes since midnight
|
|
function timeToMinutes(timeStr) {
|
|
const match = timeStr.match(/([0-9]+)(?::([0-9]+))?(am|pm)/i);
|
|
if (!match) return 0;
|
|
|
|
let hours = parseInt(match[1]);
|
|
const minutes = parseInt(match[2] || '0');
|
|
const meridian = match[3].toLowerCase();
|
|
|
|
if (meridian === 'pm' && hours !== 12) {
|
|
hours += 12;
|
|
} else if (meridian === 'am' && hours === 12) {
|
|
hours = 0;
|
|
}
|
|
|
|
return hours * 60 + minutes;
|
|
}
|
|
|
|
// Helper function to ensure consistent time format
|
|
function formatTimeSlot(hour) {
|
|
const meridian = hour >= 12 ? 'pm' : 'am';
|
|
const displayHour = hour > 12 ? hour - 12 : (hour === 0 ? 12 : hour);
|
|
return `${displayHour}:00${meridian}`;
|
|
}
|
|
|
|
// Format date as YYYY-MM-DD
|
|
function formatDate(date) {
|
|
const year = date.getFullYear();
|
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
const day = String(date.getDate()).padStart(2, '0');
|
|
return `${year}-${month}-${day}`;
|
|
}
|
|
|
|
// Parse date string to Date object
|
|
function parseDate(dateStr) {
|
|
const [year, month, day] = dateStr.split('-').map(Number);
|
|
return new Date(year, month - 1, day);
|
|
}
|
|
|
|
// Export functions for use in other files
|
|
export {
|
|
timeToMinutes,
|
|
formatTimeSlot,
|
|
formatDate,
|
|
parseDate
|
|
}; |