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

 判断时间戳是否在本周

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

  }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 基础类型有哪些?复杂类型有哪些?有什么特征?5种简单数据类型(基本数据类型):Undefined、Null、Boo...
    coolheadedY阅读 328评论 0 0
  • Js获取当前日期时间及其它操作 var myDate = new Date(); myDate.getYear()...
    郝赫阅读 1,124评论 0 3
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 2,049评论 0 2
  • 23岁了、年龄的增长好像并没有带来什么变化。今天给自己花了很多钱、以为会得到以往一样的满足感、可是并没有。买买买已...
    阿阿无阅读 163评论 0 0
  • 36W1D。27D。 还在下着雨。 今天是爸爸2016年的最后一个夜班,妈妈今天也坚持了散步,涂色。今天感觉你开始...
    林培阅读 192评论 0 0