Day.js는 네이티브 Date.prototype
을 수정하는 대신 Dayjs
오브젝트인 Date 오브젝트 래퍼를 생성합니다.
Dayjs
오브젝트는 변경이 불가능(immutable)합니다. 즉, 모든 API 작업은 새로운 Dayjs
오브젝트를 반환합니다.
- API Reference
- Parsing
- Get and Set
- Manipulating
- Displaying
- Format
.format(stringWithTokens: string)
- Difference
.diff(compared: Dayjs, unit?: string, float?: boolean)
- Unix Timestamp (milliseconds)
.valueOf()
- Unix Timestamp (seconds)
.unix()
- UTC offset (minutes)
.utcOffset()
- Days in the Month
.daysInMonth()
- As Javascript Date
.toDate()
- As JSON
.toJSON()
- As ISO 8601 String
.toISOString()
- As String
.toString()
- Format
- Query
- UTC
- Plugin APIs
매개 변수없이 호출하면 현재 날짜와 시간을 가진 새로운 Dayjs
오브젝트가 반환됩니다.
dayjs()
Day.js는 다른 날짜 형식도 구분 분석합니다.
ISO 8601 string
dayjs('2018-04-04T16:00:00.000Z')
dayjs(new Date(2018, 8, 18))
Returns a Dayjs
from a Unix timestamp (milliseconds since the Unix Epoch)
dayjs(1318781876406)
Returns a Dayjs
from a Unix timestamp (seconds since the Unix Epoch)
dayjs.unix(1318781876)
dayjs.unix(1318781876.721)
- parse custom formats
dayjs("12-25-1995", "MM-DD-YYYY")
in pluginCustomParseFormat
Dayjs
클론 오브젝트를 반환합니다.
dayjs().clone()
dayjs(dayjs('2019-01-25')) // passing a Dayjs object to a constructor will also clone it
Dayjs
날짜가 유효한지 확인합니다. 반환 타입은 boolean
입니다.
dayjs().isValid()
Gets or sets the year.
dayjs().year()
dayjs().year(2000)
Gets or sets the month. Starts at 0
dayjs().month()
dayjs().month(0)
Gets or sets the day of the month. Starts at 1
dayjs().date()
dayjs().date(1)
Gets or sets the day of the week. Starts on Sunday with 0
dayjs().day()
dayjs().day(0)
Gets or sets the hour.
dayjs().hour()
dayjs().hour(12)
Gets or sets the minute.
dayjs().minute()
dayjs().minute(59)
Gets or sets the second.
dayjs().second()
dayjs().second(1)
Gets or sets the millisecond.
dayjs().millisecond()
dayjs().millisecond(1)
Returns a number
with information getting from Dayjs
object
dayjs().get('month') // start 0
dayjs().get('day')
Unit | Shorthand | Description |
---|---|---|
date |
Date of Month | |
day |
d |
Day of Week (Sunday as 0, Saturday as 6) |
month |
M |
Month (January as 0, December as 11) |
year |
y |
Year |
hour |
h |
Hour |
minute |
m |
Minute |
second |
s |
Second |
millisecond |
ms |
Millisecond |
변경 사항이 적용된 Dayjs
를 반환합니다.
dayjs().set('date', 1)
dayjs().set('month', 3) // April
dayjs().set('second', 30)
Dayjs
오브젝트는 여러 방법으로 처리할 수 있습니다.
dayjs('2019-01-25')
.add(1, 'day')
.subtract(1, 'year')
.toString() // Fri, 26 Jan 2018 00:00:00 GMT
지정한 시간을 더한 Dayjs
오브젝트 복제본을 반환합니다.
dayjs().add(7, 'day')
지정한 시간을 뺀 Dayjs
오브젝트 복제본을 반환합니다.
dayjs().subtract(7, 'year')
특정 시간 단위의 시작 시점에 대한 시간을 Dayjs
오브젝트 복제본으로 반환합니다.
dayjs().startOf('week') // Depends on `weekStart` in locale
특정 시간 단위의 끝나는 시점에 대한 시간을 Dayjs
오브젝트 복제본으로 반환힙니다.
dayjs().endOf('month')
Dayjs
오브젝트 시간을 기본 형식으로 출력합니다. 반환 형식은 string
입니다.
예외하고 싶은 문자일 경우, 대괄호나 중괄호를 사용하여 묶으면됩니다. (예, [A] [MM]
)
dayjs().format() // 분과 초가 없는 ISO5801 형식의 현재 시간을 나타냅니다. 예: '2020-04-02T08:02:17-05:00'
dayjs('2019-01-25').format('[YYYY] YYYY-MM-DDTHH:mm:ssZ[Z]') // 'YYYY 2019-01-25T00:00:00-02:00Z'
dayjs('2019-01-25').format('DD/MM/YYYY') // '25/01/2019'
Format | Output | Description |
---|---|---|
YY |
18 | 두 자리로 된 연도 |
YYYY |
2018 | 네 자리로 된 연도 |
M |
1-12 | 달, 1부터 시작 |
MM |
01-12 | 달, 두 자리로 표현 |
MMM |
Jan-Dec | 월 이름 약어 |
MMMM |
January-December | 월 이름 |
D |
1-31 | 일 |
DD |
01-31 | 일, 두 자리로 표현 |
d |
0-6 | 요일, 일요일은 0 |
dd |
Su-Sa | The min name of the day of the week |
ddd |
Sun-Sat | The short name of the day of the week |
dddd |
Sunday-Saturday | 요일 이름 |
H |
0-23 | 시간 |
HH |
00-23 | 시간, 두 자리로 표현 |
h |
1-12 | 시간, 12시간 |
hh |
01-12 | 시간, 12시간, 두 자리로 표현 |
m |
0-59 | 분 |
mm |
00-59 | 분, 두 자리로 표현 |
s |
0-59 | 초 |
ss |
00-59 | 초, 두 자리로 표현 |
SSS |
000-999 | 밀리 초, 3자리로 표현 |
Z |
+05:00 | UTC로부터 추가된 시간 |
ZZ |
+0500 | UTC로부터 추가된 시간, 두자리로 표현 |
A |
AM PM | |
a |
am pm |
- 플러그인
AdvancedFormat
을 사용하면 더 많은 형식(Q Do k kk X x ...
)을 사용할 수 있습니다. - Localized format options
L LT LTS ...
in pluginLocalizedFormat
두 Dayjs
오브젝트 차이를 지정한 단위로 가져옵니다. 반환 타입은 number
입니다.
const date1 = dayjs('2019-01-25')
const date2 = dayjs('2018-06-05')
date1.diff(date2) // 20214000000 default milliseconds
date1.diff(date2, 'month') // 7
date1.diff(date2, 'month', true) // 7.645161290322581
date1.diff(date2, 'day') // 233
Dayjs
에 대한 Unix Epoch 이후의 밀리 초 시간을 가져옵니다. 반환 타입은 number
입니다.
dayjs('2019-01-25').valueOf() // 1548381600000
Dayjs
에 대한 Unix Epoch 이후의 초 시간을 가져옵니다. 반환 타입은 number
입니다.
dayjs('2019-01-25').unix() // 1548381600
Returns the UTC offset in minutes for the Dayjs
.
dayjs().utcOffset()
Dayjs
에서 표기하는 달에서 일수를 가져옵니다. 반환 타입은 number
입니다.
dayjs('2019-01-25').daysInMonth() // 31
Dayjs
오브젝트에서 파싱된 네이티브 Date
오브젝트 복사본을 반환합니다.
dayjs('2019-01-25').toDate()
ISO8601에 대한 형식으로 Dayjs
를 출력합니다. 반환 타입은 string
입니다.
dayjs('2019-01-25').toJSON() // '2019-01-25T02:00:00.000Z'
ISO8601에 대한 형식으로 Dayjs
를 출력합니다. 반환 타입은 string
입니다.
dayjs('2019-01-25').toISOString() // '2019-01-25T02:00:00.000Z'
날짜를 string
타입 값으로 반환합니다.
dayjs('2019-01-25').toString() // 'Fri, 25 Jan 2019 02:00:00 GMT'
Dayjs
가 다른 Dayjs
보다 앞선 시점인지를 확인합니다. 반환 타입은 boolean
입니다.
dayjs().isBefore(dayjs()) // false
dayjs().isBefore(dayjs(), 'year') // false
Dayjs
가 다른 Dayjs
과 동일한 시점인지를 확인합니다. 반환 타입은 boolean
입니다.
dayjs().isSame(dayjs()) // true
dayjs().isSame(dayjs(), 'year') // true
Dayjs
가 다른 Dayjs
보다 뒷선 시점인지를 확인합니다. 반환 타입은 boolean
입니다.
dayjs().isAfter(dayjs()) // false
dayjs().isAfter(dayjs(), 'year') // false
Returns a boolean
indicating whether a variable is a dayjs object or not.
dayjs.isDayjs(dayjs()) // true
dayjs.isDayjs(new Date()) // false
The operator instanceof
works equally well:
dayjs() instanceof dayjs // true
If you want to parse or display in UTC, you can use .utc
.local
.isUTC
with plugin UTC
.from
.to
.fromNow
.toNow
에 대한 상대 시간을 가져옵니다.
플러그인 RelativeTime
.isLeapYear
to get is a leap year or not
plugin IsLeapYear
.week
to get week of the year
plugin WeekOfYear
.weekday
to get or set locale aware day of the week
plugin WeekDay
.isoWeeksInYear
to get the number of weeks in year
plugin IsoWeeksInYear
.isSameOrAfter
to check if a date is same of after another date
plugin IsSameOrAfter
.isSameOrBefore
to check if a date is same of before another date.
plugin IsSameOrBefore
.isBetween
to check if a date is between two other dates
plugin IsBetween
.quarter
to get quarter of the year
plugin QuarterOfYear
.toArray
to return an array
that mirrors the parameters
plugin ToArray
.toObject
to return an object
with the date's properties.
plugin ToObject
.min
.max
to compare given dayjs instances.
plugin MinMax
.calendar
to display calendar time
plugin Calendar