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 hooks实践

    记录一次完整的react hooks实践

    这篇文章主要介绍了记录一次完整的react hooks实践,通过一个简单示例,介绍了react hooks,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-03-03
  • React Hooks常用场景的使用(小结)

    React Hooks常用场景的使用(小结)

    这篇文章主要介绍了React Hooks常用场景的使用,根据使用场景分别进行举例说明,帮助你认识理解并可以熟练运用 React Hooks 大部分特性,感兴趣的可以了解一下
    2021-04-04
  • React SSR 中的限流案例详解

    React SSR 中的限流案例详解

    这篇文章主要介绍了React SSR 之限流,React SSR 毕竟涉及到了服务端,有很多服务端特有的问题需要考虑,而限流就是其中之一,本文会通过一个简单的案例来说明,为什么服务端需要进行限流,需要的朋友可以参考下
    2022-07-07
  • 使用react+redux实现弹出框案例

    使用react+redux实现弹出框案例

    这篇文章主要为大家详细介绍了使用react+redux实现弹出框案例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • 详解React中的组件通信问题

    详解React中的组件通信问题

    本篇文章中主要介绍了详解React中的组件通信问题,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • React Fragment 和空标签(<></>)用法及区别详解

    React Fragment 和空标签(<></>)用法及区别详解

    本文详细介绍了React中的Fragment和空标签的使用,包括它们的区别、使用场景、性能考虑以及最佳实践,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧
    2025-01-01
  • React.cloneElement的使用详解

    React.cloneElement的使用详解

    这篇文章主要介绍了React.cloneElement的使用详解,帮助大家更好的理解和学习使用React框架,感兴趣的朋友可以了解下
    2021-04-04
  • Modal.confirm是否违反了React模式分析

    Modal.confirm是否违反了React模式分析

    这篇文章主要为大家介绍了Modal.confirm是否违反了React模式分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • React编程中需要注意的两个错误

    React编程中需要注意的两个错误

    React可以说是前端的先驱者,它总是会引领整个前端的潮流。 但我们在使用也经常会遇到错误,下面这篇文章主要给大家介绍了关于React编程中需要注意的两个错误,需要的朋友可以参考下
    2021-05-05
  • React与Redux之数组处理讲解

    React与Redux之数组处理讲解

    这篇文章主要介绍了React与Redux之数组处理讲解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-09-09

最新评论