• React 如何拿时间戳计算得到开始和结束时间戳


    获取需要的时间戳(开始 and 结束时间戳) 调用如下方法就行:

    1. function getWantTimestamp(props) {
    2. //当前时间
    3. const nowDate = parseInt((new Date().getTime() / 1000).toString()); //当前时间
    4. switch (props) {
    5. // 当前时间时间戳
    6. case "nowData": {
    7. return nowDate;
    8. }
    9. // 当前零点得时间戳
    10. case "nowZero": {
    11. let nowZero = nowDate - (nowDate % 86400) - 3600 * 8;
    12. return nowZero;
    13. }
    14. // 过去24小时的时间戳
    15. case "formerlyDay": {
    16. let formerlyDay = nowDate - 86400;
    17. return formerlyDay;
    18. }
    19. // 昨天的零点的时间戳
    20. case "yesterdayZero": {
    21. let yesterdayZero = nowDate - (nowDate % 86400) - 3600 * 8 - 3600 * 24;
    22. return yesterdayZero;
    23. }
    24. // 本周星期一零点的时间戳
    25. case "thisMondayZero": {
    26. let nowThisWeek = new Date().getDay(); //获取当前周
    27. let a = nowDate - (nowThisWeek - 1) * 86400; //得到当前时间到这周
    28. let thisMondayZero = a - (a % 86400) - 3600 * 8;
    29. return thisMondayZero;
    30. }
    31. // 上周星期一零点的时间戳
    32. case "lastMondayZero": {
    33. let nowThisWeek = new Date().getDay(); //获取当前周
    34. let a = nowDate - (nowThisWeek - 1) * 86400; //得到当前时间到这周
    35. let thisMondayZero = a - (a % 86400) - 3600 * 8;
    36. let lastMondayZero = thisMondayZero - 86400 * 7;
    37. return lastMondayZero;
    38. }
    39. // 过去7天的时间戳
    40. case "formerlySevenDay": {
    41. let formerlySevenDay = nowDate - 86400 * 7;
    42. return formerlySevenDay;
    43. }
    44. // 本月开始第一天零点的时间戳
    45. case "thisMonthBeginZero": {
    46. let MonthDate: any = new Date();
    47. MonthDate.setDate(1); //set设置时间
    48. MonthDate.setHours(0);
    49. MonthDate.setSeconds(0);
    50. MonthDate.setMinutes(0);
    51. let thisMonthBeginZero = parseInt((MonthDate / 1000).toString());
    52. return thisMonthBeginZero;
    53. }
    54. // 过去30天的时间戳
    55. case "formerlyThirtyDays": {
    56. let formerlyThirtyDays = nowDate - 86400 * 30;
    57. return formerlyThirtyDays;
    58. }
    59. // 上个月的零点的时间戳
    60. case "lastMonthDayZero": {
    61. let nowMonthDate: any = new Date();
    62. let getMonth = nowMonthDate.getMonth() + 1;
    63. nowMonthDate.setMonth(getMonth - 2);
    64. nowMonthDate.setDate(1); //set设置时间
    65. nowMonthDate.setHours(0);
    66. nowMonthDate.setSeconds(0);
    67. nowMonthDate.setMinutes(0);
    68. let lastMonthDayZero = parseInt((nowMonthDate / 1000).toString());
    69. return lastMonthDayZero;
    70. }
    71. // 今年开始第一天零点的时间戳
    72. case "thisYearDayZero": {
    73. let yearDate: any = new Date();
    74. yearDate.setMonth(0);
    75. yearDate.setDate(1); //set设置时间
    76. yearDate.setHours(0);
    77. yearDate.setSeconds(0);
    78. yearDate.setMinutes(0);
    79. let thisYearDayZero = parseInt((yearDate / 1000).toString());
    80. return thisYearDayZero;
    81. }
    82. // 过去12个月的时间戳
    83. case "formerlyTwelveYearZero": {
    84. let now12Date: any = new Date();
    85. let getYear12 = now12Date.getFullYear();
    86. now12Date.setYear(getYear12 - 1);
    87. let formerlyTwelveYearZero = parseInt((now12Date / 1000).toString());
    88. return formerlyTwelveYearZero;
    89. }
    90. // 去年开始第一天的时间戳
    91. case "lastYearDayZero": {
    92. let nowYearDate: any = new Date();
    93. let getYear = nowYearDate.getFullYear();
    94. nowYearDate.setYear(getYear - 1);
    95. nowYearDate.setMonth(0);
    96. nowYearDate.setDate(1); //set设置时间
    97. nowYearDate.setHours(0);
    98. nowYearDate.setSeconds(0);
    99. nowYearDate.setMinutes(0);
    100. let lastYearDayZero = parseInt((nowYearDate / 1000).toString());
    101. return lastYearDayZero;
    102. }
    103. default: {
    104. console.log("时间参数错误");
    105. return 0;
    106. }
    107. }
    108. }

    调用getWantTimestamp()方法就能得到需要的时间戳:

    getWantTimestamp("nowData")//nowData是switch的判断的参数

     计算当前时间到今晚23:59:59的时间戳:

    1. //当前23:59:59秒时间戳
    2. let today = new Date(new Date().toLocaleDateString()).getTime() + 24*60*60*1000-1
    3. //当前时间戳
    4. let nowDate = parseInt((new Date().getTime()).toString());
    5. //当前时间距离23:59:59秒的时间戳差值
    6. console.log((today - nowDate) / 1000));

    动态获取当前年月日时分秒

    1. import React, { useState, useEffect } from 'react';
    2. function CurrentDateTime() {
    3. const [currentDateTime, setCurrentDateTime] = useState(new Date());
    4. useEffect(() => {
    5. const interval = setInterval(() => {
    6. setCurrentDateTime(new Date());
    7. }, 1000); // 每秒更新一次
    8. return () => {
    9. clearInterval(interval);
    10. };
    11. }, []);
    12. const year = currentDateTime.getFullYear();
    13. const month = currentDateTime.getMonth() + 1; // 月份从 0 开始,因此需要加 1
    14. const date = currentDateTime.getDate();
    15. const hours = currentDateTime.getHours();
    16. const minutes = currentDateTime.getMinutes();
    17. const seconds = currentDateTime.getSeconds();
    18. return (
    19. <div>
    20. <p>当前年月日: {year}-{month < 10 ? `0${month}` : month}-{date}p>
    21. <p>当前时分秒: {hours}:{minutes < 10 ? `0${minutes}` : minutes}:{seconds < 10 ? `0${seconds}` : seconds}p>
    22. div>
    23. );
    24. }
    25. export default CurrentDateTime;

  • 相关阅读:
    [Acwing] 58周赛 4490. 染色
    【软考 系统架构设计师】系统可靠性分析与设计① 系统可靠性分析
    ETL的数据挖掘方式
    【Processing】图像处理框架语言(Hello,Processing!
    WMS系统后端开发-用户权限
    OnlyOffice文档服务器安装及集成使用
    【程序员面试金典】01.05. 一次编辑
    算法题练习——JS Node+python题解合并k个已排序的链表及链表的奇偶重排
    阿里云 短信服务——发送短信验证码图文教程
    协议僵化 or 协议僵化
  • 原文地址:https://blog.csdn.net/zhoupenghui168/article/details/133141468