dayjs
- Version 1.11.13
- Published
- 670 kB
- No dependencies
- MIT license
Install
npm i dayjs
yarn add dayjs
pnpm add dayjs
Overview
2KB immutable date time library alternative to Moment.js with the same modern API
Index
Variables
Functions
Classes
Interfaces
Type Aliases
Namespaces
Variables
variable Ls
const Ls: { [key: string]: ILocale };
Functions
function dayjs
dayjs: typeof dayjs;
function extend
extend: <T = unknown>(plugin: PluginFunc<T>, option?: T) => Dayjs;
function isDayjs
isDayjs: (d: any) => d is Dayjs;
function locale
locale: ( preset?: string | ILocale, object?: Partial<ILocale>, isLocal?: boolean) => string;
function unix
unix: (t: number) => Dayjs;
Classes
class Dayjs
class Dayjs {}
constructor
constructor(config?: string | number | Dayjs | Date);
method add
add: (value: number, unit?: ManipulateType) => Dayjs;
Returns a cloned Day.js object with a specified amount of time added.
dayjs().add(7, 'day')// => DayjsUnits are case insensitive, and support plural and short forms.
Docs: https://day.js.org/docs/en/manipulate/add
method clone
clone: () => Dayjs;
All Day.js objects are immutable. Still,
dayjs#clone
can create a clone of the current object if you need one.dayjs().clone()// => Dayjsdayjs(dayjs('2019-01-25')) // passing a Dayjs object to a constructor will also clone itDocs: https://day.js.org/docs/en/parse/dayjs-clone
method date
date: { (): number; (value: number): Dayjs };
Get the date of the month.
dayjs().date()// => 1-31Docs: https://day.js.org/docs/en/get-set/date
Set the date of the month.
Accepts numbers from 1 to 31. If the range is exceeded, it will bubble up to the next months.
dayjs().date(1)// => DayjsDocs: https://day.js.org/docs/en/get-set/date
method day
day: { (): 0 | 1 | 2 | 3 | 4 | 5 | 6; (value: number): Dayjs };
Get the day of the week.
Returns numbers from 0 (Sunday) to 6 (Saturday).
dayjs().day()// 0-6Docs: https://day.js.org/docs/en/get-set/day
Set the day of the week.
Accepts numbers from 0 (Sunday) to 6 (Saturday). If the range is exceeded, it will bubble up to next weeks.
dayjs().day(0)// => DayjsDocs: https://day.js.org/docs/en/get-set/day
method daysInMonth
daysInMonth: () => number;
Get the number of days in the current month.
dayjs('2019-01-25').daysInMonth() // 31Docs: https://day.js.org/docs/en/display/days-in-month
method diff
diff: ( date?: ConfigType, unit?: QUnitType | OpUnitType, float?: boolean) => number;
This indicates the difference between two date-time in the specified unit.
To get the difference in milliseconds, use
dayjs#diff
const date1 = dayjs('2019-01-25')const date2 = dayjs('2018-06-05')date1.diff(date2) // 20214000000 default millisecondsdate1.diff() // milliseconds to current timeTo get the difference in another unit of measurement, pass that measurement as the second argument.
const date1 = dayjs('2019-01-25')date1.diff('2018-06-05', 'month') // 7Units are case insensitive, and support plural and short forms.
Docs: https://day.js.org/docs/en/display/difference
method endOf
endOf: (unit: OpUnitType) => Dayjs;
Returns a cloned Day.js object and set it to the end of a unit of time.
dayjs().endOf('month')// => DayjsUnits are case insensitive, and support plural and short forms.
Docs: https://day.js.org/docs/en/manipulate/end-of
method format
format: (template?: string) => string;
Get the formatted date according to the string of tokens passed in.
To escape characters, wrap them in square brackets (e.g. [MM]).
dayjs().format()// => current date in ISO8601, without fraction seconds e.g. '2020-04-02T08:02:17-05:00'dayjs('2019-01-25').format('[YYYYescape] YYYY-MM-DDTHH:mm:ssZ[Z]')// 'YYYYescape 2019-01-25T00:00:00-02:00Z'dayjs('2019-01-25').format('DD/MM/YYYY') // '25/01/2019'Docs: https://day.js.org/docs/en/display/format
method get
get: (unit: UnitType) => number;
String getter, returns the corresponding information getting from Day.js object.
In general:
dayjs().get(unit) === dayjs()[unit]()Units are case insensitive, and support plural and short forms.
dayjs().get('year')dayjs().get('month') // start 0dayjs().get('date')Docs: https://day.js.org/docs/en/get-set/get
method hour
hour: { (): number; (value: number): Dayjs };
Get the hour.
dayjs().hour()// => 0-23Docs: https://day.js.org/docs/en/get-set/hour
Set the hour.
Accepts numbers from 0 to 23. If the range is exceeded, it will bubble up to the next day.
dayjs().hour(12)// => DayjsDocs: https://day.js.org/docs/en/get-set/hour
method isAfter
isAfter: (date?: ConfigType, unit?: OpUnitType) => boolean;
This indicates whether the Day.js object is after the other supplied date-time.
dayjs().isAfter(dayjs('2011-01-01')) // default millisecondsIf you want to limit the granularity to a unit other than milliseconds, pass it as the second parameter.
dayjs().isAfter('2011-01-01', 'year')// => booleanUnits are case insensitive, and support plural and short forms.
Docs: https://day.js.org/docs/en/query/is-after
method isBefore
isBefore: (date?: ConfigType, unit?: OpUnitType) => boolean;
This indicates whether the Day.js object is before the other supplied date-time.
dayjs().isBefore(dayjs('2011-01-01')) // default millisecondsIf you want to limit the granularity to a unit other than milliseconds, pass it as the second parameter.
dayjs().isBefore('2011-01-01', 'year')// => booleanUnits are case insensitive, and support plural and short forms.
Docs: https://day.js.org/docs/en/query/is-before
method isSame
isSame: (date?: ConfigType, unit?: OpUnitType) => boolean;
This indicates whether the Day.js object is the same as the other supplied date-time.
dayjs().isSame(dayjs('2011-01-01')) // default millisecondsIf you want to limit the granularity to a unit other than milliseconds, pass it as the second parameter.
dayjs().isSame('2011-01-01', 'year')// => booleanDocs: https://day.js.org/docs/en/query/is-same
method isValid
isValid: () => boolean;
This returns a
boolean
indicating whether the Day.js object contains a valid date or not.dayjs().isValid()// => booleanDocs: https://day.js.org/docs/en/parse/is-valid
method locale
locale: { (): string; (preset: string | ILocale, object?: Partial<ILocale>): Dayjs;};
method millisecond
millisecond: { (): number; (value: number): Dayjs };
Get the milliseconds.
dayjs().millisecond()// => 0-999Docs: https://day.js.org/docs/en/get-set/millisecond
Set the milliseconds.
Accepts numbers from 0 to 999. If the range is exceeded, it will bubble up to the next seconds.
dayjs().millisecond(1)// => DayjsDocs: https://day.js.org/docs/en/get-set/millisecond
method minute
minute: { (): number; (value: number): Dayjs };
Get the minutes.
dayjs().minute()// => 0-59Docs: https://day.js.org/docs/en/get-set/minute
Set the minutes.
Accepts numbers from 0 to 59. If the range is exceeded, it will bubble up to the next hour.
dayjs().minute(59)// => DayjsDocs: https://day.js.org/docs/en/get-set/minute
method month
month: { (): number; (value: number): Dayjs };
Get the month.
Months are zero indexed, so January is month 0.
dayjs().month()// => 0-11Docs: https://day.js.org/docs/en/get-set/month
Set the month.
Months are zero indexed, so January is month 0.
Accepts numbers from 0 to 11. If the range is exceeded, it will bubble up to the next year.
dayjs().month(0)// => DayjsDocs: https://day.js.org/docs/en/get-set/month
method second
second: { (): number; (value: number): Dayjs };
Get the seconds.
dayjs().second()// => 0-59Docs: https://day.js.org/docs/en/get-set/second
Set the seconds.
Accepts numbers from 0 to 59. If the range is exceeded, it will bubble up to the next minutes.
dayjs().second(1)// Dayjs
method set
set: (unit: UnitType, value: number) => Dayjs;
Generic setter, accepting unit as first argument, and value as second, returns a new instance with the applied changes.
In general:
dayjs().set(unit, value) === dayjs()[unit](value)Units are case insensitive, and support plural and short forms.
dayjs().set('date', 1)dayjs().set('month', 3) // Aprildayjs().set('second', 30)Docs: https://day.js.org/docs/en/get-set/set
method startOf
startOf: (unit: OpUnitType) => Dayjs;
Returns a cloned Day.js object and set it to the start of a unit of time.
dayjs().startOf('year')// => DayjsUnits are case insensitive, and support plural and short forms.
Docs: https://day.js.org/docs/en/manipulate/start-of
method subtract
subtract: (value: number, unit?: ManipulateType) => Dayjs;
Returns a cloned Day.js object with a specified amount of time subtracted.
dayjs().subtract(7, 'year')// => DayjsUnits are case insensitive, and support plural and short forms.
Docs: https://day.js.org/docs/en/manipulate/subtract
method toDate
toDate: () => Date;
To get a copy of the native
Date
object parsed from the Day.js object usedayjs#toDate
.dayjs('2019-01-25').toDate()// => Date
method toISOString
toISOString: () => string;
To format as an ISO 8601 string.
dayjs('2019-01-25').toISOString() // '2019-01-25T02:00:00.000Z'Docs: https://day.js.org/docs/en/display/as-iso-string
method toJSON
toJSON: () => string;
To serialize as an ISO 8601 string.
dayjs('2019-01-25').toJSON() // '2019-01-25T02:00:00.000Z'Docs: https://day.js.org/docs/en/display/as-json
method toString
toString: () => string;
Returns a string representation of the date.
dayjs('2019-01-25').toString() // 'Fri, 25 Jan 2019 02:00:00 GMT'Docs: https://day.js.org/docs/en/display/as-string
method unix
unix: () => number;
This returns the Unix timestamp (the number of **seconds** since the Unix Epoch) of the Day.js object.
dayjs('2019-01-25').unix() // 1548381600This value is floored to the nearest second, and does not include a milliseconds component.
Docs: https://day.js.org/docs/en/display/unix-timestamp
method utcOffset
utcOffset: () => number;
Get the UTC offset in minutes.
dayjs().utcOffset()Docs: https://day.js.org/docs/en/manipulate/utc-offset
method valueOf
valueOf: () => number;
This returns the number of **milliseconds** since the Unix Epoch of the Day.js object.
dayjs('2019-01-25').valueOf() // 1548381600000+dayjs(1548381600000) // 1548381600000To get a Unix timestamp (the number of seconds since the epoch) from a Day.js object, you should use Unix Timestamp
dayjs#unix()
.Docs: https://day.js.org/docs/en/display/unix-timestamp-milliseconds
method year
year: { (): number; (value: number): Dayjs };
Get the year.
dayjs().year()// => 2020Docs: https://day.js.org/docs/en/get-set/year
Set the year.
dayjs().year(2000)// => DayjsDocs: https://day.js.org/docs/en/get-set/year
Interfaces
interface ConfigTypeMap
interface ConfigTypeMap {}
property default
default: string | number | Date | Dayjs | null | undefined;
interface FormatObject
interface FormatObject {}
Type Aliases
type ConfigType
type ConfigType = ConfigTypeMap[keyof ConfigTypeMap];
type ManipulateType
type ManipulateType = Exclude<OpUnitType, 'date' | 'dates'>;
type OptionType
type OptionType = FormatObject | string | string[];
type OpUnitType
type OpUnitType = UnitType | 'week' | 'weeks' | 'w';
type PluginFunc
type PluginFunc<T = unknown> = (option: T, c: typeof Dayjs, d: typeof dayjs) => void;
type QUnitType
type QUnitType = UnitType | 'quarter' | 'quarters' | 'Q';
type UnitType
type UnitType = UnitTypeLong | UnitTypeLongPlural | UnitTypeShort;
type UnitTypeLong
type UnitTypeLong = | 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year' | 'date';
type UnitTypeLongPlural
type UnitTypeLongPlural = | 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years' | 'dates';
type UnitTypeShort
type UnitTypeShort = 'd' | 'D' | 'M' | 'y' | 'h' | 'm' | 's' | 'ms';
Namespaces
namespace dayjs/esm/locale/*
module 'dayjs/esm/locale/*' {}
namespace dayjs/locale/*
module 'dayjs/locale/*' {}
Package Files (3)
Dependencies (0)
No dependencies.
Dev Dependencies (29)
- @babel/cli
- @babel/core
- @babel/node
- @babel/preset-env
- babel-core
- babel-jest
- babel-plugin-external-helpers
- cross-env
- eslint
- eslint-config-airbnb-base
- eslint-plugin-import
- eslint-plugin-jest
- gzip-size-cli
- jasmine-core
- jest
- karma
- karma-jasmine
- karma-sauce-launcher
- mockdate
- moment
- moment-timezone
- ncp
- pre-commit
- prettier
- rollup
- rollup-plugin-babel
- rollup-plugin-terser
- size-limit
- typescript
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/dayjs
.
- Markdown[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/dayjs)
- HTML<a href="https://www.jsdocs.io/package/dayjs"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 4568 ms. - Missing or incorrect documentation? Open an issue for this package.