React 如何使用时间戳计算得到开始和结束时间戳

 更新时间:2023年09月22日 15:35:13   作者:zhoupenghui168  
这篇文章主要介绍了React 如何拿时间戳计算得到开始和结束时间戳,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

获取需要的时间戳(开始 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;

到此这篇关于React 如何拿时间戳计算得到开始和结束时间戳的文章就介绍到这了,更多相关React计算开始和结束时间戳内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • React实现组件全屏化的操作方法

    React实现组件全屏化的操作方法

    开发今天给我提了一个sql编辑器输入框比较小,不支持放大,不太方便,下面看下我的处理方法,本文基于React+antd,给大家演示一个完整的全屏demo,感兴趣的朋友一起看看吧
    2021-10-10
  • React状态管理的简明指南

    React状态管理的简明指南

    Redux 是React最常用的集中状态管理工具,本文主要介绍了React状态管理的简明指南,具有一定的参考价值,感兴趣的可以了解一下
    2024-01-01
  • 详解React Fiber的工作原理

    详解React Fiber的工作原理

    这篇文章主要介绍了React Fiber的工作原理的相关资料,帮助大家更好的理解和学习使用React框架,感兴趣的朋友可以了解下
    2021-04-04
  • 基于React实现一个todo打勾效果

    基于React实现一个todo打勾效果

    这篇文章主要为大家详细介绍了如何基于React实现一个todo打勾效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-03-03
  • React commit源码分析详解

    React commit源码分析详解

    前两章讲到了,react 在 render 阶段的 completeUnitWork 执行完毕后,就执行 commitRoot 进入到了 commit 阶段,本章将讲解 commit 阶段执行过程源码
    2022-11-11
  • React进行路由跳转的方法汇总

    React进行路由跳转的方法汇总

    在 React 中进行路由跳转有多种方法,具体取决于你使用的路由库和版本,以下是常见的路由跳转方法汇总,主要基于 react-router-dom 库,文中并通过代码示例介绍的非常详细,需要的朋友可以参考下
    2025-02-02
  • React Context详解使用过程

    React Context详解使用过程

    在Reactor中提供了Context来替代ThreadLocal,可以实现一个跨线程的共享变量的透明方式。本文主要为大家介绍了Context的用法的用法,感兴趣的可以了解一下
    2023-03-03
  • React Native 中添加自定义字体的方法

    React Native 中添加自定义字体的方法

    这篇文章主要介绍了如何在 React Native 中添加自定义字体,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-08-08
  • react中定义变量并使用方式

    react中定义变量并使用方式

    这篇文章主要介绍了react中定义变量并使用方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • react中Suspense的使用详解

    react中Suspense的使用详解

    这篇文章主要介绍了react中Suspense的使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09

最新评论