admin管理员组文章数量:1022670
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 indate = Date.parse(line[0] +' UTC')
. Can youconsole.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
3 Answers
Reset to default 5Firefox 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...
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 indate = Date.parse(line[0] +' UTC')
. Can youconsole.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
3 Answers
Reset to default 5Firefox 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...
本文标签: javascriptCannot use dateparse on firefox It works on ChromeStack Overflow
版权声明:本文标题:javascript - Cannot use date.parse on firefox. It works on Chrome - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745540136a2155143.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论