javascript中的日期时间操作

date: 2018.05.28; modification:2018.05.30

目录:

1 使用moment模块处理时间

1.1 获取时间数值


    // 年月日, 时分秒
    let now = moment();
    let year   = now.year();
    let month  = now.month() + 1;
    let day    = now.date();
    let hour   = now.hour();
    let minute = now.minute();
    let second = now.second();
    let ms     = now.millisecond();

    // 其他数值
    now.dayOfYear();    // 一年中的第几天
    now.isoWeek();      // 一年中的第几周
    now.weekday();      // 一周中的第几天(local)
    now.quarter();      // 一周中的第几季度

    console.log('now:', now);
    console.log(year, month, day, hour, minute, second);

1.2 格式化时间


    console.log(moment().format('YYYY-MM-DD HH:mm:ss')); // 2018-05-30 10:16:30

常用格式:

year
                    YY          70 71 ... 29 30
                    YYYY        1970 1971 ... 2029 2030
month
                    M           1 2 ... 11 12
                    MM          01 02 ... 11 12
day of month
                    D           1 2 ... 30 31
                    DD          01 02 ... 30 31
hour
                    H           0 1 ... 22 23
                    HH          00 01 ... 22 23
                    h           1 2 ... 11 12
                    hh          01 02 ... 11 12
minute
                    m           0 1 ... 58 59
                    mm          00 01 ... 58 59
second
                    s           0 1 ... 58 59
                    ss          00 01 ... 58 59
day of week
                    d           0 1 ... 5 6
                    e           0 1 ... 5 6 (Locale)
                    E           1 2 ... 6 7 (ISO)
                    dd          Su Mo ... Fr Sa
                    ddd         Sun Mon ... Fri Sat
                    dddd        Sunday Monday ... Friday Saturday
day of Year
                    DDD         1 2 ... 364 365
                    DDDD        001 002 ... 364 365
week of year
                    w           1 2 ... 52 53
                    ww          01 02 ... 52 53
quarter
                    Q           1 2 3 4
AM/PM
                    A           AM PM
                    a           am pm
fractional second
                    S           0 1 ... 8 9
                    SSS         000 001 ... 998 999
timezone
                    Z           -07:00 -06:00 ... +06:00 +07:00
                    ZZ          -0700 -0600 ... +0600 +0700 
unix millisecond    timestamp
                    x           1360013296123

1.3 产生日期时间


    let day = moment('2018-05-28');
    let day = moment([2018, 5, 28]);

常用日期格式:

2018-05-30 09:30         // An hour and minute time part
2018-05-30 09:30:26      // An hour, minute, and second time part
2018-05-30 09:30:26.123  // An hour, minute, second, and millisecond time part
2018-05-30 24:00:00.000  // hour 24, minute, second, millisecond equal 0 means next day at midnight

1.4 计算

1.4.1 加减


    moment().add(7, 'days');
    moment().subtract(7, 'days');
    // 可能的描述字: years, months, weeks, days, hours, minutes, seconds, milliseconds, quarters

1.4.2 起始结束时间

    
    moment().startOf('year');
    moment().endOf('year');
    // 可能的描述字: year, month, quarter, week, isoWeek, day, hour, minute, second

1.4.3 差值


    var a = moment([2007, 0, 29]);
    var b = moment([2007, 0, 28]);
    a.diff(b) // 86400000

1.4.4 比较大小


    let max = moment.max(m1, m2, m3);
    let min = moment.min(m1, m2, m3);

1.5 本地化


    moment.locale('zh-cn');

1.6 与Date之间相互转换

1.6.1 Date 转 moment


    let d = new Date('2010-01-01');
    let m = moment(d);
    console.log('d:', d);
    console.log('m:', m);

1.6.2 moment 转 Date


    moment().toDate();

2 参考资料

Moment.js中文文档: http://momentjs.cn/docs Moment.js英文文档: http://momentjs.com/docs