@dvsa/appdev-api-common
Version:
Utils library for common API functionality
165 lines (164 loc) • 5.62 kB
TypeScript
import dayjs from "dayjs";
type AcceptableDate = DateTime | string | Date | null;
/**
* DateTime utility class for handling dates with UK timezone support
*/
export declare class DateTime {
private instance;
private static readonly UKLocalDateTimeFormat;
private static readonly UKLocalDateFormat;
private static readonly UK_TIMEZONE;
/**
* Creates a new DateTime instance
* @param sourceDateTime - Initial date/time (string, Date, or another DateTime)
* @param format - Optional format string for parsing string dates
*/
constructor(sourceDateTime?: AcceptableDate, format?: string | undefined);
/**
* Converts to a JavaScript Date object
* @returns Date object
*/
toDate(): Date;
/**
* Creates a new DateTime instance from a date source
* @param sourceDateTime - Source date/time
* @param format - Optional format string for parsing
* @returns New DateTime instance or null if source is null
*/
static at(sourceDateTime: AcceptableDate, format?: string | undefined): DateTime | null;
/**
* Formats a date in UK local date time format (DD/MM/YYYY HH:mm:ss)
* @param sourceDateTime - Source date/time
* @returns Formatted string or null if source is null
*/
static StandardUkLocalDateTimeAdapter(sourceDateTime: AcceptableDate): string | null;
/**
* Formats a date in UK local date format (DD/MM/YYYY)
* @param sourceDateTime - Source date/time
* @returns Formatted string or null if source is null
*/
static StandardUkLocalDateAdapter(sourceDateTime: AcceptableDate): string | null;
/**
* Adds time to this DateTime instance (mutable operation)
* @param amount - Amount to add
* @param unit - Unit of time (day, month, year, etc.)
* @returns This instance for chaining
*/
add(amount: number, unit: dayjs.ManipulateType): DateTime;
/**
* Subtracts time from this DateTime instance (mutable operation)
* @param amount - Amount to subtract
* @param unit - Unit of time (day, month, year, etc.)
* @returns This instance for chaining
*/
subtract(amount: number, unit: dayjs.ManipulateType): DateTime;
/**
* Formats the date with a custom format string
* @param formatString - Format pattern
* @returns Formatted date string
*/
format(formatString: string): string;
/**
* Gets the day of week (0-6, Sunday is 0)
* @returns Day of week
*/
day(): number;
/**
* Sets the date to the start of a specified unit (mutable operation)
* @param unit - Unit of time (day, month, year, etc.)
* @returns This instance for chaining
*/
startOf(unit: dayjs.OpUnitType): DateTime;
/**
* Converts to string in UK date time format
* @returns Formatted date string
*/
toString(): string;
/**
* Gets ISO string representation
* @returns ISO format string
*/
toISOString(): string;
/**
* Calculates the difference between dates
* @param targetDate - Target date to compare with
* @param unit - Unit for the difference calculation
* @param precise - Whether to return decimal result
* @returns Difference in specified units
*/
diff(targetDate: AcceptableDate, unit: dayjs.QUnitType, precise?: boolean): number;
/**
* Calculates the difference in days
* @param targetDate - Target date to compare with
* @returns Difference in days
*/
daysDiff(targetDate: AcceptableDate): number;
/**
* Calculates duration from this date to target
* @param targetDate - Target date
* @param unit - Unit for duration calculation
* @returns Duration in specified units
*/
compareDuration(targetDate: AcceptableDate, unit: dayjs.QUnitType): number;
/**
* Checks if the date is valid
* @returns True if valid date
*/
isValid(): boolean;
/**
* Checks if this date is before the target date
* @param targetDate - Target date to compare with
* @returns True if this date is before target
*/
isBefore(targetDate: AcceptableDate): boolean;
/**
* Checks if this date is after the target date
* @param targetDate - Target date to compare with
* @returns True if this date is after target
*/
isAfter(targetDate: AcceptableDate): boolean;
/**
* Checks if this date is between start and end dates
* @param startDate - Start date of range
* @param endDate - End date of range
* @returns True if date is between start and end
*/
isBetween(startDate: AcceptableDate, endDate: AcceptableDate): boolean;
/**
* Sets the timezone (mutable operation)
* @param tz - Timezone identifier
* @returns This instance for chaining
*/
setTimezone(tz: string): DateTime;
/**
* Converts to UK timezone (mutable operation)
* @returns This instance for chaining
*/
toUKTime(): DateTime;
/**
* Gets the hour component
* @returns Hour (0-23)
*/
getHour(): number;
/**
* Gets the minute component
* @returns Minute (0-59)
*/
getMinute(): number;
/**
* Gets the second component
* @returns Second (0-59)
*/
getSecond(): number;
/**
* Creates a clone of this DateTime
* @returns New DateTime instance with the same date/time
*/
clone(): DateTime;
/**
* Provides debug information about this DateTime
* @returns Object with diagnostic information
*/
debug(): object;
}
export {};