前端日期小工具——持续更新中

Date对象的常用方法

方法描述
getFullYaer()从 Date 对象以四位数字返回年份
getMonth()从 Date 对象返回月份 (0 ~ 11)
getDate()从 Date 对象返回一个月中的某一天 (1 ~ 31)
getHours()返回 Date 对象的小时 (0 ~ 23)
getMinutes()返回 Date 对象的分钟 (0 ~ 59)
getSeconds()返回 Date 对象的秒数 (0 ~ 59)
getMilliSeconds()返回 Date 对象的毫秒(0 ~ 999)
getTime()返回 1970 年 1 月 1 日到指定日期的毫秒数
getDay()从 Date 对象返回一周中的某一天 (0 ~ 6),周日为第一天
toLocalString()根据本地时间格式,把 Date 对象转换为字符串
toLocalDateString()根据本地时间格式,把 Date 对象的时间部分转换为字符串
toLocalTimeString()根据本地时间格式,把 Date 对象的日期部分转换为字符串

计算日期之间间隔几周(支持跨年)

function weekCalculator(beginDate, endDate) {
	//跳过第一周,直接获取第二周周一时间
	const beginTime = beginDate.getTime() + (7 - getLocalDay(beginDate)) * (1000 * 60 * 60 * 24);
	//计算到结束时间的天数
    let day = (endDate.getTime() - beginTime) / (1000 * 60 * 60 * 24) + 1;
    //计算周数,向上取整,加上之前跳过的一周
    return Math.ceil(day / 7) + 1;
}

//以周一为第一天,获取给定日期是本周第几天,若想以周天为第一天直接使用getDay()代替
function getLocalDay(date) {
	let day = date.getDay();
    return day === 0 ? 6 : day - 1;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值