moment格式化时间操作(时间戳与字符串相互转化)

const value = {
              ...values,
              birthday: values['birthday'].format('YYYY-MM-DD'),
            };//时间戳转字符串

 moment(record.birthday, 'YYYY-MM-DD')//字符串转时间戳

 

# moment.js

## 在vue里面全局使用

main.js

// 引入moment

import moment from 'moment'

import 'moment/locale/zh-cn'

// 使用中文时间

moment.locale('zh-cn')

Vue.prototype.$moment = moment

vue其他地方调用的方法

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

## 获取当前时间

//返回当前时间moment()时间对象

moment();

moment(new Date());

//返回当前时间毫秒数

moment().valueOf() === Date.now() === new Date().getTime() === new Date().valueOf()

返回结果:2018-08-23T08:35:36.653 / 1534986255282

## 格式化当前时间

let today = moment().format('YYYY-MM-DD');

moment(new Date()).format('YYYY/MM/DD HH:mm:ss');

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

moment().format('X')

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

moment().format('x') === moment().valueOf();

## 原生Date对象同moment对象相互转换

//转换为date对象

moment().toDate() === new Date(moment())

//转换为moment对象

moment(new Date())

## 当前时间-取值操作

//获取当前时间年数

moment().year() === new Date().getFullYear() === moment().get('y')

//获取当前时间月数

moment().month()+1 === new Date().getMonth()+1

//获取当前时间秒数

moment().seconds() === moment().second() === new Date().getMonth()

//获取当前时间毫秒数

moment().millisecond() === moment().milliseconds()

## subtract()

//七天前的时间

let last7 = moment().subtract(6 ,'days').format('YYYY-MM-DD');

//本周第一天,周日

moment().day(0).format('YYYY-MM-DD');

//拓展-2018-07-5后一天

moment('2018-07-5').subtract(-1 ,'days').format('YYYY-MM-DD');

//获取上个月今天的日期

moment().subtract(1, 'months').format('YYYY-MM-DD');

返回结果:2018-08-17

## add()

//后一天时间

moment().add(1, 'd').format('YYYY-MM-DD')

//两小时之后

moment().add(2,'hours').format('YYYY-MM-DD HH:mm:ss');

## 本周五

moment().weekday(5).format('YYYY-MM-DD');

*## 上周五

moment().weekday(-3).format('YYYY-MM-DD');

## 本月第一天

//本月第一天

let thisMonthFirst = moment().startOf('month').format('YYYY-MM-DD');

//本年第一天

let thisYearFirst = moment().startOf('year').format('YYYY-MM-DD');

## 本月最后一天

//本月最后一天

moment().endOf('year').format('YYYY-MM-DD');

//本年最后一天

moment().endOf('month').format('YYYY-MM-DD');

## 上个月最后一天

moment('2018-10-06').date(0).format('YYYY-MM-DD');

## 是否之前

moment('2010-10-20').isBefore('2010-10-21'); // true

moment('2010-10-20').isBefore('2010-12-31', 'year'); // false

moment('2010-10-20').isBefore('2011-01-01', 'year'); // true

## 是否相同(判断年、月、日)

moment('2010-10-20').isSame('2010-10-20'); // true

moment('2010-10-20').isSame('2009-12-31', 'year'); // false

moment('2010-10-20').isSame('2010-01-01', 'year'); // true

moment('2010-10-20').isSame('2010-10-31', 'month'); // true

moment('2010-10-20').isSame('2011-01-01', 'year'); // false

## 是否之后(同是否之前)

moment('2010-10-20').isAfter('2010-01-01', 'year'); // false

moment('2010-10-20').isAfter('2009-12-31', 'year'); // true

## 是否之间

moment('2010-10-20').isBetween('2010-10-19', '2010-10-25'); // true

## 标准化单位

moment().get('y') // 'year'

moment().get('year') // 'year'

moment().get('years') // 'year'

moment().year() // 'year'

moment().years() // 'year'

moment().get('M')+1 // 'month'

moment().get('Month')+1 // 'month'

moment().get('Months')+1 // 'month'

moment().get('month')+1 // 'month'

moment().get('months')+1 // 'month'

moment().month()+1

moment().months()+1

moment().get('D') // 'Day'

moment().get('Day')

moment().get('Days')

moment().day()

moment().days()

moment().get('H'); // 'Hours'

moment().get('Hour');

moment().get('Hours');

moment().get('h'); // 'Hours'

moment().get('hour');

moment().get('hours');

moment().hour();

moment().hours();

moment().get('m'); //minute

moment().get('minute');

moment().get('minutes');

moment().get('Minute');

moment().get('Minutes');

moment().get('s'); //second

moment().get('second');

moment().get('seconds');

moment().get('S');

moment().get('Second');

moment().get('Seconds');

moment().second();

monent().seconds();

后记:在使用moment格式化时间的时候经常会出现比实际时间迟8小时的问题,使用utcOffset解决

monent(myDate).utcOffset(0).format('YYYY/MM/DD HH:mm:ss')

monent(myDate).utc(0).format('YYYY/MM/DD HH:mm:ss')

原文链接:https://blog.csdn.net/weixin_39793434/article/details/113587014

posted @ 2022-06-24 19:46  SimoonJia  阅读(15446)  评论(0编辑  收藏  举报