date.js Parse method overrides Javascript Parse method

最后都变了- 提交于 2019-12-12 18:37:58

问题


I'm including the date.js library in my site because I need its functionality.

I have just realized, though, that the standard Javascript parse() method is overwritten by it.

I'm trying to build a line chart in Highcharts, and the data series wants the first element to be be in milliseconds (their demos show them using the Date.UTC() method to achieve this, but my data is returned in a different format).

Short of doing a bunch of string manipulation to put my data into a format that Date.UTC will recognize, is there another way of getting the standard Javascript parse() functionality while date.js is loaded?


回答1:


I know this isn't a direct solution to your problem, but it may help anyway.

If you want a fully featured date library that doesn't modify the native Date object, I wrote one called Moment.js.

It provides a lot of the things that DateJS provides (formatting, parsing, manipulation, timeago, i18n, etc), but it's smaller, faster, and doesn't ruin the native date prototype.

https://github.com/timrwood/moment




回答2:


Nope, this is the intended design of date.js. It adds to the "prototype" of the Date object. Some people hate that, some people like it - but you've uncovered one of the drawbacks of this design.




回答3:


You can tell Highcharts to not use UTC date:

Highcharts.setOptions({
    global: {
        useUTC: false
    }
});

You should do this before you create the chart. Then you won't have to worry about converting your dates to UTC, it will be easier.




回答4:


after I asked the question, I went ahead and did it this way:

d = Date.parse(data);
            y = d.getFullYear();
            m = d.getMonth();
            d = d.getDate();
            dUTC = Date.UTC(y, m, d);

but will now try your suggestions.



来源:https://stackoverflow.com/questions/7934006/date-js-parse-method-overrides-javascript-parse-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!