UNPKG

field-datetime-new

Version:

A Blockly date picker field that uses the browser's date picker.

62 lines (55 loc) 2.01 kB
// /** // * Get the string formatted locally to the user. // * // * @param dateString A string in the format 'yyyy-mm-dd' // * @returns the locale date string for the date. // */ // export function getLocaleDateTimeString(dateString: string): string { // const date = new Date(dateString); // // Set the language/locale, defaulting to 'en-US' if not available // const language = navigator.language ?? 'en-US'; // // Format both date and time with options // return new Intl.DateTimeFormat(language, { // year: 'numeric', // month: '2-digit', // day: '2-digit', // hour: '2-digit', // minute: '2-digit', // second: '2-digit', // hour12: false, // Optional: Use 24-hour time. Set to `true` for 12-hour time with AM/PM. // }).format(date); // } /** * Get the string formatted locally to the user. * * @param dateString A string in the format 'yyyy-mm-dd' or 'yyyy-mm-ddTHH:MM:SS' * @returns the locale date string for the date. */ export function getLocaleDateTimeString(dateString: string): string { const date = new Date(dateString); const language = navigator.language ?? 'en-US'; // Check if the date is invalid if (isNaN(date.getTime())) { // console.error('Invalid date value:', dateString); return new Intl.DateTimeFormat(language, { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: true, // Use 12-hour time with AM/PM }).format(new Date()); // Return an empty string or a fallback value } // Set the language/locale, defaulting to 'en-US' if not available // Format both date and time with options return new Intl.DateTimeFormat(language, { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: true, // Use 12-hour time with AM/PM }).format(date); }