Day.js は組み込みの Date.prototype
を変更する代わりに Dayjs
オブジェクトと呼ばれる Date オブジェクトのラッパーを作成します。
Dayjs
オブジェクトは不変 (immutable) です。すなわち、すべての API 操作は新しい Dayjs
オブジェクトを返します。
Day.js は指定されない限り、常に新しい Dayjs オブジェクトを返します。
サポートされている入力形式のいずれかで dayjs()
を呼び出します。
現在の日付と時間を取得するには、パラメータなしで dayjs() を呼び出します。
dayjs();
ISO 8601 に準拠する形式から作成します。
dayjs(String);
dayjs('1995-12-25');
JavaScript の組み込みの Date オブジェクトを渡します。
dayjs(Date);
dayjs(new Date(2018, 8, 18));
Unix エポック (1970年1月1日 12:00AM UTC) 以降のミリ秒数を表す整数値を渡します。
dayjs(Number);
dayjs(1318781876406);
dayjs.unix(Number);
dayjs.unix(1318781876);
すべての Dayjs
は不変 (immutable) です。 オブジェクトのコピーが必要な場合は、 .clone()
を呼び出してください。
Dayjs
オブジェクトに対して dayjs() を呼び出すと、それもクローンされます。
dayjs(Dayjs);
dayjs().clone();
- Boolean を返します
Dayjs
オブジェクトが有効な日付かどうかをチェックします。
dayjs().isValid();
日付の取得と設定です。
- Number を返します
年を取得します。
dayjs().year();
- Number を返します
月を取得します。
dayjs().month();
- Number を返します
日を取得します。
dayjs().date();
- Number を返します
曜日を取得します。
dayjs().day();
- Number を返します
時間を取得します。
dayjs().hour();
- Number を返します
分を取得します。
dayjs().minute();
- Number を返します
秒を取得します。
dayjs().second();
- Number を返します
ミリ秒を取得します。
dayjs().millisecond();
日付のセッターです。 単位は大文字・小文字を区別しません。
dayjs().set((unit: String), (value: Int));
dayjs().set('date', 1);
dayjs().set('month', 3); // 4月
dayjs().set('second', 30);
Unit | Shorthand | Description |
---|---|---|
date |
Date of Month | |
day |
d |
Day of Week (Sunday as 0, Saturday as 6) |
month |
M |
Month |
year |
y |
Year |
hour |
h |
Hour |
minute |
m |
Minute |
second |
s |
Second |
millisecond |
ms |
Millisecond |
Dayjs
オブジェクトは次のような方法で操作することができます:
dayjs()
.startOf('month')
.add(1, 'day')
.subtract(1, 'year');
時間を足して新しい Dayjs
オブジェクトを返します。
dayjs().add((value: Number), (unit: String));
dayjs().add(7, 'day');
時間を引いて新しい Dayjs
オブジェクトを返します。 dayjs#add
と全く同じです。
dayjs().subtract((value: Number), (unit: String));
dayjs().subtract(7, 'year');
Returns a new Dayjs
object by by setting it to the start of a unit of time.
ある単位の始まりの時間の新しい Dayjs
オブジェクトを返します。
dayjs().startOf((unit: String));
dayjs().startOf('year');
ある単位の終わりの時間の新しい Dayjs
オブジェクトを返します。
dayjs().endOf((unit: String));
dayjs().endOf('month');
パースや操作が完了したならば Dayjs
オブジェクトを表示する方法が求められるはずです。
- String を返します
文字列を受け取り、対応する日付の値で置き換えます。
dayjs().format(String);
dayjs().format(); // "2014-09-08T08:02:17-05:00" (ISO 8601 形式、小数部は含まない)
dayjs().format('{YYYY} MM-DDTHH:mm:ssZ[Z]'); // "{2014} 09-08T08:02:17-05:00Z"
- 文字列内の文字をエスケープするには、文字を角括弧で囲みます (例: [Z]) 。
使用可能なフォーマットの一覧:
フォーマット | 出力 | 説明 |
---|---|---|
YY |
18 | 2桁の年 |
YYYY |
2018 | 4桁の年 |
M |
1-12 | 1始まりの月 |
MM |
01-12 | 1始まりかつ2桁の月 |
MMM |
Jan-Dec | 月の略称 |
MMMM |
January-December | 月の正式名 |
D |
1-31 | 日 |
DD |
01-31 | 2桁の日 |
d |
0-6 | 曜日 (日曜は0) |
dd |
Su-Sa | The min name of the day of the week |
ddd |
Sun-Sat | 曜日の略称 |
dddd |
Sunday-Saturday | 曜日名 |
H |
0-23 | 時間 |
HH |
00-23 | 2桁の時間 |
h |
1-12 | 12時制の時間 |
hh |
01-12 | 12時制かつ2桁の時間 |
m |
0-59 | 分 |
mm |
00-59 | 2桁の分 |
s |
0-59 | 秒 |
ss |
00-59 | 2桁の秒 |
SSS |
000-999 | 3桁のミリ秒 |
Z |
+5:00 | UTC からのオフセット |
ZZ |
+0500 | UTC からの2桁のオフセット |
A |
AM PM | 午前と午後 (大文字) |
a |
am pm | 午前と午後 (小文字) |
- More available formats
Q Do k kk X x ...
in pluginAdvancedFormat
- Number を返します
2つの Dayjs
オブジェクトの差をミリ秒単位で取得します。
const date1 = dayjs('2019-01-25');
const date2 = dayjs('2018-06-05');
date1.diff(date2); // 20214000000
date1.diff(date2, 'month'); // 7
date1.diff(date2, 'month', true); // 7.645161290322581
date1.diff(date2, 'day'); // 233
- Number を返します
Unix エポックからのミリ秒数を出力します。
dayjs().valueOf();
- Number を返します
Unix タイムスタンプ (Unix エポックからの秒数) を出力します。
dayjs().unix();
- Number を返します
その月の日数を取得します。
dayjs().daysInMonth();
- Javascript の
Date
オブジェクトを返します
Dayjs
オブジェクトから組み込みの Date
オブジェクトのコピーを取得します。
dayjs().toDate();
- Array を返します
Date コンストラクタパラメータに対応する値の配列を返します。
dayjs().toArray(); //[2018, 8, 18, 00, 00, 00, 000];
- JSON String を返します
Serializing a Dayjs
object to JSON, will return an ISO8601 string.
Dayjs
オブジェクトを JSON シリアライズし、ISO8601 形式の日付文字列を返します。
dayjs().toJSON(); //"2018-08-08T00:00:00.000Z"
- String を返します
ISO8601 形式の文字列にフォーマットします。
dayjs().toISOString();
- Object を返します
年、月、...(中略)...、ミリ秒のオブジェクトを返します。
dayjs().toObject(); // { years:2018, months:8, date:18, hours:0, minutes:0, seconds:0, milliseconds:0}
- String を返します
dayjs().toString();
- Boolean を返します
Dayjs
オブジェクトが別の Dayjs
オブジェクト以前の値かどうかを判定します。
dayjs().isBefore(Dayjs, unit? : String);
dayjs().isBefore(dayjs()); // false
dayjs().isBefore(dayjs(), 'year'); // false
- Boolean を返します
Dayjs
オブジェクトが別の Dayjs
オブジェクトの値と等しいかどうかを判定します。
dayjs().isSame(Dayjs, unit? : String);
dayjs().isSame(dayjs()); // true
dayjs().isSame(dayjs(), 'year'); // true
- Boolean を返します
Dayjs
オブジェクトが別の Dayjs
オブジェクト以降の値かどうかを判定します。
dayjs().isAfter(Dayjs, unit? : String);
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
.from
.to
.fromNow
.toNow
to get relative time
plugin RelativeTime
.isLeapYear
to get is a leap year or not
plugin IsLeapYear
.week
to get week of the year
plugin WeekOfYear
.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