获取当前日期的前三天后三天 前几个月后几个月

获取各种日期

//获取最近7天日期
console.log(getDay(0));//当天日期

//获取最近3天日期
console.log(getDay(0));//当天日期
console.log(getDay(-3));//3天前日期 
console.log(getDay(-7));//7天前日期 
console.log(getDay(+3)) // 3天后日期
console.log(getDay(+7));//7天后日期
function getDay(day){
    var today = new Date();
    var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;
    today.setTime(targetday_milliseconds); //注意,这行是关键代码
    var tYear = today.getFullYear();
    var tMonth = today.getMonth();
    var tDate = today.getDate();
    tMonth = doHandleMonth(tMonth + 1);
    tDate = doHandleMonth(tDate);
    return tYear+"-"+tMonth+"-"+tDate;
}
function doHandleMonth(month){
    var m = month;
    if(month.toString().length == 1){
     m = "0" + month;
    }
    return m;
}
console.log(calendar(0))
function calendar(roww) {
    var today = new Date();
    var tMonth = today.getMonth()+1+roww;
    return tMonth;
}

function getLastMonth(num){//获取上个月日期
        var date = new Date; 
        var year = date.getFullYear();
        var month = date.getMonth()+1 -num;
        if(month == 2){
             year = year -1;
             month = 12; 
        }else if(month == 1){
             year = year -1;
             month = 11;
       }
        return year+"-"+month;
    }
console.log(getLastMonth(2)) // shang
console.log(getLastMonth(1))


var now = new Date(); //当前日期
var nowDay = now.getDate(); //当前日
var nowMonth = now.getMonth(); //当前月
var nowYear = now.getYear(); //当前年
console.log(getMonthDays(nowMonth)) //当前月天数
console.log(getMonthDays(nowMonth-1)) //前一月天数
console.log(getMonthDays(nowMonth-2))  //上上月天数
console.log(getLastMonth(2)+'-'+1) // shang
console.log(getLastMonth(1)+'-'+1)
console.log(getLastMonth(2)+'-'+getMonthDays(nowMonth-2)) // shang
console.log(getLastMonth(1)+'-'+getMonthDays(nowMonth-1))
console.log(getDay(-3));//3天前日期
console.log(getDay(+3)) // 3天后日期

//console.log(nowMonth);
nowYear += (nowYear < 2000) ? 1900 : 0; //
var lastMonthDate = new Date(); //上月日期
// console.log('lastMonthDate'+lastMonthDate)
lastMonthDate.setDate(1);   //变成这个月一号
lastMonthDate.setMonth(lastMonthDate.getMonth()-1);
var lastYear = lastMonthDate.getYear();
lastYear += (lastYear < 2000) ? 1900 : 0; //
// console.log('lastYear'+lastYear);
var lastMonth = lastMonthDate.getMonth();
//  console.log('lastmonth'+lastMonth);

function getToday() {
    var now = new Date();
    var day = ("0" + now.getDate()).slice(-2);
    //  console.log(day);
    var month = ("0" + (now.getMonth() + 1)).slice(-2);
    var today = now.getFullYear() + "-" + (month) + "-" + (day);
    return today;
}
//获得本月的开始日期
function getMonthStartDate(){
    var monthStartDate = new Date(nowYear, nowMonth, 1);
    return formatDate(monthStartDate);
}
//格式化日期:yyyy-MM-dd
function formatDate(date) {
    var myyear = date.getFullYear();
    var mymonth = date.getMonth()+1;
    var myweekday = date.getDate();

    if(mymonth < 10){
        mymonth = "0" + mymonth;
    }
    if(myweekday < 10){
        myweekday = "0" + myweekday;
    }
    return (myyear+"-"+mymonth + "-" + myweekday);
}
function nianfen (){
    var now = new Date();
    return now.getFullYear();
}
console.log(nianfen ())
//获得上月开始时间
function getLastMonthStartDate(){
    var lastMonthStartDate = new Date(lastYear, lastMonth, 1);
    return formatDate(lastMonthStartDate);
}
console.log(getLastMonthStartDate(),777777777)
//获得上月结束时间
function getLastMonthEndDate(){
    var lastMonthEndDate = new Date(lastYear, lastMonth, getMonthDays(lastMonth));
    return formatDate(lastMonthEndDate);
}
console.log(getLastMonthEndDate(),66666666666)
//获得某月的天数 可以
function getMonthDays(myMonth){
    var monthStartDate = new Date(nowYear, myMonth, 1);
    var monthEndDate = new Date(nowYear, myMonth + 1, 1);
    //   console.log('天数'+monthStartDate+'==='+ monthEndDate)
    var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24);
    //  console.log('天数,数量:'+days);
    return days;
}


发布了17 篇原创文章 · 获赞 11 · 访问量 559

猜你喜欢

转载自blog.csdn.net/weixin_45563734/article/details/103732934