Skip to main content

Get & Set Methods

get()

Signature

get(unit: TimeUnit): number

Parameters

  • unit: Time unit to get

Return Type

number - Component value

Example

new Chronos('2025-01-15').get('month'); // 0 (January)

getTimeStamp()

NOTICE
  • The getter method timestamp and getTimeStamp() returns the same value.

  • The timestamp in seconds (which some people considers as timestamp) can be accessed using unix getter method.

Signature

getTimeStamp(): number

Return Type

number - Milliseconds since Unix epoch (January 1, 1970 UTC)

Behavior & Notes

  • Equivalent to JavaScript Date.getTime()
  • Returns the primitive timestamp value of the Chronos instance
  • Useful for interoperability with other date libraries or storage

Example

const timestamp = new Chronos('2025-01-01').getTimeStamp();
// Returns: 1735689600000 (exact timestamp for midnight Jan 1, 2025 UTC)

// Comparison with native Date
const jsDate = new Date('2025-01-01');
console.log(jsDate.getTime() === new Chronos(jsDate).getTimeStamp()); // true

Use Cases

  • Storing dates in databases as timestamps
  • Performance-critical operations where primitive numbers are preferred
  • Interfacing with systems that expect Unix timestamps

set()

Returns a new Chronos instance with the specified unit set to the given value.

Signature

set<Unit extends TimeUnit>(unit: Unit, value: TimeUnitValue<Unit>): Chronos

Parameters

  • unit: Time unit to set
  • value: Value to set

Return Type

Chronos - New instance with updated value

Type Definitions

/** Name of time unit from `year` to `millisecond` */
type TimeUnit = 'year' | 'month' | 'day' | 'week' | 'hour' | 'minute' | 'second' | 'millisecond';

/** Conditional value for `TimeUnit` */
type TimeUnitValue<Unit extends TimeUnit> =
Unit extends 'month' ? NumberRange<1, 12> // 1-12
: Unit extends 'week' ? NumberRange<1, 53> // 1-51
: Unit extends 'day' ? NumberRange<1, 31> // 1-31
: Unit extends 'hour' ? Enumerate<24> // 0-23
: Unit extends 'minute' | 'second' ? Enumerate<60> // 0-59
: Unit extends 'millisecond' ? Milliseconds // 0-999
: number;

Example

new Chronos('2025-01-15').set('month', 6); // June 15
N.B.

month is 1 based (1: January, 12: December)

See Also

with static method for more options


startOf()

Signature

startOf(unit: TimeUnit, weekStartsOn?: Enumerate<7>): Chronos

Parameters

  • unit: Unit to start from
  • weekStartsOn: Week start day (from 0-6) (default: 0)

Return Type

Chronos - Start of period

Example

new Chronos('2025-01-15').startOf('month'); // Jan 1

endOf()

Signature

endOf(unit: TimeUnit, weekStartsOn?: Enumerate<7>): Chronos

Parameters

  • unit: Unit to end at
  • weekStartsOn: Week start day (from 0-6) (default: 0)

Return Type

Chronos - End of period

Example

new Chronos('2025-01-15').endOf('month'); // Jan 31 23:59:59.999

firstDayOfMonth()

Signature

firstDayOfMonth(): Chronos

Return Type

Chronos - First day of month

Example

new Chronos('2025-01-15').firstDayOfMonth(); // Jan 1

lastDayOfMonth()

Signature

lastDayOfMonth(): Chronos

Return Type

Chronos - Last day of month

Example

new Chronos('2025-01-15').lastDayOfMonth(); // Jan 31

getWeek()

Signature

getWeek(): NumberRange<1, 53>

Return Type

NumberRange<1, 53> - ISO week number (1-53)

Example

new Chronos('2022-01-01').getWeek(); // 52 (previous year)

setWeek()

Signature

setWeek(week: NumberRange<1, 53>): Chronos

Parameters

  • week: Week number to set (from 1-53)

Return Type

Chronos - Instance at start of week

Example

new Chronos('2025-01-01').setWeek(1); // Jan 2 (ISO week 1)

getWeekOfYear()

Signature

getWeekOfYear(weekStartsOn?: Enumerate<7>): NumberRange<1, 53>

Parameters

  • weekStartsOn: Week start day (from 0-6) (default: 0)

Return Type

NumberRange<1, 53> - Week number (1-53)

Example

new Chronos('2025-01-01').getWeekOfYear(); // 1 (Sunday-start week)

getWeekYear()

Signature

getWeekYear(weekStartsOn?: Enumerate<7>): number

Parameters

  • weekStartsOn: Week start day (from 0-6) (default: 0)

Return Type

number - ISO week-numbering year

Example

new Chronos('2025-01-01').getWeekYear(); // 2022 (ISO year)

getDayOfYear()

Signature

getDayOfYear(): NumberRange<1, 366>

Return Type

NumberRange<1, 366> - Day of year (1-366)

Example

new Chronos('2025-01-01').getDayOfYear(); // 1

daysInMonth()

Signature

daysInMonth(): NumberRange<28, 31>

Return Type

NumberRange<28, 31> - Days in month (28 | 29 | 30 | 31)

Example

new Chronos('2025-02-01').daysInMonth(); // 28