moment.js

中文官網: http://momentjs.cn/timezone/javascript

 

1.建立一個moment對象java

 

1.1無參構造如:  moment();git

     var new = moment();數組

 

1.2 字符串構造如: moment(String);app

    使用 isValid() 方法驗證字符串格式是否正確如: moment("not a real date").isValid()this

1.2.1 字符串構造參數,注意時間格式必須爲 iso 8601 標準格式。若是格式不匹配,將使用 new Date(String) 建立spa

var day = moment("1995-12-25");unix

 

一個 iso 8601 的時間格式必須有日期 部分code

2013-02-08  # A calendar date part
2013-W06-5  # A week date part
orm

2013-039    # An ordinal date part

1.2.2 也能夠包含時間 (不是必須),分割日期符號爲一個 空格 或者是一個 大寫的T

2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

1.2.3 也能夠只有一個時間 如:

2013-02-08 09  # A calendar date part and hour time part
2013-W06-5 09  # A week date part and hour time part

2013-039 09    # An ordinal date part and hour time part

1.2.4 包含時間時也能夠用UTC格式如:

2013-02-08 09+07:00            # +-HH:mm
2013-02-08 09-0100             # +-HHmm
2013-02-08 09Z                 # Z

2013-02-08 09:30:26.123+07:00  # +-HH:mm

 

1.3 字符串 + 格式 如:  moment(String,String)

moment(String, String);
moment(String, String, String);
moment(String, String, Boolean);

moment(String, String, String, Boolean);

當你已經知道了當前時間的格式了,你就可能使用這種方式來構建一個moment

moment("12-25-1995", "MM-DD-YYYY");

1.3.1 解析時moment將忽略非數字的字符,因此下面的結果是同樣的

moment("12-25-1995", "MM-DD-YYYY");

moment("12/25/1995", "MM-DD-YYYY");

1.3.2 除非你指定了時區,不然解析時將以當前的時區建立日期

moment("2010-10-20 4:30",       "YYYY-MM-DD HH:mm");   // parsed as 4:30 local time

moment("2010-10-20 4:30 +0000", "YYYY-MM-DD HH:mm Z"); // parsed as 4:30 UTC

1.3.3 若是解析後的字符串時間不存在,isValid方法將返回false

moment("2010 13",           "YYYY MM").isValid();     // false (not a real month)
moment("2010 11 31",        "YYYY MM DD").isValid();  // false (not a real day)
moment("2010 2 29",         "YYYY MM DD").isValid();  // false (not a leap year)

moment("2010 notamonth 29", "YYYY MMM DD").isValid(); // false (not a real month name)

1.3.4 moment對提供的時間字符的要求是很是寬鬆的,這樣可能致使一些你不想看到的結果,在2.3.0版本中提供了一種精確匹配的方法,

你只要在參數後面加上一個布爾值就可如:

moment('It is 2012-05-25', 'YYYY-MM-DD').isValid();       // true
moment('It is 2012-05-25', 'YYYY-MM-DD', true).isValid(); // false
moment('2012-05-25',       'YYYY-MM-DD', true).isValid(); // true

 

1.4 字符串 + 多個格式 

moment(String, String[], String, Boolean);

如:

moment("12-25-1995", ["MM-DD-YYYY", "YYYY-MM-DD"]);

格式匹配規則

1.優先匹配一個有效的日期格式

2.優先匹配長的日期格式

3.優先精確匹配

4.就近格式匹配

 

下面是精確匹配

moment("29-06-1995", ["MM-DD-YYYY", "DD-MM-YYYY"], 'fr');       // uses 'fr' locale
moment("29-06-1995", ["MM-DD-YYYY", "DD-MM-YYYY"], true);       // uses strict parsing
moment("05-06-1995", ["MM-DD-YYYY", "DD-MM-YYYY"], 'fr', true); // uses 'fr' locale and strict parsing

 

1.5 使用對象建立 moment({unit:value,...})

     你能夠使用指定了單元的對象來建立一個moment對象,省略的單元默認值爲 0 或當前天,月,年

     day 和 date:都是指定 一個月中的某天

moment({ hour:15, minute:10 });
moment({ y    :2010, M     :3, d   :5, h    :15, m      :10, s      :3, ms          :123});
moment({ year :2010, month :3, day :5, hour :15, minute :10, second :3, millisecond :123});
moment({ years:2010, months:3, days:5, hours:15, minutes:10, seconds:3, milliseconds:123});

moment({ years:2010, months:3, date:5, hours:15, minutes:10, seconds:3, milliseconds:123});

注意: 像   moment(Array)   new Date(year, month, date),  月份都是從0開始

 

 

1.6 使用 long 建立一個moment (unix 偏移量毫秒),像 new Date(Number)

var day = moment(1318781876406);

1.7 使用時間戳建立一個 moment (秒)

moment.unix(Number)

var day = moment.unix(1318781876.721);

1.8 使用Javascript Date 建立

moment(Date);

var day = new Date(2011, 9, 16);

var dayWrapper = moment(day);

1.9 數組

moment(Number[]);

例:

[year, month, day, hour, minute, second, millisecond]
moment([2010, 1, 14, 15, 25, 50, 125]); // February 14th, 3:25:50.125 PM

1.9.1 若是不指定的值默認爲最低值如:

moment([2010]);        // January 1st
moment([2010, 6]);     // July 1st

moment([2010, 6, 10]); // July 10th

1.9.2 使用數組作構造參數建立的日期時區是當前的,用utc建立日期可以使用  moment.utc(Number[]).

moment.utc([2010, 1, 14, 15, 25, 50, 125]);

1.9.3 數組建立是基於javascript new Date(Number) 方法,參數 月,時,分,秒,毫秒 都是從0 開始計算。年,天 是從1開始計算

若是當前數組表示的日期不存在,那麼 , moment#isValid 將返回 false

moment([2010, 13]).isValid();     // false (not a real month)

moment([2010, 10, 31]).isValid(); // false (not a real day)

moment([2010, 1, 29]).isValid();  // false (不是閏年)

1.10 使用moment 建立一個moment (克隆)

     moment(Moment)

1.10.1 全部的 moment 都是易變的,若是你想克隆一個 moment對象,你能夠明確的指定,使用目標作爲一個參數建立,或用 clone方法

var a = moment([2012]);
var b = moment(a);
a.year(2000);

b.year(); // 2012

var a = moment([2012]);

var b = a.clone();

a.year(2000);

b.year(); // 2012

 

1.11. 局布單元設置值 moment("15","hh")

說明:若是你建立一個moment只爲一些單元指定了值,那麼剩餘的單元的值爲:年,月,日 是當前時間。時,分,秒,毫秒。爲 0

1.11.1 如:指定了時,分,秒。那麼默認的日期爲今天。若是都未指定,那麼表示當前時刻等於 javascript new Date()

 

moment(5, "HH");  // today, 5:00:00.000
moment({hour: 5});  // today, 5:00:00.000
moment({hour: 5, minute: 10});  // today, 5:10.00.000
moment({hour: 5, minute: 10, seconds: 20});  // today, 5:10.20.000

moment({hour: 5, minute: 10, seconds: 20, milliseconds: 300});  // today, 5:10.20.300

1.11.2 當指定了,那麼年,月爲當前時間值,時,分,秒,毫秒爲0

moment(5, "DD");  // this month, 5th day-of-month
moment("4 05:06:07", "DD hh:mm:ss");  // this month, 4th day-of-month, 05:06:07.000

 

 

下面是 年,月,日的符號

Input Example Description
YYYY

2014

年,4-2位的數字

YY 14

2位的年表示

Q 1..4 Quarter of year. Sets month to first month in quarter.
M MM

1..12

月,M:一位如:1,MM: 兩位如:01 

MMM MMMM

Jan..December

月份的名字能夠經過 moment.locale()設置,MMM:爲縮略寫法。

MMMM:爲長月份表示

D DD

1..31

Day of month

Do 1st..31st Day of month with ordinal
DDD DDDD 1..365 Day of year
X 1410715640.579 Unix timestamp
x 1410715640579

Unix ms timestamp

週年,周,週日 的符號

Input Example Description
gggg 2014 Locale 4 digit week year
gg 14 Locale 2 digit week year
w ww 1..53 Locale week of year
e 1..7 Locale day of week
ddd dddd Mon...Sunday Day name in locale set by moment.locale()
GGGG 2014 ISO 4 digit week year
GG 14 ISO 2 digit week year
W WW 1..53 ISO week of year
E 1..7

ISO day of week

時、分、少表示

Input Example Description
H HH 0..23 24 hour time
h hh 1..12 12 hour time used with a A.
a A am pm Post or ante meridiem
m mm 0..59 Minutes
s ss 0..59 Seconds
S 0..9 Tenths of a second
SS 0..99 Hundreds of a second
SSS 0..999 Thousandths of a second
SSSS 0000..9999 fractional seconds
Z ZZ +12:00

Offset from UTC as +-HH:mm+-HHmm, or Z

 

 

 

2. 賦值/取值

 

 

 

 

. 合法性校驗 moment#valid

你能夠使用  moment#invalidAt  去檢驗哪一個單元出現了錯誤如:

var m = moment("2011-10-10T10:20:90");

m.isValid(); // false

m.invalidAt(); // return 5  for seconds

返回結果表示 :

  1. 0 years
  2. 1 months
  3. 2 days
  4. 3 hours
  5. 4 minutes
  6. 5 seconds
  7. 6 milliseconds

注意: 若是有多處單元出錯,那麼將返回第一個單元的錯誤

 

 

 

 

2.1 指定UTC

moment.utc();
moment.utc(Number);
moment.utc(Number[]);
moment.utc(String);
moment.utc(String, String);
moment.utc(String, String[]);
moment.utc(String, String, String);
moment.utc(Moment);

moment.utc(Date);

2.1.1 moment 默認解析和輸入是用本地時間,若是你想使用UTC建立,那麼你要用  moment.utc() 替換 moment().方法。

當moment爲一個UTC模式,那麼其輸入的樣式也將是UTC時間如:

moment().format();     // 2013-02-04T10:35:24-08:00

moment.utc().format(); // 2013-02-04T18:35:24+00:00

2.1.2 此外,若是是UTC模式,那麼全部的getter,setter默認爲javascript Date 中  Date#getUTC* andDate#setUTC*  而不是  Date#get* 和 Date#set* 

方法

moment.utc().seconds(30) === new Date().setUTCSeconds(30);

moment.utc().seconds()   === new Date().getUTCSeconds();

2.1.3  雖然它們輸入的樣子與非UTC模式的時間不一樣,但它們都表示相同的時間如:

var a = moment();
var b = moment.utc();
a.format();  // 2013-02-04T10:35:24-08:00
b.format();  // 2013-02-04T18:35:24+00:00
a.valueOf(); // 1360002924000

b.valueOf(); // 1360002924000

2.1.4 從UTC模式到非UTC模式的之間的轉換能夠使用  moment#utc  和   moment#local 

var a = moment.utc([2011, 0, 1, 8]);

a.hours(); // 8 UTC 模式

a.local(); // 進行切換

a.hours(); // 0 非 UTC 模式

相關文章
相關標籤/搜索