React 如何使用時(shí)間戳計(jì)算得到開始和結(jié)束時(shí)間戳
獲取需要的時(shí)間戳(開始 and 結(jié)束時(shí)間戳) 調(diào)用如下方法就行:
function getWantTimestamp(props) { //當(dāng)前時(shí)間 const nowDate = parseInt((new Date().getTime() / 1000).toString()); //當(dāng)前時(shí)間 switch (props) { // 當(dāng)前時(shí)間時(shí)間戳 case "nowData": { return nowDate; } // 當(dāng)前零點(diǎn)得時(shí)間戳 case "nowZero": { let nowZero = nowDate - (nowDate % 86400) - 3600 * 8; return nowZero; } // 過(guò)去24小時(shí)的時(shí)間戳 case "formerlyDay": { let formerlyDay = nowDate - 86400; return formerlyDay; } // 昨天的零點(diǎn)的時(shí)間戳 case "yesterdayZero": { let yesterdayZero = nowDate - (nowDate % 86400) - 3600 * 8 - 3600 * 24; return yesterdayZero; } // 本周星期一零點(diǎn)的時(shí)間戳 case "thisMondayZero": { let nowThisWeek = new Date().getDay(); //獲取當(dāng)前周 let a = nowDate - (nowThisWeek - 1) * 86400; //得到當(dāng)前時(shí)間到這周 let thisMondayZero = a - (a % 86400) - 3600 * 8; return thisMondayZero; } // 上周星期一零點(diǎn)的時(shí)間戳 case "lastMondayZero": { let nowThisWeek = new Date().getDay(); //獲取當(dāng)前周 let a = nowDate - (nowThisWeek - 1) * 86400; //得到當(dāng)前時(shí)間到這周 let thisMondayZero = a - (a % 86400) - 3600 * 8; let lastMondayZero = thisMondayZero - 86400 * 7; return lastMondayZero; } // 過(guò)去7天的時(shí)間戳 case "formerlySevenDay": { let formerlySevenDay = nowDate - 86400 * 7; return formerlySevenDay; } // 本月開始第一天零點(diǎn)的時(shí)間戳 case "thisMonthBeginZero": { let MonthDate: any = new Date(); MonthDate.setDate(1); //set設(shè)置時(shí)間 MonthDate.setHours(0); MonthDate.setSeconds(0); MonthDate.setMinutes(0); let thisMonthBeginZero = parseInt((MonthDate / 1000).toString()); return thisMonthBeginZero; } // 過(guò)去30天的時(shí)間戳 case "formerlyThirtyDays": { let formerlyThirtyDays = nowDate - 86400 * 30; return formerlyThirtyDays; } // 上個(gè)月的零點(diǎn)的時(shí)間戳 case "lastMonthDayZero": { let nowMonthDate: any = new Date(); let getMonth = nowMonthDate.getMonth() + 1; nowMonthDate.setMonth(getMonth - 2); nowMonthDate.setDate(1); //set設(shè)置時(shí)間 nowMonthDate.setHours(0); nowMonthDate.setSeconds(0); nowMonthDate.setMinutes(0); let lastMonthDayZero = parseInt((nowMonthDate / 1000).toString()); return lastMonthDayZero; } // 今年開始第一天零點(diǎn)的時(shí)間戳 case "thisYearDayZero": { let yearDate: any = new Date(); yearDate.setMonth(0); yearDate.setDate(1); //set設(shè)置時(shí)間 yearDate.setHours(0); yearDate.setSeconds(0); yearDate.setMinutes(0); let thisYearDayZero = parseInt((yearDate / 1000).toString()); return thisYearDayZero; } // 過(guò)去12個(gè)月的時(shí)間戳 case "formerlyTwelveYearZero": { let now12Date: any = new Date(); let getYear12 = now12Date.getFullYear(); now12Date.setYear(getYear12 - 1); let formerlyTwelveYearZero = parseInt((now12Date / 1000).toString()); return formerlyTwelveYearZero; } // 去年開始第一天的時(shí)間戳 case "lastYearDayZero": { let nowYearDate: any = new Date(); let getYear = nowYearDate.getFullYear(); nowYearDate.setYear(getYear - 1); nowYearDate.setMonth(0); nowYearDate.setDate(1); //set設(shè)置時(shí)間 nowYearDate.setHours(0); nowYearDate.setSeconds(0); nowYearDate.setMinutes(0); let lastYearDayZero = parseInt((nowYearDate / 1000).toString()); return lastYearDayZero; } default: { console.log("時(shí)間參數(shù)錯(cuò)誤"); return 0; } } }
調(diào)用getWantTimestamp()方法就能得到需要的時(shí)間戳:
getWantTimestamp("nowData")//nowData是switch的判斷的參數(shù)
計(jì)算當(dāng)前時(shí)間到今晚23:59:59的時(shí)間戳:
//當(dāng)前23:59:59秒時(shí)間戳 let today = new Date(new Date().toLocaleDateString()).getTime() + 24*60*60*1000-1 //當(dāng)前時(shí)間戳 let nowDate = parseInt((new Date().getTime()).toString()); //當(dāng)前時(shí)間距離23:59:59秒的時(shí)間戳差值 console.log((today - nowDate) / 1000));
動(dòng)態(tài)獲取當(dāng)前年月日時(shí)分秒:
import React, { useState, useEffect } from 'react'; function CurrentDateTime() { const [currentDateTime, setCurrentDateTime] = useState(new Date()); useEffect(() => { const interval = setInterval(() => { setCurrentDateTime(new Date()); }, 1000); // 每秒更新一次 return () => { clearInterval(interval); }; }, []); const year = currentDateTime.getFullYear(); const month = currentDateTime.getMonth() + 1; // 月份從 0 開始,因此需要加 1 const date = currentDateTime.getDate(); const hours = currentDateTime.getHours(); const minutes = currentDateTime.getMinutes(); const seconds = currentDateTime.getSeconds(); return ( <div> <p>當(dāng)前年月日: {year}-{month < 10 ? `0${month}` : month}-{date}</p> <p>當(dāng)前時(shí)分秒: {hours}:{minutes < 10 ? `0${minutes}` : minutes}:{seconds < 10 ? `0${seconds}` : seconds}</p> </div> ); } export default CurrentDateTime;
到此這篇關(guān)于React 如何拿時(shí)間戳計(jì)算得到開始和結(jié)束時(shí)間戳的文章就介紹到這了,更多相關(guān)React計(jì)算開始和結(jié)束時(shí)間戳內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于React實(shí)現(xiàn)一個(gè)todo打勾效果
這篇文章主要為大家詳細(xì)介紹了如何基于React實(shí)現(xiàn)一個(gè)todo打勾效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03