美文网首页
js判断时间戳是否在本周、根据时间戳获取近七天以及近30天

js判断时间戳是否在本周、根据时间戳获取近七天以及近30天

作者: 向往天空的鸡 | 来源:发表于2018-06-15 15:51 被阅读0次

     判断时间戳是否在本周

    isWeek:function (time) {

        // 当前时间

        var timestamp = Date.parse(new Date());

        var serverDate = new Date(timestamp);

        //本周周日的的时间

        var sundayTiem = timestamp + ((7 - serverDate.getDay())* 24 * 60 * 60 * 1000)

        var SundayData = new Date(sundayTiem);

        //年

        var tomorrowY = SundayData.getFullYear();

        //月

        var tomorrowM = (SundayData.getMonth() + 1 < 10 ? '0' + (SundayData.getMonth() + 1) : SundayData.getMonth() + 1);

        //日

        var tomorrowD = SundayData.getDate() < 10 ? '0' + SundayData.getDate() : SundayData.getDate();

        console.log(tomorrowY+'-'+tomorrowM+'-'+tomorrowD);

        // 本周周一的时间

        var mondayTime = timestamp - ((serverDate.getDay()-1) * 24 * 60 * 60 * 1000)

        var mondayData = new Date(mondayTime);

        //年

        var mondayY = mondayData.getFullYear();

        //月

        var mondayM = (mondayData.getMonth() + 1 < 10 ? '0' + (mondayData.getMonth() + 1) : mondayData.getMonth() + 1);

        //日

        var mondayD = mondayData.getDate() < 10 ? '0' + mondayData.getDate() : mondayData.getDate();

        console.log(mondayY + '-' + mondayM + '-' + mondayD);

        // 当前时间

        var currentData = new Date(time);

        //年

        var currentY = currentData.getFullYear();

        //月

        var currentM = (currentData.getMonth() + 1 < 10 ? '0' + (currentData.getMonth() + 1) : currentData.getMonth() + 1);

        //日

        var currentD = currentData.getDate() < 10 ? '0' + currentData.getDate() : currentData.getDate();

        //时

        var currenH = currentData.getHours();

        //分

        var currenM = currentData.getMinutes();

        var str = "星期" + "日一二三四五六".charAt(currentData.getDay());

        var sundayDay = tomorrowY + tomorrowM + tomorrowD;

        var mondayDay = mondayY + mondayM + mondayD;

        var currentDay = currentY + currentM + currentD;

        console.log('本周日:'+sundayDay + ',本周一:' + mondayDay + ',当前时间:' + currentDay)

        parseInt(currentDay);

        parseInt(mondayDay);

        if (parseInt(currentDay) >= parseInt(mondayDay)) {

          if (parseInt(currentDay) <= parseInt(sundayDay)) {

              console.log('是本周')

              return {

                titleDate:str,

                titleTime: currenH + ':' + currenM

              }

            }else {

            console.log('不是本周')

            return{

              titleDate: ' ' + currentM + '/' + currentD+' ',

              titleTime: currenH + ':' + currenM

            } 

            }

        }else {

          console.log('不是本周')

          return {

            titleDate: ' ' + currentM + '/' + currentD + ' ',

            titleTime: currenH + ':' + currenM

          } 

        }

      }

    根据时间戳获取 近7天以及近30天

    // 获取当天往后的七天或者一个月

    function timeForMat(count) {

      let time1 = new Date()

      time1.setTime(time1.getTime())

      let Y1 = time1.getFullYear()

      let M1 = ((time1.getMonth() + 1) > 10 ? (time1.getMonth() + 1) : '0' + (time1.getMonth() + 1))

      let D1 = (time1.getDate() > 10 ? time1.getDate() : '0' + time1.getDate())

      let timer1 = Y1 + M1 + + D1 // 当前时间

      let time2 = new Date()

      time2.setTime(time2.getTime() + (24 * 60 * 60 * 1000 * count))

      let Y2 = time2.getFullYear()

      let M2 = ((time2.getMonth() + 1) > 9 ? (time2.getMonth() + 1) : '0' + (time2.getMonth() + 1))

      let D2 = (time2.getDate() > 9 ? time2.getDate() : '0' + time2.getDate())

      let timer2 = Y2 + M2 + D2 // 之后的七天或者一个月

      return {

        t1: timer1,

        t2: timer2

      }

    }

    相关文章

      网友评论

          本文标题:js判断时间戳是否在本周、根据时间戳获取近七天以及近30天

          本文链接:https://www.haomeiwen.com/subject/eyjmeftx.html