ts-time-utils
Version:
A comprehensive TypeScript utility library for time, dates, durations, and calendar operations with full tree-shaking support
171 lines • 4.38 kB
TypeScript
import type { DurationInput, DurationComparison } from './types.js';
/**
* Represents a duration of time with arithmetic and conversion capabilities
*/
export declare class Duration {
private readonly _milliseconds;
constructor(input: number | DurationInput | string);
/**
* Create Duration from milliseconds
*/
static fromMilliseconds(ms: number): Duration;
/**
* Create Duration from seconds
*/
static fromSeconds(seconds: number): Duration;
/**
* Create Duration from minutes
*/
static fromMinutes(minutes: number): Duration;
/**
* Create Duration from hours
*/
static fromHours(hours: number): Duration;
/**
* Create Duration from days
*/
static fromDays(days: number): Duration;
/**
* Create Duration from weeks
*/
static fromWeeks(weeks: number): Duration;
/**
* Create Duration from a string (e.g., "1h 30m", "2.5 hours", "90 seconds")
*/
static fromString(str: string): Duration;
/**
* Create Duration between two dates
*/
static between(start: Date, end: Date): Duration;
/**
* Get duration in milliseconds
*/
get milliseconds(): number;
/**
* Get duration in seconds
*/
get seconds(): number;
/**
* Get duration in minutes
*/
get minutes(): number;
/**
* Get duration in hours
*/
get hours(): number;
/**
* Get duration in days
*/
get days(): number;
/**
* Get duration in weeks
*/
get weeks(): number;
/**
* Add another duration
*/
add(other: Duration | number | DurationInput): Duration;
/**
* Subtract another duration
*/
subtract(other: Duration | number | DurationInput): Duration;
/**
* Multiply duration by a factor
*/
multiply(factor: number): Duration;
/**
* Divide duration by a factor
*/
divide(factor: number): Duration;
/**
* Get absolute duration (always positive)
*/
abs(): Duration;
/**
* Negate duration
*/
negate(): Duration;
/**
* Check if duration equals another
*/
equals(other: Duration | number): boolean;
/**
* Check if duration is greater than another
*/
greaterThan(other: Duration | number): boolean;
/**
* Check if duration is less than another
*/
lessThan(other: Duration | number): boolean;
/**
* Compare with another duration (-1, 0, 1)
*/
compareTo(other: Duration): DurationComparison;
/**
* Check if duration is zero
*/
isZero(): boolean;
/**
* Check if duration is positive
*/
isPositive(): boolean;
/**
* Check if duration is negative
*/
isNegative(): boolean;
/**
* Convert to human-readable string
*/
toString(): string;
/**
* Convert to detailed object representation
*/
toObject(): Required<DurationInput>;
/**
* Convert to JSON representation
*/
toJSON(): number;
/**
* Create Duration from JSON
*/
static fromJSON(ms: number): Duration;
private calculateMilliseconds;
private parseString;
private normalizeToDuration;
}
/**
* Create a new Duration instance
*/
export declare function createDuration(input: number | DurationInput | string): Duration;
/**
* Check if a value is a valid duration
*/
export declare function isValidDuration(value: any): value is Duration;
/**
* Parse duration from various formats
*/
export declare function parseDurationString(input: string | number | DurationInput): Duration;
/**
* Format duration to human-readable string
*/
export declare function formatDurationString(duration: Duration | number, options?: {
long?: boolean;
precision?: number;
}): string;
/**
* Get the maximum duration from an array
*/
export declare function maxDuration(...durations: Duration[]): Duration | null;
/**
* Get the minimum duration from an array
*/
export declare function minDuration(...durations: Duration[]): Duration | null;
/**
* Sum multiple durations
*/
export declare function sumDurations(...durations: Duration[]): Duration;
/**
* Get average duration from an array
*/
export declare function averageDuration(...durations: Duration[]): Duration | null;
//# sourceMappingURL=duration.d.ts.map