Moment.js 文档

 

Moment.js ,JavaScript 日期处理类库。 旨在在浏览器和Node.js中工作。

目前,以下浏览器用于ci系统:Windows 7上的IE8,IE9,Windows XP上的稳定Chrome,Mac上的Safari 10.8和Linux上的稳定Firefox。

引入方式

 Node.js

npm install moment
var moment = require('moment');
moment().format();

浏览器

<script src="moment.js"></script>
<script>
    moment().format();
</script>

webpack

npm install moment --save

webpack.config.js中加入以下代码

plugins: [ 
    new MomentLocalesPlugin(),  
  ],
  import moment from 'moment'
  import 'moment/locale/zh-cn' //引入中文包

使用

日期格式化 

InputExampleDescription
YYYY20144位数年份(4 digit year)
YY142位数年份(2 digit year)
Q1..4季度(Quarter of year. Sets month to first month in quarter.)
M MM1..12月(Month number)
MMM MMMMJan..Decembermoment.locale()方法中设置的月份名称( Month name in locale set by moment.locale() )
D DD1..31月份中的天数(Day of month)
Do1st..31st月份中的有序天数(Day of month with ordinal)
DDD DDDD1..365一年中的天数(Day of year)
X1410715640.579UNix时间戳(Unix timestamp)
x1410715640579UNix毫秒时间戳(Unix ms timestamp)

 

InputExampleDescription
gggg2014(本地时间显示4位数年)Locale 4 digit week year
gg14(本地时间显示2位数年)Locale 2 digit week year
w ww1..53(本地时间显示一年中的54周中某一周)Locale week of year
e1..7(本地时间显示一周中某一天)Locale day of week
ddd ddddMon...Sunday(monent.localse()方法中设置的周名称)Day name in locale set by moment.locale()
GGGG2014(ISO标准时间显示4位数年)ISO 4 digit week year
GG14(ISO标准时间显示2位数年)ISO 2 digit week year
W WW1..53(ISO标准时间显示54周中某一周)ISO week of year
E1..7(ISO标准时间显示一周中某一天)ISO day of week

 

InputExampleDescription
H HH0..23(24小时时间)24 hour time
h hh1..12(12小时时间)12 hour time used with a A.
a Aam pm(上午或下午)Post or ante meridiem
m mm0..59(分钟)Minutes
s ss0..59(秒)Seconds
S0..9Tenths of a second
SS0..99Hundreds of a second
SSS0..999Thousandths of a second
SSSS0000..9999fractional seconds
Z ZZ+12:00Offset from UTC as +-HH:mm+-HHmm, or Z
moment().format('MMMM Do YYYY, h:mm:ss a'); // 四月 22日 2019, 2:49:53 下午
moment().format('dddd');                    // 星期一
moment().format("MMM Do YY");               // 4月 22日 19
moment().format('YYYY [escaped] YYYY');     // 2019 escaped 2019
moment().format();                          // 2019-04-22T14:49:53+08:00

           

相对时间

moment("20111031", "YYYYMMDD").fromNow(); // 7 年前
moment("20120620", "YYYYMMDD").fromNow(); // 7 年前
moment().startOf('day').fromNow();        // 15 小时前
moment().endOf('day').fromNow();          // 9 小时内
moment().startOf('hour').fromNow();       // 1 小时前

 

日历时间

moment().subtract(10, 'days').calendar(); // 2019年4月12日
moment().subtract(6, 'days').calendar();  // 上周二下午2点51
moment().subtract(3, 'days').calendar();  // 上周五下午2点51
moment().subtract(1, 'days').calendar();  // 昨天下午2点51分
moment().calendar();                      // 今天下午2点51分
moment().add(1, 'days').calendar();       // 明天下午2点51分
moment().add(3, 'days').calendar();       // 本周四下午2点51
moment().add(10, 'days').calendar();      // 2019年5月2日

 

多语言支持

 

TimeLT8:30 PM
Time with secondsLTS8:30:25 PM
/Month numeral, day of month, yearL09/04/1986
 l9/4/1986
Month name, day of month, yearLLSeptember 4 1986
 llSep 4 1986
Month name, day of month, year, timeLLLSeptember 4 1986 8:30 PM
 lllSep 4 1986 8:30 PM
Month name, day of month, day of week, year, timeLLLLThursday, September 4 1986 8:30 PM
 llllThu, Sep 4 1986 8:30 PM
moment().format('L');    // 2019-04-22
moment().format('l');    // 2019-04-22
moment().format('LL');   // 2019年4月22日
moment().format('ll');   // 2019年4月22日
moment().format('LLL');  // 2019年4月22日下午2点51分
moment().format('lll');  // 2019年4月22日下午2点51分
moment().format('LLLL'); // 2019年4月22日星期一下午2点51分
moment().format('llll'); // 2019年4月22日星期一下午2点51分

时差

 

RangeKeySample Output(范例)
0 to 45 secondssa few seconds ago
45 to 90 secondsma minute ago
90 seconds to 45 minutesmm2 minutes ago ... 45 minutes ago
45 to 90 minuteshan hour ago
90 minutes to 22 hourshh2 hours ago ... 22 hours ago
22 to 36 hoursda day ago
36 hours to 25 daysdd2 days ago ... 25 days ago
25 to 45 daysMa month ago
45 to 345 daysMM2 months ago ... 11 months ago
345 to 545 days (1.5 years)ya year ago
546 days+yy2 years ago ... 20 years ago

时差(之前,现在为基准)

moment().fromNow();   //几秒前
moment().fromNow(Boolean);   //几秒 

//如果传递true,则可以获得不带后缀的值。
moment([2007, 0, 29]).fromNow();     // 4年前
moment([2007, 0, 29]).fromNow(true); // 4年

时差之前

var a = moment([2007, 0, 20]);
var b = moment([2007, 0, 29]);
a.from(b) // "9天前"

时差之后现在为基准

moment([2007, 0, 29]).toNow();     // 12 年内
moment([2007, 0, 29]).toNow(true); // 12 年

时差之后

var a = moment([2007, 0, 28]);
var b = moment([2007, 0, 29]);
a.to(b) // "in a day"

时差毫秒

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

默认情况下,moment#diff将返回向下舍入的数字。 如果需要浮点数,请将true作为第三个参数传递。 在2.0.0之前,moment#diff返回舍入的数字,而不是向下舍入的数字。

var a = moment([2008, 6]);
var b = moment([2007, 0]);
a.diff(b, 'years');       // 1
a.diff(b, 'years', true); // 1.5

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值