获取需要的时间戳(开始 and 结束时间戳) 调用如下方法就行:
function getWantTimestamp(props) {
//当前时间
const nowDate = parseInt((new Date().getTime() / 1000).toString()); //当前时间
switch (props) {
// 当前时间时间戳
case "nowData": {
return nowDate;
}
// 当前零点得时间戳
case "nowZero": {
let nowZero = nowDate - (nowDate % 86400) - 3600 * 8;
return nowZero;
}
// 过去24小时的时间戳
case "formerlyDay": {
let formerlyDay = nowDate - 86400;
return formerlyDay;
}
// 昨天的零点的时间戳
case "yesterdayZero": {
let yesterdayZero = nowDate - (nowDate % 86400) - 3600 * 8 - 3600 * 24;
return yesterdayZero;
}
// 本周星期一零点的时间戳
case "thisMondayZero": {
let nowThisWeek = new Date().getDay(); //获取当前周
let a = nowDate - (nowThisWeek - 1) * 86400; //得到当前时间到这周
let thisMondayZero = a - (a % 86400) - 3600 * 8;
return thisMondayZero;
}
// 上周星期一零点的时间戳
case "lastMondayZero": {
let nowThisWeek = new Date().getDay(); //获取当前周
let a = nowDate - (nowThisWeek - 1) * 86400; //得到当前时间到这周
let thisMondayZero = a - (a % 86400) - 3600 * 8;
let lastMondayZero = thisMondayZero - 86400 * 7;
return lastMondayZero;
}
// 过去7天的时间戳
case "formerlySevenDay": {
let formerlySevenDay = nowDate - 86400 * 7;
return formerlySevenDay;
}
// 本月开始第一天零点的时间戳
case "thisMonthBeginZero": {
let MonthDate: any = new Date();
MonthDate.setDate(1); //set设置时间
MonthDate.setHours(0);
MonthDate.setSeconds(0);
MonthDate.setMinutes(0);
let thisMonthBeginZero = parseInt((MonthDate / 1000).toString());
return thisMonthBeginZero;
}
// 过去30天的时间戳
case "formerlyThirtyDays": {
let formerlyThirtyDays = nowDate - 86400 * 30;
return formerlyThirtyDays;
}
// 上个月的零点的时间戳
case "lastMonthDayZero": {
let nowMonthDate: any = new Date();
let getMonth = nowMonthDate.getMonth() + 1;
nowMonthDate.setMonth(getMonth - 2);
nowMonthDate.setDate(1); //set设置时间
nowMonthDate.setHours(0);
nowMonthDate.setSeconds(0);
nowMonthDate.setMinutes(0);
let lastMonthDayZero = parseInt((nowMonthDate / 1000).toString());
return lastMonthDayZero;
}
// 今年开始第一天零点的时间戳
case "thisYearDayZero": {
let yearDate: any = new Date();
yearDate.setMonth(0);
yearDate.setDate(1); //set设置时间
yearDate.setHours(0);
yearDate.setSeconds(0);
yearDate.setMinutes(0);
let thisYearDayZero = parseInt((yearDate / 1000).toString());
return thisYearDayZero;
}
// 过去12个月的时间戳
case "formerlyTwelveYearZero": {
let now12Date: any = new Date();
let getYear12 = now12Date.getFullYear();
now12Date.setYear(getYear12 - 1);
let formerlyTwelveYearZero = parseInt((now12Date / 1000).toString());
return formerlyTwelveYearZero;
}
// 去年开始第一天的时间戳
case "lastYearDayZero": {
let nowYearDate: any = new Date();
let getYear = nowYearDate.getFullYear();
nowYearDate.setYear(getYear - 1);
nowYearDate.setMonth(0);
nowYearDate.setDate(1); //set设置时间
nowYearDate.setHours(0);
nowYearDate.setSeconds(0);
nowYearDate.setMinutes(0);
let lastYearDayZero = parseInt((nowYearDate / 1000).toString());
return lastYearDayZero;
}
default: {
console.log("时间参数错误");
return 0;
}
}
}
调用getWantTimestamp()方法就能得到需要的时间戳:
getWantTimestamp("nowData")//nowData是switch的判断的参数
计算当前时间到今晚23:59:59的时间戳:
//当前23:59:59秒时间戳
let today = new Date(new Date().toLocaleDateString()).getTime() + 24*60*60*1000-1
//当前时间戳
let nowDate = parseInt((new Date().getTime()).toString());
//当前时间距离23:59:59秒的时间戳差值
console.log((today - nowDate) / 1000));
动态获取当前年月日时分秒:
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>当前年月日: {year}-{month < 10 ? `0${month}` : month}-{date}</p>
<p>当前时分秒: {hours}:{minutes < 10 ? `0${minutes}` : minutes}:{seconds < 10 ? `0${seconds}` : seconds}</p>
</div>
);
}
export default CurrentDateTime;