首页 > 解决方案 > 日期格式从(20180810)到(2018 年 8 月 10 日)

问题描述

任何人都可以帮我解决日期格式吗?

我试过new Date(NumberValue)但返回无效日期

模拟服务器:

"NumberValue": "2.018081E7"

格式化程序:

 if(ValueType === "D"){
                    return parseFloat(NumberValue);

标签: javascriptxmlodatasapui5

解决方案


您可以使用正则表达式将日期字符串拆分为年、月、日,然后您可以将其传递给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


推荐阅读