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. Ubuntu 14.04 ThinkPad E431无线网卡驱动安装

    Ubuntu 14.04下安装无线网卡驱动. sudo apt-get install linux-headers-generic build-essential dkms  sudo apt-get ...

  2. 自动化测试===uiautomator2类似appium

    项目地址:https://github.com/openatx/uiautomator2 http://mp.weixin.qq.com/s/YKxwW-pL603Fll3QIWapVw https: ...

  3. 2015多校第6场 HDU 5355 Cake 贪心,暴力DFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5355 题意:给你n个尺寸大小分别为1,2,3,…,n的蛋糕,要求你分成m份,要求每份中所有蛋糕的大小之 ...

  4. FineReport——登录不到决策系统

    在不断的测试过程中,可能会造成缓存数据的累积,所以在登录过程中可能会出现登录不到决策系统,而是跳转到某一模板页面 解决方法就是清理缓存或者换一个浏览器测试.

  5. 微软推出ASP.NET Core 2.0,并支持更新Visual Studio 2017

    微软推出ASP.NET Core 2.0的一般可用性,并发布.NET Core 2.0.该公司还推出了其旗舰集成开发环境(IDE)的更新:Visual Studio 2017版本15.3和Visual ...

  6. html,图片上传预览,input file获取文件等相关操作

    input file常用方法: var obj=document.getElementById("upimage"); var file=obj.files[0];//获取文件数据 ...

  7. 【python】配置文件

    来源:http://developer.51cto.com/art/201003/189885.htm python 读写配置文件在实际应用中具有十分强大的功能,在实际的操作中也有相当简捷的操作方案, ...

  8. tomcat - gc日志输出

    原创 2017年01月04日 14:32:37 2090 tomcat/bin catalina.sh JAVA_OPTS='-server -Xms4g -Xmx4g -Xss256k -XX:Pe ...

  9. python中的is, ==与对象的相等判断

    在java中,对于两个对象啊a,b,若a==b表示,a和b不仅值相等,而且指向同一内存位置,若仅仅比较值相等,应该用equals.而在python中对应上述两者的是‘is’ 和‘==’. (1) py ...

  10. JS作用域和ASP(vbs)作用域比较

    一.js作用域,先上图: 以上代码执行的效果是,依次弹出 undefined, undefined, a, a,为什么是这样的结果啦?因为JS的作用域为链式作用域. 作用域链: 用VAR声明一个变量时 ...