Get Timestamp
Get Timestamp — Chronos Documentation.
getTimestamp
Generates an ISO 8601 timestamp string from a date input, or from the current time when no input is provided.
Import
import { getTimestamp } from "toolbox-x/utils";Function Signatures
getTimestamp(): ISODateTimeString<'utc'>;
getTimestamp(value: DateArgs, format?: ISODateFormat): ISODateTimeString<'utc' | 'local'>;
getTimestamp(options?: TimestampOptions): ISODateTimeString<'utc' | 'local'>;Parameters
| Name | Type | Description |
|---|---|---|
value | DateArgs | Date input as a Date, date string, or timestamp number. |
format | ISODateFormat | Output format: 'utc' (default) or 'local'. |
options | TimestampOptions | Object containing optional value and format. |
Returns
ISODateTimeString<'utc'> (default) or ISODateTimeString<'local'> - An ISO 8601 timestamp string.
If format is 'utc', the output ends with Z.
If format is 'local', the output includes the local time & timezone offset like +06:00.
Example Usage
playground.ts
Notes
- Defaults to the current date and time when no
valueis provided. - Invalid date input falls back to the current date and time.
'local'output reflects the system timezone offset for the given date.'utc'output matchesDate.prototype.toISOString()for valid inputs.- The output is always a string typed as
ISODateTimeString<'utc'>orISODateTimeString<'local'>for type safety and clarity in codebases. - The output value (whether UTC or local) is always in ISO 8601 format and returns the same instance of
Datewhen used asnew Date(getTimestamp(some-value))for valid inputs, ensuring consistency across formats. - Exported as
getTimestamp(lowercases), separate fromChronos.getTimeStamp()which returns timestamp in milliseconds.
Type Definitions
type DateArgs = string | number | Date;
type ISODateFormat = 'local' | 'utc';
interface TimestampOptions {
value?: DateArgs;
format?: ISODateFormat;
}
type $ISOTimeString = `${number}-${number}-${number}T${number}:${number}:${number}.${number}`;
type ISODateTimeString<F extends ISODateFormat = 'utc'> = F extends 'utc'
? `${$ISOTimeString}Z`
: `${$ISOTimeString}${$UTCOffset}`;Use Cases
- Storing timestamps in logs and databases
- Generating ISO strings for APIs
- Converting inputs to a consistent timestamp format
- Including local timezone offsets for display or auditing
Conclusion
The getTimestamp utility provides:
- Simple ISO output with minimal configuration
- Flexible inputs via
Date, string, or numeric timestamp - UTC or local formatting depending on your needs
- Safe defaults for invalid or missing inputs
Last updated: Fri, May 22, 2026 07:46:19AM (Coordinated Universal Time)
