//获取当前时间加两天(闰年 天数)
function getTimer() {
var str = ""
var timer = new Date()
var year = timer.getFullYear()
var month = timer.getMonth() + 1 < 10 ? "0" + (timer.getMonth() + 1) : timer.getMonth() + 1
var day = (timer.getDate() + 2) < 10 ? "0" + (timer.getDate() + 2) : timer.getDate() + 2
var hour = timer.getHours() < 10 ? "0" + timer.getHours() : timer.getHours()
var minute = timer.getMinutes() < 10 ? "0" + timer.getMinutes() : timer.getMinutes()
var second = timer.getSeconds() < 10 ? "0" + timer.getSeconds() : timer.getSeconds()
var erMonth = month
//二月
if (erMonth == "02") {
if (year % 4 == 0) {
if (day == 30) {
month = "03"
day = "01"
}
} else {
if (day == 29) {
month = "03"
day = "01"
}
}
}
//其他30天
if (erMonth == "04" || erMonth == "06" || erMonth == "09" || erMonth == "11") {
if (day == "31") {
month = timer.getMonth() + 2 < 10 ? "0" + (timer.getMonth() + 1) : timer.getMonth() + 2
day = "01"
}
} else {
if (day == "32") {
month = timer.getMonth() + 2 < 10 ? "0" + (timer.getMonth() + 1) : timer.getMonth() + 2
day = "01"
}
}
//年
if (erMonth == "13") {
year = year + 1
}
return str = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second
}
console.log(getTimer());
这段博客提供了一个JavaScript函数getTimer,用于获取当前时间并加上两天。函数考虑了闰年和不同月份天数的情况,对日期进行了相应处理,最后返回格式化后的日期时间字符串,并通过console.log输出结果。
2323

被折叠的 条评论
为什么被折叠?



