moment

Moment.js

日期處理套件

安裝

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"/>
npm i --save moment

old style

//輸出 YYYY-MM-DD hh:mm:ss.mmm
function getDate() {
    var now = new Date();

    var yyyy = now.getFullYear();
    var mm = now.getMonth() + 1;
    var dd = now.getDate();

    var hh = now.getHours();
    var MM = now.getMinutes();
    var ss = now.getSeconds();
    var mmm = now.getMilliseconds();

    mm = (mm < 10) ? '0' + mm : mm;
    dd = (dd < 10) ? '0' + dd : dd;
    hh = (hh < 10) ? '0' + hh : hh;
    MM = (MM < 10) ? '0' + MM : MM;
    ss = (ss < 10) ? '0' + ss : ss;

    var today = hh.toString() + ':' + MM.toString() + ':' + ss.toString() + '.' + mmm.toString();

    return today;
}

常用指令

moment().format('YYYY-MM-DD hh:mm:ss');
moment().add(7, 'days');
moment().subtract(7, 'days');
// n years ago
moment([2007, 0, 29]).fromNow();
moment.utc();

// 設定
moment().set({'year': 2013, 'month': 3});
moment().seconds(30);

// 取得
moment().seconds(); 

// 第幾季
moment().quarter(); 

moment().startOf('month');
moment().endOf("month");

moment('2010-10-20').isBetween('2010-10-19', '2010-10-25');

延伸閱讀

results for ""

    No results matching ""