moment 是一个 javascript 日期处理类库。
使用:
//安装 moment npm install moment -- save
引用
//在main.js中全局引入 import moment from "moment"
设定moment区域为中国
//import 方式
import 'moment/locale/zh-cn'
moment.locale('zh-cn'); 挂载全局变量
vue.prototype.$moment = moment;
代码示例:
- 当前日期向前或者向后推的日期格式化
moment().subtract(13, "days").calendar(); // 当前时间往前推13天的日历时间: 2024/07/16 moment().subtract(3, "days").calendar(); // 当前时间往前推3天: 本周三16:46 moment().subtract(1, "days").calendar(); // 当前时间往前推1天: 昨天16:47 moment().calendar(); // 今天16:48 moment().add(1, "days").calendar(); // 当前时间往后推1天: 明天16:49 moment().add(3, "days").calendar(); // 当前时间往后推3天: 下周二16:50 moment().add(10, "days").calendar(); // 当前时间往后推10天: 2024/07/06
常用函数:
//获取当前时间
moment();//sun jun 04 2023 15:12:11 gmt+0800
//获取今天0时0分0秒
moment().startof('day'); /sun jun 04 2024 00:00:00 gmt+0800
//获取本周第一天(周日)0时0分0秒
moment().startof("week"); //mon may 29 2024 00:00:00 gmt+0800
//获取本周周一0时0分0秒
moment().startof("isoweek"); //mon may 29 2024 00:00:00 gmt+0800
//获取当前月第一天0时0分0秒
moment().startof("month"); //thu jun 01 2024 00:00:00 gmt+0800
//获取今天23时59分59秒
moment().endof("day"); //sun jun 04 2024 23:59:59 gmt+0800
//获取本周最后一天(周六)23时59分59秒
moment().endof("week"); //sun jun 04 2024 23:59:59 gmt+0800
//获取本周周日23时59分59秒
moment().endof("isoweek"); //sun jun 04 2024 23:59:59 gmt+0800
//获取当前月最后一天23时59分59秒
moment().endof("month"); //fri jun 30 2024 23:59:59 gmt+0800
//获取当前月的总天数
moment().daysinmonth(); //30
//获取时间戳(以秒为单位)
moment().unix(); //1685863710
moment().format('x'); //1685863669
//获取时间戳(以毫秒为单位)
moment().valueof(); //返回值为数值型:1685863954482
moment().format('x'); // 返回值为字符串类型:1685863897121
//获取年份
moment().year(); //2024
moment().get("year"); //2024
//获取月份
moment().month(); //5
moment().get("month"); //5
//获取一个月中的某一天
moment().date(); //4
moment().get("date"); //4
//获取一个星期中的某一天
moment().day(); //4
moment().weekday(); //6
moment().isoweekday(); //7
moment().get("day"); //0
moment().get("weekday"); //6
moment().get("isoweekday"); //7
//获取小时
moment().hours(); //15
moment().get("hours"); //15
//获取分钟
moment().minutes(); //46
moment().get("minutes"); //46
//获取秒数
moment().seconds(); //24
moment().get("seconds"); //41
//获取当前的年月日时分秒
moment().toarray(); //[ 2024, 5, 4, 15, 48, 40, 288 ]
moment().toobject(); //{ "years": 2024, "months": 5, "date": 4, "hours": 15, "minutes": 49, "seconds": 9, "milliseconds": 386 }
//当前时间
moment()
//中国标准时间
moment().todate()
//格式化当前时间
moment().format('yyyy-mm-dd')
//12小时制:
moment().format('yyyy-mm-dd hh:mm:ss')
//24小时制://kk有问题 改为hh
moment().format('yyyy-mm-dd kk:mm:ss')
moment().format('yyyy-mm-dd hh:mm:ss')
//本月月初
moment().startof('month')
//本月月末
moment().endof('month')
//n天/月/小时后
moment().add(5, 'month')
//5个月之后的日期,参数为负则表示之前,参数'month'还可以为'day'、'hour'
//当前时间的前10天时间
moment().subtract(10, "days").format("yyyy-mm-dd");
//当前时间的前1年时间
moment().subtract(1, "years").format("yyyy-mm-dd");
//当前时间的前3个月时间
moment().subtract(3, "months").format("yyyy-mm-dd");
//当前时间的前一个星期时间
moment().subtract(1, "weeks").format("yyyy-mm-dd");
附:js获取当前日期是第几周、每年有多少周、周的开始时间和结束时间
//引入moment.js
import moment from 'moment';
//获取一年中有多少周、每一周开始和结束时间
const mapweeksofyear = ({ year} = {}) => {
const nowyear = year ? year : moment().year();
const timestamp = (new date()).valueof()
// 设置正在处理的年份
let handleyear = moment(new date(string(nowyear)));
// 这一年有多少个周
const totalweeks = handleyear.endof('year').isoweek();
const arryweek = [];
let currentweek = null; //当前日期是第几周
for(let i = 1;i <= totalweeks;i++){
let startof = handleyear.week(i).startof('week').format('mm-dd');
let endof = handleyear.week(i).endof('week').format('mm-dd');
let ednyear = handleyear.week(i).endof('week').format('yyyy');
let startvalue= handleyear.week(i).startof('week').valueof();
let endvalue= handleyear.week(i).endof('week').valueof();
if(startvalue<=timestamp&&endvalue>=timestamp){
currentweek = i
}
arryweek.push({
value: i,
name: `第${i}周 ${ednyear>nowyear?nowyear + "-" +startof :startof} 至 ${ednyear>nowyear?ednyear + "-" +endof:endof}`,
starttime: handleyear.week(i).startof('week').format('yyyy-mm-dd'),// 这周的开始时间
endtime: handleyear.week(i).endof('week').format('yyyy-mm-dd'), // 这周的结束时间
})
}
return {arryweek,currentweek}
}到此这篇关于javascript日期处理类库moment()获取时间的文章就介绍到这了,更多相关moment()获取时间内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论