50 lines
1.7 KiB
JavaScript
50 lines
1.7 KiB
JavaScript
|
|
/**
|
||
|
|
* Parse a SQLite timestamp (which is stored as UTC) into a JavaScript Date object.
|
||
|
|
* SQLite CURRENT_TIMESTAMP returns: "YYYY-MM-DD HH:MM:SS" in UTC
|
||
|
|
* But without a 'Z' suffix, JavaScript treats it as local time.
|
||
|
|
* This function ensures it's interpreted as UTC.
|
||
|
|
*
|
||
|
|
* @param {string} sqliteTimestamp - Timestamp from SQLite (e.g., "2025-10-30 14:30:00")
|
||
|
|
* @returns {Date} - JavaScript Date object
|
||
|
|
*/
|
||
|
|
export function parseUTCTimestamp(sqliteTimestamp) {
|
||
|
|
if (!sqliteTimestamp) return new Date();
|
||
|
|
|
||
|
|
// If timestamp already has 'Z' or timezone info, use it directly
|
||
|
|
if (sqliteTimestamp.includes('Z') || sqliteTimestamp.includes('+') || sqliteTimestamp.includes('T')) {
|
||
|
|
return new Date(sqliteTimestamp);
|
||
|
|
}
|
||
|
|
|
||
|
|
// SQLite format: "YYYY-MM-DD HH:MM:SS"
|
||
|
|
// Add 'Z' to explicitly mark it as UTC
|
||
|
|
return new Date(sqliteTimestamp + 'Z');
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Format a SQLite timestamp as a localized date string
|
||
|
|
* @param {string} sqliteTimestamp
|
||
|
|
* @returns {string} - Localized date (e.g., "10/30/2025")
|
||
|
|
*/
|
||
|
|
export function formatLocalDate(sqliteTimestamp) {
|
||
|
|
return parseUTCTimestamp(sqliteTimestamp).toLocaleDateString();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Format a SQLite timestamp as a localized time string
|
||
|
|
* @param {string} sqliteTimestamp
|
||
|
|
* @returns {string} - Localized time (e.g., "2:30:00 PM")
|
||
|
|
*/
|
||
|
|
export function formatLocalTime(sqliteTimestamp) {
|
||
|
|
return parseUTCTimestamp(sqliteTimestamp).toLocaleTimeString();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Format a SQLite timestamp as a localized date and time string
|
||
|
|
* @param {string} sqliteTimestamp
|
||
|
|
* @returns {string} - Localized date and time (e.g., "10/30/2025, 2:30:00 PM")
|
||
|
|
*/
|
||
|
|
export function formatLocalDateTime(sqliteTimestamp) {
|
||
|
|
return parseUTCTimestamp(sqliteTimestamp).toLocaleString();
|
||
|
|
}
|
||
|
|
|