Date Utilities
The date utilities module provides functions for parsing, formatting, and manipulating dates, datetimes, and times. Built on top ofdate-fns, these utilities simplify common date operations. Located at packages/loopar/core/global/date-utils.js.
Overview
All date utility functions support multiple input formats and provide consistent handling of invalid dates. They can:- Accept Date objects, date strings, or datetime strings
- Format dates using custom format strings or the “DB” shorthand for database formats
- Return null for invalid inputs instead of throwing errors
- Use
date-fnsformatting tokens
Functions
getDate
Parses and formats a date value.The date to parse. Can be:
- A Date object
- A date string (e.g., ‘2024-03-15’)
- A datetime string (e.g., ‘2024-03-15 10:30:00’)
- Omitted (defaults to current date)
Optional format string. If provided, returns a formatted string:
"DB"- Database format (yyyy-MM-dd)- Custom format using date-fns tokens (e.g., ‘MM/DD/YYYY’, ‘dd MMM yyyy’)
- If omitted, returns a Date object
- When
formatis omitted: Returns a Date object - When
formatis provided: Returns a formatted date string - Returns null if the date is null or invalid
Format Examples
- Database Format
- US Format
- European Format
- Long Format
getDateTime
Parses and formats a datetime value.The datetime to parse. Can be:
- A Date object
- A datetime string (e.g., ‘2024-03-15 14:30:00’)
- A date string (time will be 00:00:00)
- Omitted (defaults to current datetime)
Optional format string. If provided, returns a formatted string:
"DB"- Database format (yyyy-MM-dd HH:mm:ss)- Custom format using date-fns tokens (e.g., ‘MM/DD/YYYY HH:mm’, ‘yyyy-MM-dd’T’HH:mm:ss’)
- If omitted, returns a Date object
- When
formatis omitted: Returns a Date object - When
formatis provided: Returns a formatted datetime string - Returns null if the date is null or invalid
Format Examples
- Database Format
- 12-Hour Format
- ISO Format
- Custom Format
getTime
Parses and formats a time value.The time to parse. Can be:
- A Date object (time portion will be extracted)
- A time string (e.g., ‘14:30:00’, ‘14:30’)
- A datetime string (time portion will be extracted)
- Omitted (defaults to current time)
Optional format string. If provided, returns a formatted string:
"DB"- Database format (HH:mm:ss)- Custom format using date-fns tokens (e.g., ‘HH:mm’, ‘hh:mm A’)
- If omitted, returns a Date object
- When
formatis omitted: Returns a Date object with today’s date and the specified time - When
formatis provided: Returns a formatted time string - Returns null if the time is null or invalid
- Note: Seconds are always set to “00” as the time widget doesn’t control seconds yet
Time Parsing Behavior
ThegetTime function has special parsing logic:
-
Time string input (e.g., “14:30” or “14:30:00”):
- Creates a Date object with today’s date
- Sets hours and minutes from the string
- Sets seconds to “00”
-
Date object input:
- Extracts the time portion
- Sets seconds to “00”
-
Invalid input:
- Returns null
The seconds are automatically set to “00” because the Loopar time widget doesn’t currently support seconds input. This ensures consistency across the framework.
Format Examples
- Database Format
- 24-Hour Format
- 12-Hour Format
- Short 12-Hour
Common Use Cases
Saving to Database
Use the “DB” format shorthand for database operations:Displaying to Users
Format dates for user-friendly display:Working with Form Inputs
Parse user input and convert between formats:Error Handling
All functions return null for invalid inputs:Date-fns Format Tokens
Common format tokens used with these functions:| Token | Description | Example |
|---|---|---|
yyyy | 4-digit year | 2024 |
yy | 2-digit year | 24 |
MMMM | Full month name | March |
MMM | Short month name | Mar |
MM | 2-digit month | 03 |
M | Month number | 3 |
dd | 2-digit day | 15 |
d | Day number | 15 |
do | Day with ordinal | 15th |
HH | 24-hour (2 digits) | 14 |
H | 24-hour | 14 |
hh | 12-hour (2 digits) | 02 |
h | 12-hour | 2 |
mm | Minutes (2 digits) | 30 |
ss | Seconds (2 digits) | 00 |
A | Uppercase AM/PM | PM |
a | Lowercase am/pm | pm |
For the complete list of format tokens, refer to the date-fns documentation.