JavaScript 时间处理 加一天 减一天

1、首先 获取当前时间或者自定义一个时间

// 获取系统当前时间
var nowDate = new Date()
输出结果:Thu Dec 09 2021 15:42:07 GMT+0800 (中国标准时间)
// 自定义时间
var diyDate = new Date('2021-12-12')
输出结果:Sun Dec 12 2021 08:00:00 GMT+0800 (中国标准时间)

2、实例 日期加一天

// 自定义时间
var diyDate = new Date('2021-12-12')
//加1天
date = new Date(diyDate.setDate(diyDate.getDate() + 1));
// 拼接日期 yyyy-MM-dd 注意月份要 + 1
date = date.getFullYear() + "-" + ((date.getMonth() + 1) > 9 ? date.getMonth() + 1 : "0" + (date.getMonth() + 1)) + "-" + (date.getDate() > 9 ? date.getDate() : "0" + date.getDate());
//结果
输出日期:2021-12-13

3、日期获取方法

getFullYear()		获取四位的年(yyyy)
getMonth()			获取月(0-11getDay()			以数值获取周名(0-6getDate()			以数值返回天(1-31getHours()			获取小时(0-23getMinutes()		获取分(0-59getSeconds()		获取秒(0-59getMilliseconds()	获取毫秒(0-999getTime()			获取时间(从 197011 日至今)

4、日期设置方法

setFullYear()		设置年(可选月和日)
setMonth()			设置月(0-11setDate()			以数值(1-31)设置日
setHours()			设置小时(0-23setMinutes()		设置分(0-59setSeconds()		设置秒(0-59setMilliseconds()	设置毫秒(0-999setTime()			设置时间(从 197011 日至今的毫秒数)

猜你喜欢

转载自blog.csdn.net/Maxueyingying/article/details/130968692
今日推荐