Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undesired American date format #170

Open
rafalsty opened this issue Aug 21, 2018 · 0 comments
Open

Undesired American date format #170

rafalsty opened this issue Aug 21, 2018 · 0 comments

Comments

@rafalsty
Copy link

rafalsty commented Aug 21, 2018

Hi, first of all thanks for your library.

I'm using jqPlot 1.0.9 to create line chart as follows:

var plot = $.jqplot('lineChart', [dataArray],
    {
        axes: {
            xaxis: {
                renderer: $.jqplot.DateAxisRenderer,
                tickOptions: {
                    formatString: '%#d.%#m.%Y'
                },
                tickInterval: selectedInterval,
                rendererOptions: {sortMergedLabels:true}
            }
        },
...

I provide all the dates as day.month.year, e.g. 16.08.2018. The thing is that it shows points on x axis correctly only if they cannot be treated as American date format. For example, when I have 1.08.2018, the point on the plot is incorrectly selected for 8.1.2018. And when I have 16.08.2018, it is correctly treated as 16.8.2018. This error happens in Chrome but not in Firefox.

What could be the reason of this strange behavior and what is the solution to it? Thanks!

PS There is this line

d[j][0] = new $.jsDate(d[j][0]).getTime();

in jqplot.dateAxisRenderer.js

I have checked that the values are different in Firefox and Chrome. I've saved values of d[j][0] before and after the call to this line of code, with the use of console.log.

For Chrome: before 31.07.2018, after 1532988000000, before 31.07.2018, after 1532988000000, before 01.08.2018, after 1515366000000, before 31.07.2018, after 1533031200000

For Firefox: before 31.07.2018, after 1532988000000, before 31.07.2018, after 1532988000000, before 01.08.2018, after 1533074400000, before 31.07.2018, after 1533031200000

This value is different by browser for 1/08. So it looks like getTime() works in different way in Chrome and Firefox.

PS2 Further it is set in the line 10684 in jquery.jqplot.js:

ms = Date.parse(current);

where current is "01.08.2018" and ms is 1515366000000. As far as I know, Date.parse uses MM/DD/YYYY by default.

Changing this line to

ms = moment(current, "DD.MM.YYYY")

seems to solve the issue. Unfortunately, it breaks something else, i.e. when this line of code

var nmonths = mend.diff(mstart, 'month');

is executed from DateAxisRenderer, it then reaches jsDate.diff, jsDate and jsDate.createDate and ends with "d2.getFullYear is not a function" in this place:

var diffYears = d1.getFullYear() - d2.getFullYear();

PS3 The same problem is present if I use original version of jqPlot and have only renderer specified, without options:

                xaxis: {
                    renderer: $.jqplot.DateAxisRenderer,
                }

PS4 The following code worked for me but I would be happier if I could receive the real fix:

            // Original code: ms = Date.parse(current);

            // Workaround:
            if (current.indexOf('/') !== -1 || current.indexOf('GMT') !== -1) {
                ms = Date.parse(current);
            } else {
                if (current.indexOf(':') !== -1) {
                    ms = moment(current, "DD.MM.YYYY HH:mm");
                } else {
                    ms = moment(current, "DD.MM.YYYY");
                }
            }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants