几个自己写的时间处理函数

17 篇文章 0 订阅
7 篇文章 0 订阅
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>

</body>
<script>
    //获取几天后日期
    function dateCount(arg, date) {
        var date1 = arg;
        var date2 = new Date(date1);
        date2.setDate(date2.getDate() + parseInt(date));
        var times = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate();
        var Year = 0;
        var Month = 0;
        var Day = 0;
        var CurrentDate = "";
        Year = date2.getFullYear();
        Month = date2.getMonth() + 1;
        Day = date2.getDate();
        CurrentDate += Year + "-";
        if (Month >= 10) {
            CurrentDate += Month + "-";
        } else {
            CurrentDate += "0" + Month + "-";
        }
        if (Day >= 10) {
            CurrentDate += Day;
        } else {
            CurrentDate += "0" + Day;
        }
        return CurrentDate;
    }



    Date.prototype.format = function () {
        var s = '';
        s += this.getFullYear() + '-'; // 获取年份。
        if ((this.getMonth() + 1) >= 10) { // 获取月份。
            s += (this.getMonth() + 1) + "-";
        } else {
            s += "0" + (this.getMonth() + 1) + "-";
        }
        if (this.getDate() >= 10) { // 获取日。
            s += this.getDate();
        } else {
            s += "0" + this.getDate();
        }
        return (s); // 返回日期。
    };




    //两个日期之间的所有日期
    function getAll(begin, end) {
        var ab = begin.split("-");
        var ae = end.split("-");
        var db = new Date();
        db.setUTCFullYear(ab[0], ab[1] - 1, ab[2]);
        var de = new Date();
        de.setUTCFullYear(ae[0], ae[1] - 1, ae[2]);
        var unixDb = db.getTime();
        var unixDe = de.getTime();
        var str = "";
        for (var k = unixDb + 24 * 60 * 60 * 1000; k < unixDe;) {
            str += (new Date(parseInt(k))).format() + ",";
            k = k + 24 * 60 * 60 * 1000;
        }
        return str;
    }




    //两个时间相差天数
    function datedifference(sDate1, sDate2) { //sDate1和sDate2是2006-12-18格式
        var aDate, oDate1, oDate2, iDays;
        aDate = sDate1.split("-");
        oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]); //转换为9-25-2017格式
        aDate = sDate2.split("-");
        oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);
        iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24);
        return iDays;
    };

       // 格式化日期
       function getTime (originVal,day) {
      const dt = new Date(originVal);
      const y = dt.getFullYear();
      const m = (dt.getMonth() + 1 + "").padStart(2, "0");
      const d = (dt.getDate() + day +"").padStart(2, "0");

      // const hh = (dt.getHours() + "").padStart(2, "0");
      // const mm = (dt.getMinutes() + "").padStart(2, "0");
      // const ss = (dt.getSeconds() + "").padStart(2, "0");

      // return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
      return `${y}-${m}-${d} `;

    };

    var a = new Date();
    var b = dateCount(a,7)
    var c = new Date(b).getDate()
    console.log(a);
    console.log(b);
    console.log('-----------------------------'+'b');
    console.log(c);
    console.log('-----------------------------'+'c');
    var e = getTime(a,0)
    console.log(e);
    
    console.log('-----------------------------'+'e');
    // var d =  getAll(a,b)
    console.log('-----------------------------'+'d');
    
   
    
    

    // module.exports = {
    //     DateCount: dateCount,
    //     GetAll: getAll,
    //     Datedifference: datedifference
    // }
</script>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值