time input: add formatTime helper

This commit is contained in:
Elijah Duffy
2025-07-22 11:28:35 -07:00
parent 026810b32d
commit 647235e1fe
2 changed files with 24 additions and 4 deletions

View File

@@ -1,3 +1,19 @@
<script lang="ts" module>
/**
* formatTime returns a human readable 12-hour based string.
*
* @param time The time to format, can be null or undefined.
* @param fallback Optional fallback string to return if time is null or undefined.
* If not provided, an empty string will be returned.
* If provided, it will be returned when time is null or undefined.
* @returns The time in 12-hour format or the fallback string if undefined.
*/
export const formatTime = (time: Time | null | undefined, fallback?: string): string => {
if (!time) return fallback ?? '';
return moment(time.toString(), 'HH:mm:ss').format('h:mm A');
};
</script>
<script lang="ts">
import { liveValidator, validate } from '@svelte-toolkit/validate';
import Label from './Label.svelte';
@@ -153,8 +169,7 @@
updateHiddenInput();
// update formatted value
const date = moment(value, 'HH:mm');
formattedValue = date.format('h:mm A');
formattedValue = formatTime(value);
};
/**