Chinaunix首页 | 论坛 | 博客
  • 博客访问: 85404
  • 博文数量: 25
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 255
  • 用 户 组: 普通用户
  • 注册时间: 2015-04-09 11:03
个人简介

★*^_^*★

文章分类

全部博文(25)

文章存档

2021年(2)

2018年(15)

2017年(1)

2016年(4)

2015年(3)

我的朋友

分类: JavaScript

2018-07-03 14:48:23

var date = getDate (1); //前一天,如果传3就是前三天
$('#test1').val(date.passday);

function getDate (num) {
    var nowDate = new Date();
    var nowYear = nowDate.getFullYear();
    var nowMonth = nowDate.getMonth();
    var nowDay = nowDate.getDate();
    var nowHours = nowDate.getHours();
    var nowMinutes = nowDate.getMinutes();
    var nowSeconds = nowDate.getSeconds();
    var passDate = new Date(nowYear, nowMonth, nowDay - num + 1, 0, 0, 0); //前几天
    var passThreeDate = new Date(nowYear, nowMonth-3, nowDay + 1, 0, 0, 0); //前3个月
    var passYear = passDate.getFullYear();
    var passMonth = passDate.getMonth();
    var passThreeMonth = passThreeDate.getMonth()+1; //前3个月
    var passDay = passDate.getDate();
    var passHours = passDate.getHours();
    var passMinutes = passDate.getMinutes();
    var passSeconds = passDate.getSeconds();
    var beforeMonth = nowMonth;  //前一个月
    var nowMonthNew = nowMonth+1; //当月
    var passMonthNew = passMonth+1;
    if(nowMonth < 10){
        nowMonthNew = "0" + ''+ nowMonthNew;
        beforeMonth = "0" + ''+ nowMonth;
    }
    if(nowDay < 10) nowDay = "0" + '' + nowDay;
    if(nowHours < 10) nowHours = "0" + '' + nowHours;
    if(nowMinutes < 10) nowMinutes = "0" + '' + nowMinutes;
    if(nowSeconds < 10) nowSeconds = "0" + '' + nowSeconds;
    if(passDay < 10) passDay = "0" + '' + passDay;
    if(passMonth < 10) passMonthNew = "0" + ''+ passMonthNew;
    if(passThreeMonth < 10) passThreeMonth = "0" + ''+ passThreeMonth;
    if(passHours < 10) passHours = "0" + '' + passHours;
    if(passMinutes < 10) passMinutes = "0" + '' + passMinutes;
    if(passSeconds < 10) passSeconds = "0" + '' + passSeconds;
    return {
        nowday: nowYear + '-' + nowMonthNew + '-' + nowDay + ' ' + nowHours + ':' + nowMinutes + ':' + nowSeconds,
        passday: passYear + '-' + passMonthNew + '-' + passDay + ' ' + passHours + ':' + passMinutes + ':' + passSeconds,
        nowDayDate:nowYear + '-' + nowMonthNew + '-' + nowDay,
        threeDayBefore: nowYear + '-' + nowMonthNew + '-' + passDay,
        nowMonthDate:nowYear + '' + beforeMonth,
        threeMonthBefore: nowYear + '' + passThreeMonth
    }
}
阅读(677) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~