Skip to content

Latest commit

 

History

History
530 lines (357 loc) · 12 KB

API-reference.md

File metadata and controls

530 lines (357 loc) · 12 KB

API Reference

Day.js は組み込みの Date.prototype を変更する代わりに Dayjs オブジェクトと呼ばれる Date オブジェクトのラッパーを作成します。

Dayjs オブジェクトは不変 (immutable) です。すなわち、すべての API 操作は新しい Dayjs オブジェクトを返します。


Day.js は指定されない限り、常に新しい Dayjs オブジェクトを返します。

Parse

サポートされている入力形式のいずれかで dayjs() を呼び出します。

Now

現在の日付と時間を取得するには、パラメータなしで dayjs() を呼び出します。

dayjs();

String

ISO 8601 に準拠する形式から作成します。

dayjs(String);
dayjs('1995-12-25');

Date

JavaScript の組み込みの Date オブジェクトを渡します。

dayjs(Date);
dayjs(new Date(2018, 8, 18));

Unix Timestamp (milliseconds)

Unix エポック (1970年1月1日 12:00AM UTC) 以降のミリ秒数を表す整数値を渡します。

dayjs(Number);
dayjs(1318781876406);

Unix Timestamp (seconds)

dayjs.unix(Number);
dayjs.unix(1318781876);

Clone

すべての Dayjs は不変 (immutable) です。 オブジェクトのコピーが必要な場合は、 .clone() を呼び出してください。 Dayjs オブジェクトに対して dayjs() を呼び出すと、それもクローンされます。

dayjs(Dayjs);
dayjs().clone();

Validation

  • Boolean を返します

Dayjs オブジェクトが有効な日付かどうかをチェックします。

dayjs().isValid();

Get + Set

日付の取得と設定です。

Year

  • Number を返します

年を取得します。

dayjs().year();

Month

  • Number を返します

月を取得します。

dayjs().month();

Date of Month

  • Number を返します

日を取得します。

dayjs().date();

Day of Week

  • Number を返します

曜日を取得します。

dayjs().day();

Hour

  • Number を返します

時間を取得します。

dayjs().hour();

Minute

  • Number を返します

分を取得します。

dayjs().minute();

Second

  • Number を返します

秒を取得します。

dayjs().second();

Millisecond

  • Number を返します

ミリ秒を取得します。

dayjs().millisecond();

Set

日付のセッターです。 単位は大文字・小文字を区別しません。

dayjs().set((unit: String), (value: Int));
dayjs().set('date', 1);
dayjs().set('month', 3); // 4月
dayjs().set('second', 30);

List of all available units

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

Manipulate

Dayjs オブジェクトは次のような方法で操作することができます:

dayjs()
  .startOf('month')
  .add(1, 'day')
  .subtract(1, 'year');

Add

時間を足して新しい Dayjs オブジェクトを返します。

dayjs().add((value: Number), (unit: String));
dayjs().add(7, 'day');

Subtract

時間を引いて新しい Dayjs オブジェクトを返します。 dayjs#add と全く同じです。

dayjs().subtract((value: Number), (unit: String));
dayjs().subtract(7, 'year');

Start of Time

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');

End of Time

ある単位の終わりの時間の新しい Dayjs オブジェクトを返します。

dayjs().endOf((unit: String));
dayjs().endOf('month');

Display

パースや操作が完了したならば Dayjs オブジェクトを表示する方法が求められるはずです。

Format

  • 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 午前と午後 (小文字)

Difference

  • 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

Unix Timestamp (milliseconds)

  • Number を返します

Unix エポックからのミリ秒数を出力します。

dayjs().valueOf();

Unix Timestamp (seconds)

  • Number を返します

Unix タイムスタンプ (Unix エポックからの秒数) を出力します。

dayjs().unix();

Days in Month

  • Number を返します

その月の日数を取得します。

dayjs().daysInMonth();

As Javascript Date

  • Javascript の Date オブジェクトを返します

Dayjs オブジェクトから組み込みの Date オブジェクトのコピーを取得します。

dayjs().toDate();

As Array

  • Array を返します

Date コンストラクタパラメータに対応する値の配列を返します。

dayjs().toArray(); //[2018, 8, 18, 00, 00, 00, 000];

As JSON

  • JSON String を返します

Serializing a Dayjs object to JSON, will return an ISO8601 string.

Dayjs オブジェクトを JSON シリアライズし、ISO8601 形式の日付文字列を返します。

dayjs().toJSON(); //"2018-08-08T00:00:00.000Z"

As ISO 8601 String

  • String を返します

ISO8601 形式の文字列にフォーマットします。

dayjs().toISOString();

As Object

  • Object を返します

年、月、...(中略)...、ミリ秒のオブジェクトを返します。

dayjs().toObject(); // { years:2018, months:8, date:18, hours:0, minutes:0, seconds:0, milliseconds:0}

As String

  • String を返します
dayjs().toString();

Query

Is Before

  • Boolean を返します

Dayjs オブジェクトが別の Dayjs オブジェクト以前の値かどうかを判定します。

dayjs().isBefore(Dayjs, unit? : String);
dayjs().isBefore(dayjs()); // false
dayjs().isBefore(dayjs(), 'year'); // false

Is Same

  • Boolean を返します

Dayjs オブジェクトが別の Dayjs オブジェクトの値と等しいかどうかを判定します。

dayjs().isSame(Dayjs, unit? : String);
dayjs().isSame(dayjs()); // true
dayjs().isSame(dayjs(), 'year'); // true

Is After

  • Boolean を返します

Dayjs オブジェクトが別の Dayjs オブジェクト以降の値かどうかを判定します。

dayjs().isAfter(Dayjs, unit? : String);
dayjs().isAfter(dayjs()); // false
dayjs().isAfter(dayjs(), 'year'); // false

Is a Dayjs .isDayjs(compared: any)

Returns a boolean indicating whether a variable is a dayjs object or not.

dayjs.isDayjs(dayjs()); // true
dayjs.isDayjs(new Date()); // false

Plugin APIs

RelativeTime

.from .to .fromNow .toNow to get relative time

plugin RelativeTime

IsLeapYear

.isLeapYear to get is a leap year or not

plugin IsLeapYear

WeekOfYear

.week to get week of the year

plugin WeekOfYear

IsSameOrAfter

.isSameOrAfter to check if a date is same of after another date

plugin IsSameOrAfter

IsSameOrBefore

.isSameOrBefore to check if a date is same of before another date.

plugin IsSameOrBefore

IsBetween

.isBetween to check if a date is between two other dates

plugin IsBetween