• JS常用时间操作moment.js参考文档


    Moment.js是一个轻量级的JavaScript时间库,它方便了日常开发中对时间的操作,提高了开发效率。日常开发中,通常会对时间进行下面这几个操作:比如获取时间,设置时间,格式化时间,比较时间等等。下面就是我对moment.js使用过程中的整理,方便以后查阅。

    一、引入moment.js

    1.Node.js方式引入

    (1)安装

    npm install moment  或者  yarn add moment
    

    (2)引入

    1. // require 方式
    2. var moment = require('moment');
    3. // import 方式
    4. import moment from 'moment';
    2.浏览器方式引入
    
    

    二、moment时区和转换

    1.时区的设置
    1. // require 方式
    2. require('moment/locale/zh-cn')
    3. moment.locale('zh-cn');
    4. // import 方式
    5. import 'moment/locale/zh-cn'
    6. moment.locale('zh-cn');
    7. //使用utc时区,后面可继续添加相关获取时间或格式化操作
    8. moment.utc()

     2.UTC和北京时间的互转

    2.1 目标是UTC时间,则用moment(utcTime).utc()方法,举例:

    moment(utcTime).utc().format('YYYY-MM-DD HH:mm:ss')
    

    2.2 目标是北京时间,则用moment(utcTime).utcOffset(8)方法,举例:

    moment(utcTime).utcOffset(8).format('YYYY/MM/DD HH:mm:ss')
    

    三、使用

    1.获取时间

    (1)获取当前时间

    moment()
    

    (2)获取今天0时0分0秒

    moment().startOf('day')
    

    (3)获取本周第一天(周日)0时0分0秒

    moment().startOf('week')
    

    (4)获取本周周一0时0分0秒

    moment().startOf('isoWeek')
    

    (5)获取当前月第一天0时0分0秒

    moment().startOf('month')
    

    (6)获取今天23时59分59秒

    moment().endOf('day')
    

    (7)获取本周最后一天(周六)23时59分59秒

    moment().endOf('week')
    

    (8)获取本周周日23时59分59秒

    moment().endOf('isoWeek')
    

    (9)获取当前月最后一天23时59分59秒

    moment().endOf('month')
    

    (10)获取当前月的总天数

    moment().daysInMonth() 
    

    (11)获取时间戳(以秒为单位)

    1. moment().format('X') // 返回值为字符串类型
    2. moment().unix() // 返回值为数值型

    (12)获取时间戳(以毫秒为单位)

    1. moment().format('x') // 返回值为字符串类型
    2. moment().valueOf() // 返回值为数值型

    (13)获取年份

    1. moment().year()
    2. moment().get('year')

    (14)获取月份

    1. moment().month() // (0~11, 0: January, 11: December)
    2. moment().get('month')

    (15)获取一个月中的某一天

    1. moment().date()
    2. moment().get('date')

    (16)获取一个星期中的某一天

    1. moment().day() // (0~6, 0: Sunday, 6: Saturday)
    2. moment().weekday() // (0~6, 0: Sunday, 6: Saturday)
    3. moment().isoWeekday() // (1~7, 1: Monday, 7: Sunday)
    4. moment().get('day')
    5. mment().get('weekday')
    6. moment().get('isoWeekday')

    (17)获取小时

    1. moment().hours()
    2. moment().get('hours')

    (18)获取分钟

    1. moment().minutes()
    2. moment().get('minutes')

    (19)获取秒数

    1. moment().toArray() // [years, months, date, hours, minutes, seconds, milliseconds]
    2. moment().toObject() // {years: xxxx, months: x, date: xx ...}
    2.设置时间

    (1)设置年份

    1. moment().year(2019)
    2. moment().set('year', 2019)
    3. moment().set({year: 2019})

    (2)设置月份

    1. moment().month(11) // (0~11, 0: January, 11: December)
    2. moment().set('month', 11)

    (3)设置某个月中的某一天

    1. moment().date(15)
    2. moment().set('date', 15)

    (4)设置某个星期中的某一天

    1. moment().weekday(0) // 设置日期为本周第一天(周日)
    2. moment().isoWeekday(1) // 设置日期为本周周一
    3. moment().set('weekday', 0)
    4. moment().set('isoWeekday', 1)

    (5)设置小时

    1. moment().hours(12)
    2. moment().set('hours', 12)

    (6)设置分钟

    1. moment().minutes(30)
    2. moment().set('minutes', 30)

    (7)设置秒数

    1. moment().seconds(30)
    2. moment().set('seconds', 30)

    (8)年份+1

    1. moment().add(1, 'years')
    2. moment().add({years: 1})

    (9)月份+1

    moment().add(1, 'months')
    

    (10)日期+1

    moment().add(1, 'days')
    

    (11)星期+1

    moment().add(1, 'weeks')
    

    (12)小时+1

    moment().add(1, 'hours')
    

    (13)分钟+1

    moment().add(1, 'minutes')
    

    (14)秒数+1

    moment().add(1, 'seconds')
    

    (15)年份-1

    1. moment().subtract(1, 'years')
    2. moment().subtract({years: 1})

    (16)月份-1

    moment().subtract(1, 'months')
    

    (17)日期-1

    moment().subtract(1, 'days')
    

    (18)星期-1

    moment().subtract(1, 'weeks')
    

    (19)小时-1

    moment().subtract(1, 'hours')
    

    (20)分钟-1

    moment().subtract(1, 'minutes')
    

    (21)秒数-1

    moment().subtract(1, 'seconds')
    
    3.格式化时间
    格式代码说明返回值例子
    M数字表示的月份,没有前导零1到12
    MM数字表示的月份,有前导零01到12
    MMM三个字母缩写表示的月份Jan到Dec
    MMMM月份,完整的文本格式January到December
    Q季度1到4
    D月份中的第几天,没有前导零1到31
    DD月份中的第几天,有前导零01到31
    d星期中的第几天,数字表示0到6,0表示周日,6表示周六
    ddd三个字母表示星期中的第几天Sun到Sat
    dddd星期几,完整的星期文本从Sunday到Saturday
    w年份中的第几周如42:表示第42周
    YYYY四位数字完整表示的年份如:2014 或 2000
    YY两位数字表示的年份如:14 或 98
    A大写的AM PMAM PM
    a小写的am pmam pm
    HH小时,24小时制,有前导零00到23
    H小时,24小时制,无前导零0到23
    hh小时,12小时制,有前导零00到12
    h小时,12小时制,无前导零0到12
    m没有前导零的分钟数0到59
    mm有前导零的分钟数00到59
    s没有前导零的秒数1到59
    ss有前导零的描述01到59
    XUnix时间戳1411572969

    (1)格式化年月日: ‘xxxx年xx月xx日’

    moment().format('YYYY年MM月DD日')
    

     (2)格式化年月日: ‘xxxx-xx-xx’

    moment().format('YYYY-MM-DD')
    

    (3)格式化时分秒(24小时制): ‘xx时xx分xx秒’

    moment().format('HH时mm分ss秒')
    

    (4)格式化时分秒(12小时制):‘xx:xx:xx am/pm’

    moment().format('hh:mm:ss a')
    

    (5)格式化时间戳(以毫秒为单位)

    moment().format('x') // 返回值为字符串类型
    
    4.比较时间

    (1)获取两个日期之间的时间差

    1. let start_date = moment().subtract(1, 'weeks')
    2. let end_date = moment()
    3. end_date.diff(start_date) // 返回毫秒数
    4. end_date.diff(start_date, 'months') // 0
    5. end_date.diff(start_date, 'weeks') // 1
    6. end_date.diff(start_date, 'days') // 7
    7. start_date.diff(end_date, 'days') // -7
    5.转化为JavaScript原生Date对象
    1. moment().toDate()
    2. new Date(moment())
     6.

    日期格式化
    1. moment().format('MMMM Do YYYY, h:mm:ss a'); // 五月 24日 2019, 7:47:43 晚上
    2. moment().format('dddd'); // 星期五
    3. moment().format("MMM Do YY"); // 5月 24日 19
    4. moment().format('YYYY [escaped] YYYY'); // 2019 escaped 2019
    5. moment().format(); // 2019-05-24T19:47:43+08:00

    7.相对时间输出实例
    1. moment("20111031", "YYYYMMDD").fromNow(); // 12 年前
    2. moment("20120620", "YYYYMMDD").fromNow(); // 12 年前
    3. moment().startOf('day').fromNow(); // 20 小时前
    4. moment().endOf('day').fromNow(); // 4 小时内
    5. moment().startOf('hour').fromNow(); // 1 小时前
    8.日历时间输出实例
    1. moment().subtract(10, 'days').calendar(); // 2019年5月14日
    2. moment().subtract(6, 'days').calendar(); // 上周六晚上7点49
    3. moment().subtract(3, 'days').calendar(); // 本周二晚上7点49
    4. moment().subtract(1, 'days').calendar(); // 昨天晚上7点49分
    5. moment().calendar(); // 今天晚上7点49分
    6. moment().add(1, 'days').calendar(); // 明天晚上7点49分
    7. moment().add(3, 'days').calendar(); // 下周一晚上7点49
    8. moment().add(10, 'days').calendar(); // 2019年6月3日
    9.多语言支持输出实例
    1. moment().format('L'); // 2019-05-24
    2. moment().format('l'); // 2019-05-24
    3. moment().format('LL'); // 2019年5月24日
    4. moment().format('ll'); // 2019年5月24日
    5. moment().format('LLL'); // 2019年5月24日晚上7点50分
    6. moment().format('lll'); // 2019年5月24日晚上7点50分
    7. moment().format('LLLL'); // 2019年5月24日星期五晚上7点50分
    8. moment().format('llll'); // 2019年5月24日星期五晚上7点50分
    10.其它实用技巧
    1. moment().format("YYYY-MM-DD") //格式化显示当前时间
    2. `${moment().subtract("month", +1).format("YYYY-MM")}-01` //上一个月的1号
    3. `${moment().add("month", -1).format("YYYY-MM")}-01` //还是上一个月1号
    4. let M = `${moment().format("YYYY-MM")}-01` //本月一号
    5. moment(M).add("days", -1).format("YYYY-MM-DD") //上一个月月底
    6. moment().startOf("year").format("YYYY-MM-DD") //本年的的开始日期,("2019-01-01")
    7. moment().endOf("year").format("YYYY-MM-DD") //本年的的结束日期,("2019-12-31")
    8. //moment 转成时间戳
    9. moment().valueOf()
    10. //时间戳 转 moment
    11. moment(string).format()

  • 相关阅读:
    数据库更新没有反映在前端页面
    nginx的location的优先级和匹配方式和重定向
    题目 1051: [编程入门]结构体之成绩统计2
    Python中list的操作4-3
    哈佛架构 VS 冯·诺依曼架构
    ‘Tensor‘ object has no attribute ‘np‘
    材料数据库设计问题
    SpringCloud(十)——ElasticSearch简单了解(一)初识ElasticSearch和RestClient
    通过包管理器方式安装 Node.js
    Codeforces Round #802 (Div. 2)
  • 原文地址:https://blog.csdn.net/2201_75705263/article/details/133977649