Add utility functions for date formatting, time rounding, and notifications
- Implemented functions to format dates between YYYY-MM-DD and DD.MM.YYYY - Added functions to get today's date in ISO format - Created functions to round time to the nearest 15 minutes - Developed a function to format time as HH:MM - Added a function to format duration in HH:MM:SS - Implemented a toast notification system with auto-remove functionality - Added functions to get day of the week and month names in German
This commit is contained in:
126
public/js/utils.js
Normal file
126
public/js/utils.js
Normal file
@@ -0,0 +1,126 @@
|
||||
/**
|
||||
* Utility Functions
|
||||
* Date/Time formatting, rounding, duration formatting
|
||||
*/
|
||||
|
||||
/**
|
||||
* Format date from YYYY-MM-DD to DD.MM.YYYY
|
||||
*/
|
||||
function formatDateDisplay(dateStr) {
|
||||
const [year, month, day] = dateStr.split('-');
|
||||
return `${day}.${month}.${year}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format date from DD.MM.YYYY to YYYY-MM-DD
|
||||
*/
|
||||
function formatDateISO(dateStr) {
|
||||
const [day, month, year] = dateStr.split('.');
|
||||
return `${year}-${month}-${day}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get today's date in YYYY-MM-DD format
|
||||
*/
|
||||
function getTodayISO() {
|
||||
const today = new Date();
|
||||
const year = today.getFullYear();
|
||||
const month = String(today.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(today.getDate()).padStart(2, '0');
|
||||
return `${year}-${month}-${day}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Round time down to nearest 15 minutes
|
||||
*/
|
||||
function roundDownTo15Min(date) {
|
||||
const minutes = date.getMinutes();
|
||||
const roundedMinutes = Math.floor(minutes / 15) * 15;
|
||||
date.setMinutes(roundedMinutes);
|
||||
date.setSeconds(0);
|
||||
date.setMilliseconds(0);
|
||||
return date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Round time up to nearest 15 minutes
|
||||
*/
|
||||
function roundUpTo15Min(date) {
|
||||
const minutes = date.getMinutes();
|
||||
const roundedMinutes = Math.ceil(minutes / 15) * 15;
|
||||
date.setMinutes(roundedMinutes);
|
||||
date.setSeconds(0);
|
||||
date.setMilliseconds(0);
|
||||
return date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format time as HH:MM
|
||||
*/
|
||||
function formatTime(date) {
|
||||
const hours = String(date.getHours()).padStart(2, '0');
|
||||
const minutes = String(date.getMinutes()).padStart(2, '0');
|
||||
return `${hours}:${minutes}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format seconds to HH:MM:SS
|
||||
*/
|
||||
function formatDuration(seconds) {
|
||||
const hrs = Math.floor(seconds / 3600);
|
||||
const mins = Math.floor((seconds % 3600) / 60);
|
||||
const secs = seconds % 60;
|
||||
return `${String(hrs).padStart(2, '0')}:${String(mins).padStart(2, '0')}:${String(secs).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show toast notification
|
||||
*/
|
||||
function showNotification(message, type = 'info') {
|
||||
const container = document.getElementById('toastContainer');
|
||||
|
||||
// Create toast element
|
||||
const toast = document.createElement('div');
|
||||
toast.className = `toast toast-${type}`;
|
||||
|
||||
// Icon based on type
|
||||
const icons = {
|
||||
success: '✓',
|
||||
error: '✕',
|
||||
info: 'ℹ'
|
||||
};
|
||||
|
||||
toast.innerHTML = `
|
||||
<span class="toast-icon">${icons[type] || 'ℹ'}</span>
|
||||
<span>${message}</span>
|
||||
`;
|
||||
|
||||
container.appendChild(toast);
|
||||
|
||||
// Auto-remove after 3 seconds
|
||||
setTimeout(() => {
|
||||
toast.classList.add('hiding');
|
||||
setTimeout(() => {
|
||||
container.removeChild(toast);
|
||||
}, 300);
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get day of week abbreviation in German
|
||||
*/
|
||||
function getDayOfWeek(date) {
|
||||
const days = ['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'];
|
||||
return days[date.getDay()];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get month name in German
|
||||
*/
|
||||
function getMonthName(month) {
|
||||
const months = [
|
||||
'Januar', 'Februar', 'März', 'April', 'Mai', 'Juni',
|
||||
'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'
|
||||
];
|
||||
return months[month];
|
||||
}
|
||||
Reference in New Issue
Block a user