小程序中使用moment.js

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值