Js获取下周周一0点时间戳?

文章介绍了两个JavaScript函数,getNowDateAndNowWeek和getNextMondayTime,用于获取当前周的周日、周一以及下周周一的日期和时间,通过计算时间戳并转换为指定格式输出。
摘要由CSDN通过智能技术生成

/**

  * 获取选择当前的第几周,当前的周一、周日

  * time 选择日期的时间戳

  */

function getNowDateAndNowWeek() {

    // 获取当前时间戳

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

    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();

    // 时

    var mondayH = mondayData.getHours() < 10 ? '0' + mondayData.getHours() : mondayData.getHours();

    // 分

    var mondayMinu = mondayData.getMinutes() < 10 ? '0' + mondayData.getMinutes() : mondayData.getMinutes();

    // 秒

    var mondayS = mondayData.getSeconds() < 10 ? '0' + mondayData.getSeconds() : mondayData.getSeconds();

    // 下周周一的时间

    var nextMondayTiem = timestamp + ((8 - serverDate.getDay()) * 24 * 60 * 60 * 1000)

    var nextMondayData = new Date(nextMondayTiem);

    // 年

    var nextMondY = nextMondayData.getFullYear();

    // 月

    var nextMondM = (nextMondayData.getMonth() + 1 < 10 ? '0' + (nextMondayData.getMonth() + 1) : nextMondayData.getMonth() + 1);

    // 日

    var nextMondD = nextMondayData.getDate() < 10 ? '0' + nextMondayData.getDate() : nextMondayData.getDate();

    console.log('下周一:  ' + nextMondY + '-' + nextMondM + '-' + nextMondD);

    // 输出值

    var config = {

      SunDay: tomorrowY + '-' + tomorrowM + '-' + tomorrowD,

      Monday: mondayY + '-' + mondayM + '-' + mondayD + ' ' + mondayH + ':' + mondayMinu + ':' + mondayS,

      nextMonday: nextMondY + '-' + nextMondM + '-' + nextMondD

    }

    return config;

}

function getNextMondayTime() {

  // 获取当天0点时间戳

  var timestamp = new Date(new Date().setHours(0,0,0,0)).getTime();

  var serverDate = new Date(timestamp);

  // 下周周一的时间

  var nextMondayTiem = timestamp + ((8 - serverDate.getDay()) * 24 * 60 * 60 * 1000)

  var nextMondayData = new Date(nextMondayTiem);

  console.log(nextMondayTiem);

  console.log(nextMondayData);

  // 年

  var nextMondY = nextMondayData.getFullYear();

  // 月

  var nextMondM = (nextMondayData.getMonth() + 1 < 10 ? '0' + (nextMondayData.getMonth() + 1) : nextMondayData.getMonth() + 1);

  // 日

  var nextMondD = nextMondayData.getDate() < 10 ? '0' + nextMondayData.getDate() : nextMondayData.getDate();

  // 时

  var nextMondH = nextMondayData.getHours() < 10 ? '0' + nextMondayData.getHours() : nextMondayData.getHours();

  // 分

  var nextMondMinu = nextMondayData.getMinutes() < 10 ? '0' + nextMondayData.getMinutes() : nextMondayData.getMinutes();

  // 秒

  var nextMondS = nextMondayData.getSeconds() < 10 ? '0' + nextMondayData.getSeconds() : nextMondayData.getSeconds();

  // 输出值

  var config = {

    nextMonday: nextMondY + '-' + nextMondM + '-' + nextMondD + ' ' + nextMondH + ':' + nextMondMinu + ':' + nextMondS

  }

  return config;

}

// 获取下周周一0点时间戳

function getNextMondayTime() {

  // 获取当天0点时间戳

  var timestamp = new Date(new Date().setHours(0,0,0,0)).getTime();

  var serverDate = new Date(timestamp);

  // 下周周一的时间戳

  var nextMondayTiem = timestamp + ((8 - serverDate.getDay()) * 24 * 60 * 60 * 1000);

  return nextMondayTiem;

}


 

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值