date.ts
Overview
The date.ts file provides a collection of utility functions for formatting and manipulating dates and times using the dayjs library. It centralizes common date/time formatting patterns and relative date calculations to ensure consistent date handling across the application. The functions convert input dates into standardized string representations suitable for display or further processing.
Functions
formatDate(date: any): string
Formats the provided date into the string format "DD/MM/YYYY HH:mm:ss".
Parameters:
date(any): The date value to format. It can be aDateobject, a string, or any value recognized bydayjs.
Returns: A formatted date-time string (e.g.,
"31/12/2023 23:59:59") or an empty string ifdateis falsy.Usage example:
formatDate(new Date()); // "27/04/2024 14:23:45" formatDate('2024-04-27T14:23:45Z'); // "27/04/2024 14:23:45" formatDate(null); // ""
formatTime(date: any): string
Formats the provided date or time into the string format "HH:mm:ss".
Parameters:
date(any): The date/time value to format.
Returns: A formatted time string (e.g.,
"14:23:45") or an empty string ifdateis falsy.Usage example:
formatTime(new Date()); // "14:23:45" formatTime('2024-04-27T14:23:45Z'); // "14:23:45" formatTime(undefined); // ""
today(): string
Returns the current date and time formatted as "DD/MM/YYYY HH:mm:ss".
Parameters: None
Returns: Current date-time string.
Usage example:
today(); // "27/04/2024 14:23:45"
lastDay(): string
Returns the date and time exactly one day before the current moment, formatted as "DD/MM/YYYY HH:mm:ss".
Parameters: None
Returns: Formatted date-time string for yesterday.
Usage example:
lastDay(); // "26/04/2024 14:23:45"
lastWeek(): string
Returns the date and time exactly one week before the current moment, formatted as "DD/MM/YYYY HH:mm:ss".
Parameters: None
Returns: Formatted date-time string for one week ago.
Usage example:
lastWeek(); // "20/04/2024 14:23:45"
formatPureDate(date: any): string
Formats the provided date into the string format "DD/MM/YYYY" (date only, no time).
Parameters:
date(any): The date value to format.
Returns: A formatted date string or empty string if
dateis falsy.Usage example:
formatPureDate(new Date()); // "27/04/2024" formatPureDate(null); // ""
formatStandardDate(date: any): string
Formats the provided date into the ISO-like string format "YYYY-MM-DD".
Parameters:
date(any): The date to format.
Returns: Formatted date string or empty string if input is falsy or invalid.
Implementation Detail: Uses
dayjs'sisValid()method to ensure the date is valid before formatting.Usage example:
formatStandardDate(new Date()); // "2024-04-27" formatStandardDate('invalid-date'); // ""
Important Implementation Details
The file relies on the lightweight
dayjslibrary for all date parsing and formatting operations.Each function first checks if the input date is truthy; if not, it returns an empty string to avoid errors.
formatStandardDateincludes an additional validity check, returning an empty string if the date is invalid.Relative date functions (
lastDay,lastWeek) usedayjs().subtract()to calculate past dates.The formatting strings follow common date/time display standards to ensure consistency.
Interaction with the System
This file acts as a utility module that can be imported wherever date/time formatting or relative date calculations are required.
It abstracts away the direct usage of
dayjsthroughout the application, promoting a consistent date/time format.Other modules or components responsible for displaying dates, logging timestamps, or performing date-based filtering likely depend on these functions.
Mermaid Diagram: Function Flowchart
flowchart TD
A[formatDate(date)] -->|uses| D[dayjs(date).format('DD/MM/YYYY HH:mm:ss')]
B[formatTime(date)] -->|uses| E[dayjs(date).format('HH:mm:ss')]
C[today()] -->|calls| A
F[lastDay()] -->|calls| A
G[lastWeek()] -->|calls| A
H[formatPureDate(date)] -->|uses| I[dayjs(date).format('DD/MM/YYYY')]
J[formatStandardDate(date)] -->|validates| K[dayjs(date).isValid()]
K -->|true| L[dayjs(date).format('YYYY-MM-DD')]
K -->|false| M[return '']
Summary
The date.ts file is a focused utility collection providing consistent and reusable date/time formatting and relative date calculations using the dayjs library. It encapsulates common patterns and safeguards against invalid or missing inputs, making it a dependable core utility module for date handling throughout the application.