1. 程式人生 > 其它 >小程式中使用moment.js

小程式中使用moment.js

技術標籤:小程式web app

1、下載moment.min.js

2、下載的檔案 在這裡插入圖片描述3、使用的頁面引入使用

const moment = require("../../../utils/moment")
moment.locale('en', {
  longDateFormat : {
      l: "YYYY-MM-DD",
      L: "YYYY-MM-DD HH:mm"
  }
});

自定義配置

獲取當前語言

moment.locale();

載入語言

moment.locale('zh-cn');

獲取UTC

moment().utc();

UTC偏移量

moment().utcOffset();

設定偏移量

moment().utcOffset("+08:00");
moment().utcOffset(8);
moment().utcOffset(480);

Moment和Date
相互轉換
Date ==> Moment

moment(new Date())

Moment ==> Date

moment().toDate()

是否 Moment 物件

moment.isMoment() // false
moment.isMoment(new Date()) // false
moment.isMoment(moment()) // true

是否 Date 物件

moment.isDate(); // false
moment.isDate(new Date()); // true
moment.isDate(moment()); // false

驗證日期格式是否正確

moment("not a real date").isValid(); // false

初始化日期
字串

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

支援以下格式

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

字串+格式化

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

物件方式

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});

Unix 偏移量(毫秒)

var day = moment(1318781876406);

取值

moment().unix();

Date物件

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

陣列

moment([2010, 1, 14, 15, 25, 50, 125]);
moment([2010]);   
moment([2010, 6]); 
moment([2010, 6, 10]);

取值/賦值
日期和時間

// 毫秒
moment().millisecond(Number);
moment().millisecond(); // Number
moment().milliseconds(Number);
moment().milliseconds(); // Number
// 秒
moment().second(Number);
moment().second(); // Number
moment().seconds(Number);
moment().seconds(); // Number
// 分鐘
moment().minute(Number);
moment().minute(); // Number
moment().minutes(Number);
moment().minutes(); // Number
// 小時
moment().hour(Number);
moment().hour(); // Number
moment().hours(Number);
moment().hours(); // Number
// 日期
moment().date(Number);
moment().date(); // Number
moment().dates(Number);
moment().dates(); // Number

年月日時分秒

// 取值
moment().get('year');
moment().get('month');  // 0 to 11
moment().get('date');
moment().get('hour');
moment().get('minute');
moment().get('second');
moment().get('millisecond');
// 賦值
moment().set('year', 2013);
moment().set('month', 3);  // April
moment().set('date', 1);
moment().set('hour', 13);
moment().set('minute', 20);
moment().set('second', 30);
moment().set('millisecond', 123);

moment().set({'year': 2013, 'month': 3});

星期的取值和賦值
週六為6
週日為0

moment().day(-7); // last Sunday (0 - 7)
moment().day(7); // next Sunday (0 + 7)
moment().day(10); // next Wednesday (3 + 7)
moment().day(24); // 3 Wednesdays from now (3 + 7 + 7 + 7)

按區域標準

// 比如週一是一星期的第一天
moment().weekday(-7); // last Monday
moment().weekday(7); // next Monday

時間操作

keyShorthand
yearsy
quartersQ
monthM
weeksw
daysd
hoursh
minutesm
secondss
millisecondsms

加法

moment().add(7, 'days').add(1, 'months'); // with chaining
moment().add(7, 'd');
moment().add({days:7,months:1}); // with object literal

減法

moment().subtract(7, 'days');

顯示

moment().format();                                // "2014-09-08T08:02:17-05:00" (ISO 8601)
moment().format("YYYY-MM-DD hh:mm:ss");           // "2014-09-08 02:17:10"
moment().format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Sunday, February 14th 2010, 3:25:50 pm"
moment().format("ddd, hA");                       // "Sun, 3PM"
moment('gibberish').format('YYYY MM DD');         // "Invalid date"

排除字元

moment().format('[today] dddd'); // 'today Sunday'

比較

//之前
moment('2010-10-20').isBefore('2010-10-21'); // true
//相同
moment('2010-10-20').isSame('2010-10-20'); // true
//之後
moment('2010-10-20').isAfter('2010-10-19'); // true
//之間
moment('2010-10-20').isBetween('2010-10-19', '2010-10-25'); // true
// 是否閏年
moment().isLeapYear();

物件克隆

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