javascript - 日期格式从(20180810)到(2018 年 8 月 10 日)
问题描述
任何人都可以帮我解决日期格式吗?
我试过new Date(NumberValue)
但返回无效日期
模拟服务器:
"NumberValue": "2.018081E7"
格式化程序:
if(ValueType === "D"){
return parseFloat(NumberValue);
解决方案
您可以使用正则表达式将日期字符串拆分为年、月、日,然后您可以将其传递给new Date()
const s = '20180810'
const rx = /(\d{4})(\d{2})(\d{2})/
let [_, Y, M, D] = s.match(rx)
console.log(new Date(Y, M - 1 , D)) // month is zero indexed
根据评论进行编辑:
如果您可以使用解构 -match()
只需返回一个数组,以便您可以使用索引来获得相同的效果:
var s = '20180810'
var rx = /(\d{4})(\d{2})(\d{2})/
var m = s.match(rx)
console.log(new Date(m[1], m[2] - 1, m[3])) // month is zero indexed
推荐阅读
- mysql - 在mySQL中使用带有外键的第三个表连接两个表
- r - 堆叠条形图作为每类观察百分比的函数,而不更改 ggplot2 中的值
- jenkins - 将本地 linux Jenkins 集成到 Github 企业的最佳方法是什么?
- java - Java中如何避免过多的for循环?
- java - 通过jFrame将数据插入mysql数据库
- ios - Swift - 在 viewWillTransition() 期间未调用 TextView layoutSubviews()
- filter - DAX - 在两个指定日期之间过滤
- python - 是否可以只获取 MongoDB 文档特定字段的值?
- node.js - RabbitMq 和 nodeJs 集成 当 2 条相互关联的消息到达队列时,订阅者上的调用函数
- javascript - 刷新页面或在浏览器中输入 URL 重定向到主页 Angular 7