Moment.js文書

6786 ワード


Moment.js,JavaScript日付処理クラス.ブラウザとノード.jsで働くことを目的としています.
現在、以下のブラウザはciシステムに使われています.Windows 7上のIE 8、IE 9、Windows XP上の安定Chrome、Mac上のSafari 10.8、Linux上の安定Firefox.
導入方式
 Node.js
npm install moment
var moment = require('moment');
moment().format();
ブラウザ


    moment().format();
webpack
npm install moment --save
webpack.co.nfig.jsには以下のコードが入ります.
plugins: [ 
    new MomentLocalesPlugin(),  
  ],
  import moment from 'moment'
  import 'moment/locale/zh-cn' //     
使用
日付の書式設定 
Input
Example
DescriptionYYYY20144桁の年(4 digit year)YY142桁年(2 digit year)Q1..4四半期(Quarter of year.Setsmont first moth in quarter.)M MM1..12月(Month number)MMM MMMMJan..Decembermoment.locale() (  Month name in locale set by  moment.locale() )D DD1..31月の日数(Day of moneth)Do1st..31st月中の秩序日数(Day of month with ordinal)DDD DDDD1..365一年間の日数(Day of year)X1410715640.579UNixタイムスタンプ(Unix timestamp)x1410715640579UNixミリ秒タイムスタンプ(Unix ms timestamp)
 
Input
Example
Descriptiongggg2014(ローカル時間は4桁の年を表示します)Locale 4 digit week yeargg14(ローカル時間は2桁の年を表示します)Locale 2 digit week yearw ww1..53(ローカル時間は一年の54週のうち、ある週を示しています.)Locale week of yeare1..7(ローカル時間は一週間に一日表示されます.)Locale day of weekddd ddddMon...Sunday(mont.locase()メソッドに設定された週名)Day name in locale set by  moment.locale()GGGG2014(ISO標準時間は4桁の年を表示します)ISO 4 digit week yearGG14(ISO標準時間は2桁の年を表示します)ISO 2 digit week yearW WW1..53(ISO標準時間は54週のうち、ある週を示しています.)ISO week of yearE1..7(ISO標準時間は一週間に一日を表示します.)ISO day of week
 
Input
Example
DescriptionH HH0..23(24時間)24 hour timeh hh1..12(12時間)12 hour time used with  a A.a Aam pm(午前または午後)Post or ante meriemm mm0..59(分)Minutess ss0..59(秒)SecondsS0..9Tenths of a secondSS0..99Hundreds of a secondSSS0..999Thousadths of a secondSSSS0000..9999fractional secondsZ ZZ+12:00Offset from UTC as  +-HH:mm、  +-HHmm、or  Z
moment().format('MMMM Do YYYY, h:mm:ss a'); //    22  2019, 2:49:53   
moment().format('dddd');                    //    
moment().format("MMM Do YY");               // 4  22  19
moment().format('YYYY [escaped] YYYY');     // 2019 escaped 2019
moment().format();                          // 2019-04-22T14:49:53+08:00
           
相対時間
moment("20111031", "YYYYMMDD").fromNow(); // 7   
moment("20120620", "YYYYMMDD").fromNow(); // 7   
moment().startOf('day').fromNow();        // 15    
moment().endOf('day').fromNow();          // 9    
moment().startOf('hour').fromNow();       // 1    
 
カレンダー時間
moment().subtract(10, 'days').calendar(); // 2019 4 12 
moment().subtract(6, 'days').calendar();  //      2 51
moment().subtract(3, 'days').calendar();  //      2 51
moment().subtract(1, 'days').calendar();  //     2 51 
moment().calendar();                      //     2 51 
moment().add(1, 'days').calendar();       //     2 51 
moment().add(3, 'days').calendar();       //      2 51
moment().add(10, 'days').calendar();      // 2019 5 2 
 
マルチ言語サポート
 
Time
LT
8:30 PM
Time with seconds
LTS
8:30:25 PM
//Month numeral、day of moneth、year
L
09/04/1986
 
l
9/4/1986
Month name,day of mons,year
LL
September 4 1986
 
ll
Sep 4 1986
Month name,day of mons,year,time
LLL
September 4 1986 8:30 PM
 
ll
Sep 4 1986 8:30 PM
Month name,day of mons,day of week,year,time
LL
Thursday、September 4 1986 8:30 PM
 
ll
Thu,Sep 4 1986 8:30 PM
moment().format('L');    // 2019-04-22
moment().format('l');    // 2019-04-22
moment().format('LL');   // 2019 4 22 
moment().format('ll');   // 2019 4 22 
moment().format('LLL');  // 2019 4 22   2 51 
moment().format('lll');  // 2019 4 22   2 51 
moment().format('LLLL'); // 2019 4 22      2 51 
moment().format('llll'); // 2019 4 22      2 51 
時差
 
Range
キー
Sample Output(例)
0ト45 seconds
s
a few seconds ago
45ト90 seconds
m
a minute ago
90 seconds to 45 minutes
mm
2 minutes ago…45 minutes ago
45ト90 minutes
h
an hour ago
90 minutes to 22 houss
hh
2 houss ago…22 houss ago
22ト36ハウス
d
a day ago
36 houss to 25 days
dd
2 days ago…25 days ago
25ト45 days
M
a month ago
45ト345 days
MM
2 monts ago…11 months ago
345 to 545 days(1.5 years)
y
a year ago
546 days+
yy
2 years ago…20 years ago
時差(前は今が基準)
moment().fromNow();   //   
moment().fromNow(Boolean);   //   

//    true,           。
moment([2007, 0, 29]).fromNow();     // 4  
moment([2007, 0, 29]).fromNow(true); // 4 
時差前
var a = moment([2007, 0, 20]);
var b = moment([2007, 0, 29]);
a.from(b) // "9  "
時差後は今が基準です.
moment([2007, 0, 29]).toNow();     // 12   
moment([2007, 0, 29]).toNow(true); // 12  
時差後
var a = moment([2007, 0, 28]);
var b = moment([2007, 0, 29]);
a.to(b) // "in a day"
時差ミリ秒
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b) // 86400000
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // 1
デフォルトでは、moment#diffは切り下げられた数字に戻ります.浮動小数点が必要なら、trueを第三のパラメータとして伝えてください.2.0.0前に、moment铂diffは切り捨てられた数字を返します.下に丸めた数字ではありません.
var a = moment([2008, 6]);
var b = moment([2007, 0]);
a.diff(b, 'years');       // 1
a.diff(b, 'years', true); // 1.5