1

I have a graph component written in javascript using the canvas. You can update its values if you pass it a valid json array, of dates, coupled with prices of that date (stock trading candlesticks).

The jsonArray I try to populate on this call usually comes from creating new dates in js - but is there a way to send my jsonArray down the wire (from Primefaces) in such a way that the dates get interpreted as dates?

When I use

PrimeFaces.current().executeScript("myFunction(" + jsonObject.toString() + ")");

Dates that come down the wire are becoming long looking numbers which I guess are the number of milliseconds since 1970. What can I do to send this (rather large) jsonarray and have its dates interpreted as dates? (they fail on the date.getMonth() call, because they are numbers instead of dates).

When creating the jsonArray on the server side, I do the following, which looks wrong because getTime() returns a long. So how would dates be properly handled here?

json.addProperty("date", data.getKey().getTs().getTime());
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Mike
  • 609
  • 12
  • 36
  • 1
    Could always "hack" JavaScript to use Date methods on a number: `Number.prototype.getMonth=function(){return (new Date(this)).getMonth()}` Otherwise JSON is a data exchange format, it doesn't come with the capability to represent date objects within its structure. This would be relying completely upon your method for understanding it's supposed to turn something into a date object. You'd probably have to code something to transform it into a date object. Or whatever code you have that is calling getMonth, toss it through new Date first. – Ultimater Feb 24 '19 at 23:06
  • "toss it through "new Date:" first" - works like a charm! Please post that as an answer so I can accept it!! :) Thanks my friend!! – Mike Feb 24 '19 at 23:27
  • 1
    Post the code that worked and accept your own answer please so others can see where you inserted it in your code. – Ultimater Feb 24 '19 at 23:30
  • ok then, I just wanted you to get the credit for it... Thanks again. – Mike Feb 24 '19 at 23:31

1 Answers1

1

The function getting called with the long values as dates was the following. As Ultimater suggested, pass this object through new Date() - which should work for a date object - as well as a long, so no harm done!

dateToString(date, multiline) {

    if(date === null)
        return;

    // Added this
    date = new Date(date);

    var datestr = date.getMonth() + " " + date.getDay() + ", " + date.getFullYear();
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Mike
  • 609
  • 12
  • 36