js判斷時(shí)間戳是否在本周、根據(jù)時(shí)間戳獲取近七天以及近30天

?判斷時(shí)間戳是否在本周

isWeek:function (time) {

? ? // 當(dāng)前時(shí)間

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

? ? var serverDate = new Date(timestamp);

? ? //本周周日的的時(shí)間

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

? ? // 本周周一的時(shí)間

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

? ? // 當(dāng)前時(shí)間

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

? ? //時(shí)

? ? 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 + ',當(dāng)前時(shí)間:' + 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

? ? ? }?

? ? }

? }


根據(jù)時(shí)間戳獲取 近7天以及近30天

// 獲取當(dāng)天往后的七天或者一個(gè)月

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 // 當(dāng)前時(shí)間

? 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 // 之后的七天或者一個(gè)月

? return {

? ? t1: timer1,

? ? t2: timer2

? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 基礎(chǔ)類型有哪些?復(fù)雜類型有哪些?有什么特征?5種簡(jiǎn)單數(shù)據(jù)類型(基本數(shù)據(jù)類型):Undefined、Null、Boo...
    coolheadedY閱讀 375評(píng)論 0 0
  • Js獲取當(dāng)前日期時(shí)間及其它操作 var myDate = new Date(); myDate.getYear()...
    郝赫閱讀 1,206評(píng)論 0 3
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些閱讀 2,142評(píng)論 0 2
  • 23歲了、年齡的增長(zhǎng)好像并沒(méi)有帶來(lái)什么變化。今天給自己花了很多錢、以為會(huì)得到以往一樣的滿足感、可是并沒(méi)有。買買買已...
    阿阿無(wú)閱讀 200評(píng)論 0 0
  • 36W1D。27D。 還在下著雨。 今天是爸爸2016年的最后一個(gè)夜班,媽媽今天也堅(jiān)持了散步,涂色。今天感覺(jué)你開(kāi)始...
    林培閱讀 212評(píng)論 0 0

友情鏈接更多精彩內(nèi)容