moment.js 日期包装类

Moment.js

1创建时间对象

moment();                                                             当前时间

moment(new Date(2020, 3, 7));                              date 对象创建

moment(1318781876406);                                     时间戳创建

moment("12-25-1995", "MM-DD-YYYY");                   日期文字+日期格式

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

moment([year, month , date , hours , minutes , seconds , milliseconds ]); 数组创建

moment.unix(Number)                   unix时间

日期格式表

Input

Output

M or MM

Month Number (1 - 12)

MMM or MMMM

Month Name (In currently language set by `moment.lang()`)

D or DD

Day of month

DDD or DDDD

Day of year

d, dd, ddd, or dddd

Day of week (NOTE: these tokens are not used to create the date, as there are 4-5 weeks in a month, and it would be impossible to get the date based off the day of the week)

YY

2 digit year (if greater than 70, will return 1900's, else 2000's)

YYYY

4 digit year

a or A

AM/PM

H, HH

24 hour time

h, or hh

12 hour time (use in conjunction with a or A)

m or mm

Minutes

s or ss

Seconds

S

Deciseconds (1/10th of a second)

SS

Centiseconds (1/100th of a second)

SSS

Milliseconds (1/1000th of a second)

Z or ZZ

Timezone offset as `+0700` or `+07:30` Available in version *1.2.0*

Key

Shorthand

year

y

month

M

week

w

day

d

hour

h

minutes

m

seconds

s

millisecond

ms

2 修改时间对象

2.1 加减

moment().add(1000)                         加毫秒

moment().add('days', 7)                     加7天

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

moment().add({days:-7,months:1}); 加1月,减7天

2.2设值

moment().seconds(30);       year/month/date/day/minutes/seconds 方法

moment().seconds();           取值

设首/尾

moment().startOf('year'); // set to Jan 1 12:00:00.000 pm this year

moment().startOf('month'); // set to the first of this month, 12:00:00.000 pm

moment().startOf('day'); // set to 12:00:00.000 pm today

moment().startOf('hour'); // set to now, but with 0 mins, 0 secs, and 0 ms

moment().startOf('minute'); // set to now, but with 0 seconds and 0 milliseconds

moment().startOf('second'); // same as moment().milliseconds(0);

moment().endOf('year');

moment().sod(); 当天头

moment().eod(); 当天尾

3       格式化输出

moment().format("dddd, MMMM Do YYYY, h:mm:ss a");

Token

Output

Month

M

1 2 ... 11 12

Mo

1st 2nd ... 11th 12th

MM

01 02 ... 11 12

MMM

Jan Feb ... Nov Dec

MMMM

January February ... November December

Day of Month

D

1 2 ... 30 30

Do

1st 2nd ... 30th 31st

DD

01 02 ... 30 31

Day of Year

DDD

1 2 ... 364 365

DDDo

1st 2nd ... 364th 365th

DDDD

001 002 ... 364 365

Day of Week

d

0 1 ... 5 6

do

0th 1st ... 5th 6th

ddd

Sun Mon ... Fri Sat

dddd

Sunday Monday ... Friday Saturday

Week of Year

w

1 2 ... 52 53

wo

1st 2nd ... 52nd 53rd

ww

01 02 ... 52 53

Year

YY

70 71 ... 29 30

YYYY

1970 1971 ... 2029 2030

AM/PM

A

AM PM

a

am pm

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

Timezone

z or zz

EST CST ... MST PST **Note:** as of *1.6.0*, the z/zz format tokens have been deprecated. [Read more about it here.](https://github.com/timrwood/moment/issues/162)

Z

-07:00 -06:00 ... +06:00 +07:00

ZZ

-0700 -0600 ... +0600 +0700 Z and ZZ are available in version *1.2.0*

Localized date format

LT

8:30 PM LT is available in version *1.3.0*

L

07/10/1986

LL

July 10 1986

LLL

July 10 1986 8:30 PM

LLLL

Saturday, July 10 1986 8:30 PM L, LL, LLL, LLLL are available in version *1.3.0*

4       比较

var a = moment([2007, 0, 29]);

var b = moment([2007, 0, 28]);

  1. a.diff(b, 'days') // 1

5       汉化

moment.lang('zh-cn', {

months : "一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月".split("_"),

monthsShort : "1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月".split("_"),

weekdays : "星期日_星期一_星期二_星期三_星期四_星期五_星期六".split("_"),

weekdaysShort : "周日_周一_周二_周三_周四_周五_周六".split("_"),

weekdaysMin : "日_一_二_三_四_五_六".split("_"),

longDateFormat : {

LT : "Ah点mm",

L : "YYYY年MMMD日",

LL : "YYYY年MMMD日",

LLL : "YYYY年MMMD日LT",

LLLL : "YYYY年MMMD日ddddLT",

l : "YYYY年MMMD日",

ll : "YYYY年MMMD日",

lll : "YYYY年MMMD日LT",

llll : "YYYY年MMMD日ddddLT"

},

meridiem : function (hour, minute, isLower) {

if (hour < 9) {

return "早上";

} else if (hour < 11 && minute < 30) {

return "上午";

} else if (hour < 13 && minute < 30) {

return "中午";

} else if (hour < 18) {

return "下午";

} else {

return "晚上";

}

},

calendar : {

sameDay : '[今天]LT',

nextDay : '[明天]LT',

nextWeek : '[下]ddddLT',

lastDay : '[昨天]LT',

lastWeek : '[上]ddddLT',

sameElse : 'L'

},

relativeTime : {

future : "%s内",

past : "%s前",

s : "几秒",

m : "1分钟",

mm : "%d分钟",

h : "1小时",

hh : "%d小时",

d : "1天",

dd : "%d天",

M : "1个月",

MM : "%d个月",

y : "1年",

yy : "%d年"

}

});

6       重写扩展

moment.months monthsShort weekdays weekdaysShort weekdaysMin longDateFormat relativeTime 扩展自定义

7       其他

零碎的api

clone                    复制

isValid                  验证合法

daysInMonth     每月天数

isDST                   是否休息日

isLeapYear            闰年

官方文档:http://momentjs.com/docs

源码下载:https://github.com/timrwood/moment/

moment.js 日期包装类 (说明示例)的更多相关文章

  1. 一个JS的日期格式化算法示例

    一个JS的日期格式化算法. 例子: <script> /** * Js日期格式化算法实例 * by www.jbxue.com */ function dateFormat(date, f ...

  2. C# MVC 用户登录状态判断 【C#】list 去重(转载) js 日期格式转换(转载) C#日期转换(转载) Nullable<System.DateTime>日期格式转换 (转载) Asp.Net MVC中Action跳转(转载)

    C# MVC 用户登录状态判断   来源:https://www.cnblogs.com/cherryzhou/p/4978342.html 在Filters文件夹下添加一个类Authenticati ...

  3. Js 日期转换函数(UTC时间转换及日期想加减)

    IOS上Js日期转换中new Date("yyyy-mm-dd")不能正常工作,必须使用new Date("yyyy/MM/dd"); 日期相加减: Date. ...

  4. 移动端lCalendar纯原生js日期时间选择器

    网上找过很多的移动端基于zepto或jquery的日期选择器,在实际产品中也用过一两种,觉得都不太尽如人意,后来果断选择了H5自己的日期input表单,觉得还可以,至少不用引用第三方插件了,性能也不错 ...

  5. 161226、js日期格式化

    JavaScript Date format(js日期格式化) 方法一:// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季 ...

  6. 【JavaScript】 knockout.js 日期格式化借助【momentjs】

    源:Knockout.js 日期格式化 源:momentjs

  7. js 日期处理,json处理

    模块化js :requirejshttp://www.requirejs.cn/ 好用的日期控件:http://www.bootcss.com/p/bootstrap-datetimepicker/i ...

  8. JS获取当前日期时间及JS日期格式化

    Js获取当前日期时间: var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份( ...

  9. js日期时间比较函数

    转自:http://www.cnblogs.com/zxjyuan/archive/2010/09/07/1820708.html js日期比较(yyyy-mm-dd) function duibi( ...

随机推荐

  1. Python模块学习 - Functools

    Functools模块 Higher-order functions and operations on callable objects,看这个标题我都是懵逼的,这都是啥啥啥啊,赶紧拿出百度翻译:可 ...

  2. 网络知识===关于MAC地址和IP不能互相替代,缺一不可的原因

    最近在看书<图解TCP/IP>书中分别谈到了IP和MAC地址.于是我就有两个疑惑, 为什么有了IP地址,我们还要获取MAC地址? 为什么我们初始不直接使用MAC地址作为终点地址?还要那么复 ...

  3. centos_7.1.1503_src_7

    http://vault.centos.org/7.1.1503/os/Source/SPackages/ tex-fonts-hebrew-0.1-21.el7.src.rpm 05-Jul-201 ...

  4. Oracle 获取ddl语句

    --得到所有表空间的ddl语句 SELECT DBMS_METADATA.GET_DDL('TABLESPACE', TS.tablespace_name)FROM DBA_TABLESPACES T ...

  5. Python爬虫音频数据

    一:前言 本次爬取的是喜马拉雅的热门栏目下全部电台的每个频道的信息和频道中的每个音频数据的各种信息,然后把爬取的数据保存到mongodb以备后续使用.这次数据量在70万左右.音频数据包括音频下载地址, ...

  6. Can't load standard profile: GRAY.pf

    报错: java.lang.IllegalArgumentException: Can't load standard profile: GRAY.pf at java.awt.color.ICC_P ...

  7. Rotate Image——数学相关

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  8. 【转】Debug 运行正常,Release版本不能正常运行

    http://blog.csdn.net/ruifangcui7758/archive/2010/10/18/5948611.aspx引言 如果在您的开发过程中遇到了常见的错误,或许您的Release ...

  9. python类的使用与多文件组织

    多文件的组织 跨目录级导入模块 from ..xxfile import xxmodule #从上级目录中的xxfile中导入xxmodule import xxsub_dir.xxfile #从xx ...

  10. Mysql用户管理(远程连接、授权)

    1.新建用户 登录MYSQL: @>mysql -u root -p @>密码 创建用户: mysql> insert into mysql.user(Host,User,Passw ...