javascript - 将 bigint 字段中存储的日期转换为正常日期格式

标签 javascript reactjs

我在react.js 文件中有一个方法,可以将日期保存在下面的bigint 字段中

    getTodaysDate(){
        let formatTwoDigits = (digit) => ("0" + digit).slice(-2);
        var tempDate = new Date();
        var date = `${tempDate.getFullYear()}${formatTwoDigits(tempDate.getMonth()+1)}${formatTwoDigits(tempDate.getDate())}${formatTwoDigits(tempDate.getHours())}${formatTwoDigits(tempDate.getMinutes())}${formatTwoDigits(tempDate.getSeconds())}`;
        return date;
      }

这是sql数据库中保存的内容

    20190313133112

我想读出此内容并以如下所示的有意义的格式显示

    mm/dd/yyyy  [3/13/2019]

如何将此存储的日期转换为上面的正常日期?

最佳答案

您可以使用替换。

^(\d{4})(\d{2})(\d{2}).*
   |       |       |   |_____ Anything except new line
   |       |       |  
   |       |       |_________ Day   ( group 3 `$3`)
   |       |
   |       |_________________ Month ( group 2 `$2`)
   |        
   |_________________________ year  ( group 1 `$1`)

let str = `20190313133112`

let op = str.replace(/^(\d{4})(\d{2})(\d{2}).*/,"$2/$3/$1")

console.log(op)

关于javascript - 将 bigint 字段中存储的日期转换为正常日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55149179/

相关文章:

javascript - React 组件 - Plotly 图表创建器

javascript - 用 JavaScript 解释这种排序

javascript - React.js : How to use the same button while passing variables to a click function

html - Reactjs 热加载器 css 文件

javascript - Next js 和 Apollo - Cookie 未被传递

javascript - Express/Node中3个相关类别的RESTful路由结构

javascript - 从javascript钛数组中删除项目

javascript - amCharts 工具提示仅在 DateAxis 上显示,但在 ValueAxis 上不显示

javascript - 获取gridpanel的单元格值。扩展Js

javascript - 将 css 转换添加到不断增长的元素列表中