-2

Anyone can help me out with the date format?

I have tried new Date(NumberValue) but returning Invalid Date

MockServer:

"NumberValue": "2.018081E7"

Formatter:

 if(ValueType === "D"){
                    return parseFloat(NumberValue);
Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
userfrh
  • 47
  • 2
  • 9
  • 1
    @userfrh The `"NumberValue"` property doesn't seem to be of type `Edm.DateTime` (OData V2) or `Edm.Date` (OData V4). Please make sure first to have a proper EDM type for all properties that hold date values. – Boghyon Hoffmann Apr 18 '19 at 06:54

2 Answers2

2

You could use a regex to split your date string into year, month, day, which you can then pass to 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

Edit based on comment:
If you can use destructuring — match() just returns an array so you can use indexes for the same effect:

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
Mark
  • 90,562
  • 7
  • 108
  • 148
  • Hey, thanks it does work when im using ES6. But unfortunately im using Javascript ES5.let [_, Y, M, D] = s.match(rx) this code somehow does not work for me. I've inspect and it stated s.match is not a function. – userfrh Apr 18 '19 at 06:56
  • @userfrh `match()` just returns an array, so you can save it as an array and index into it. See edit. – Mark Apr 18 '19 at 07:04
  • s.match still appear not a function. I tried to do var rx = new RegExp('/(\d{4})(\d{2})(\d{2})/'); like this as well. – userfrh Apr 18 '19 at 07:16
  • That’s strange @userfrh `match()` has been around for a longtime - ECMAScript 3 I think. Are you sure your starting with a string. Is it possible `20180810` is a number or `s` is not defined? – Mark Apr 18 '19 at 07:20
-1

You could achieve use moment.js

var val = parseFloat(2.018081E7);
var date = moment(val,'YYYYMMDD').format('DD MMM YYYY');
console.log(date)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
prasanth
  • 22,145
  • 4
  • 29
  • 53
  • I'm still new at this, where should i put ? Since im using XML. – userfrh Apr 18 '19 at 06:03
  • @userfrh for you reference [`script include xml`](https://stackoverflow.com/questions/384639/how-to-include-javascript-in-xml-document) – prasanth Apr 18 '19 at 06:08