最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot use date.parse on firefox. It works on Chrome - Stack Overflow

programmeradmin0浏览0评论

I'm not experienced with jQuery, so I stacked to a problem. The problem is that in Chrome my code works fine, but in Firefox it doesn't. It seems that the date.parse mand is not working because "tsv" data is fine but "date" data is not.

The codes is below. If anyone can help me please let me know...

jQuery.get('data.php', null, function(tsv) {
                var lines = [];
                traffic = [];
                try {
                    // split the data return into lines and parse them
                    tsv = tsv.split(/\n/g);
                    jQuery.each(tsv, function(i, line) {
                        line = line.split(/\t/);
                    var date = Date.parse(line[0] +' UTC');
                        traffic.push([
                            date,
                            parseFloat(line[1].replace(',', ' '), 10)
                        ]);
                    }) ;
                } catch (e) {  }
                options.series[0].data = traffic;
                chart = new Highcharts.Chart(options);

    // alert(tsv);
     // alert(traffic);
   //    alert(date);
            }) ;

I'm not experienced with jQuery, so I stacked to a problem. The problem is that in Chrome my code works fine, but in Firefox it doesn't. It seems that the date.parse mand is not working because "tsv" data is fine but "date" data is not.

The codes is below. If anyone can help me please let me know...

jQuery.get('data.php', null, function(tsv) {
                var lines = [];
                traffic = [];
                try {
                    // split the data return into lines and parse them
                    tsv = tsv.split(/\n/g);
                    jQuery.each(tsv, function(i, line) {
                        line = line.split(/\t/);
                    var date = Date.parse(line[0] +' UTC');
                        traffic.push([
                            date,
                            parseFloat(line[1].replace(',', ' '), 10)
                        ]);
                    }) ;
                } catch (e) {  }
                options.series[0].data = traffic;
                chart = new Highcharts.Chart(options);

    // alert(tsv);
     // alert(traffic);
   //    alert(date);
            }) ;
Share Improve this question edited Dec 24, 2015 at 16:03 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 7, 2013 at 13:34 NaThANNaThAN 233 silver badges8 bronze badges 4
  • 1 What is format of date string in line[0]? – Anoop Commented Apr 7, 2013 at 13:36
  • I have corrected it to "var date = Date.parse(line[0] +' UTC');" – NaThAN Commented Apr 7, 2013 at 13:37
  • I think we need to know what line[0] evaluates to in date = Date.parse(line[0] +' UTC'). Can you console.log() this value and add to your question? – Paul Grime Commented Apr 7, 2013 at 13:39
  • I dont know how to use console.log(); The date data is NaN and tsv data= 2013-04-06 22:46:08 24.81,2013-04-06 22:47:07 24.75,2013-04-06 22:48:06 24.69,2013-04-06 22:49:06 24.63,2013-04-06 22:50:06 24.63,2013-04-06 22:51:06 24.63,2013-04-06 22:52:06 – NaThAN Commented Apr 7, 2013 at 13:46
Add a ment  | 

3 Answers 3

Reset to default 5

Firefox does not support parsing dates in the format 2013-04-06 22:46:08.

You can use the date.js library to get support for this format.

If you don't want to use date.js then the function below will parse the date from yyyy-mm-dd HH:mm:ss format to UTC.

function parseDateUTC(input) {
    var reg = /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/;
    var parts = reg.exec(input);
    return parts ? (new Date(Date.UTC(parts[1], parts[2] -1, parts[3], parts[4], parts[5],parts[6]))) : null
}

Then you just have to change your code to

var date = parseDateUTC(line[0]);

date.js is no longer updated, in fact, the website is down for me. I have read that the best replacement for it is MomentJS. as per parsing

moment().format('1995-02-23 12:14:16', 'YYYY-MM-DD hh:mm:ss');

I had a similar problem. I ended up including date.js (include this as a script and it will overwrite the date.parse() function).

Yet, to make it work in HighStocks I needed to convert the result into milliseconds. If dateString contains a Date in one of the formats date.js can convert, then

dateString= Date.parse(dateString);

works only in Chrome, but

dateString= Date.parse(dateString).getTime();

converts any of the results browsers offer into milliseconds and so far it works for me...

发布评论

评论列表(0)

  1. 暂无评论