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 模式

相关文章
相关标签/搜索