77ea27f5f5
Co-authored-by: Nahiyan Khan <nahiyan@squareup.com> Co-authored-by: Taylor Ho <taylorkmho@gmail.com> Co-authored-by: Lily Delalande <119957291+lily-de@users.noreply.github.com> Co-authored-by: Spence <spencrmartin@gmail.com> Co-authored-by: spencrmartin <spencermartin@squareup.com> Co-authored-by: Judson Stephenson <Jud@users.noreply.github.com> Co-authored-by: Max Novich <mnovich@squareup.com> Co-authored-by: Best Codes <106822363+The-Best-Codes@users.noreply.github.com> Co-authored-by: caroline-a-mckenzie <cmckenzie@squareup.com> Co-authored-by: Michael Neale <michael.neale@gmail.com>
58 lines
1.4 KiB
TypeScript
58 lines
1.4 KiB
TypeScript
export const formatToLocalDateTime = (dateString?: string | null): string => {
|
|
if (!dateString) {
|
|
return 'N/A';
|
|
}
|
|
try {
|
|
const date = new Date(dateString);
|
|
// Check if the date is valid
|
|
if (isNaN(date.getTime())) {
|
|
return 'Invalid Date';
|
|
}
|
|
return date.toLocaleString(); // Uses user's locale and timezone
|
|
} catch (e) {
|
|
console.error('Error formatting date:', e);
|
|
return 'Invalid Date';
|
|
}
|
|
};
|
|
|
|
export const formatDate = (dateString?: string | null): string => {
|
|
if (!dateString) {
|
|
return 'N/A';
|
|
}
|
|
try {
|
|
const date = new Date(dateString);
|
|
if (isNaN(date.getTime())) {
|
|
return 'Invalid Date';
|
|
}
|
|
return date.toLocaleDateString(); // Uses user's locale
|
|
} catch (e) {
|
|
console.error('Error formatting date:', e);
|
|
return 'Invalid Date';
|
|
}
|
|
};
|
|
|
|
export const formatToLocalDateWithTimezone = (dateString?: string | null): string => {
|
|
if (!dateString) {
|
|
return 'N/A';
|
|
}
|
|
try {
|
|
const date = new Date(dateString);
|
|
if (isNaN(date.getTime())) {
|
|
return 'Invalid Date';
|
|
}
|
|
// Format: Jan 1, 2023, 10:00:00 AM PST (example)
|
|
return date.toLocaleString(undefined, {
|
|
year: 'numeric',
|
|
month: 'short',
|
|
day: 'numeric',
|
|
hour: 'numeric',
|
|
minute: '2-digit',
|
|
second: '2-digit',
|
|
timeZoneName: 'short',
|
|
});
|
|
} catch (e) {
|
|
console.error('Error formatting date with timezone:', e);
|
|
return 'Invalid Date';
|
|
}
|
|
};
|