return 使用 Date.UTC 转换时日期时间的正确月份
return the right month of a datetime when using Date.UTC conversion
我正在使用 Symfony、doctrine、twig 和 highchart.js。
我需要用 js 库和 doctrine ORM 数据对象创建图表。
在这里查看我的控制器:
public function chartTestAction() {
$em=$this->getDoctrine()->getManager();
$datasGraphique = $em->getRepository('mySpaceMyBundle:Graphique2')->findAll();
return $this->render('MySpaceMyBundle:myFolder:chartTest.html.twig',
array('datasGraphique' => $datasGraphique)
);
}
所以,我的实体在 table 架构中看起来像这样:
----------------
|Graphique entity|
|----------------|
|id |
|consomation1 |
|consomation2 |
|consomation3 |
|dateIndex |
|----------------|
为了解释,我的图表中有 3 个折线图,在我的折线图中,我需要像这样为每个组合设置 dateIndex example。
事实上,我在 xAxis 上有日期时间,一天中每小时每小时,这是一个固定的时间间隔。但是我需要在 yAxis 上为日期时间设置 xAxis 的参数,以便在正确的 dateIndex 上正确显示折线图。这意味着,xAxis 是固定的,每小时每小时,但是如果我在 2015-05-05 00:03:08
上有一个 dateIndex,则 yAxis 需要指向这个日期,而这个日期不会出现在 xAxis 每小时固定的小时上。
这是我的树枝视图,其中 javascript 部分包含 highchart.js 的系列代码:
series: [
//consomation1
{
yAxis: 1,
type: 'spline',
name: '{{consomation1Name}}',
data: [{% for data in datasGraphique %}
[Date.UTC({{data.dateIndex|date("Y,m,d,H,i,s"")}}), {{data.consomation1}}],
{% endfor %}],
tooltip: {
valueSuffix: ' kW'
},
},
//consomation2
{
yAxis: 2,
name: '{{consomation2Name}}' ,
data: [{% for data in datasGraphique %}
[Date.UTC({{data.dateIndex|date('Y,m,d,H,i,s"', "Europe/Paris")}}), {{data.consomation2}}],
{% endfor %}],
tooltip: {
valueSuffix: ' kW'
},
},
//consomation3
{
name: '{{consomation3Name}}' ,
data: [{% for data in datasGraphique %}
[Date.UTC({{data.dateIndex|date("Y,m,d,H,i,s"")}}), {{data.consomation3}}],
{% endfor %}],
type: 'spline',
tooltip: {
valueSuffix: ' °C'
},
dashStyle: 'shortdot',
}
]
Date.UTC 转换时出现问题。 事实上,js 脚本 returns 我没有错误,除了日期时间外,它运行良好。
例如在我的数据库中,我有这个字段 2015-05-05 00:03:08
与 2015 匹配年份,May 匹配 mounth,5 匹配天 00:03:08 匹配 hour:min:sec。但是在我的图表中,在 date.UTC 转换之后 returns 我这个日期的这个值 2015, Jun, 5, 00:03:08
与我的真实数据不匹配,只是这个月,因为在 UTC January = 0和 December = 11 而不是 datetime,其中 January = 1 和 December = 12。
有人知道解决办法吗?
我认为首先我需要在我的 dql 查询中将日期时间转换为 unix 时间戳,但我知道该学说不支持 UNIX_TIMESTAMP 函数转换。
我怎样才能获得正确的月份,我可以在 javascrip 中强制这样做吗?
这是我的 javascript 中用于 xAxis 的代码:
//xAxis setup to manage the chart
xAxis: [{
type: 'datetime',
dateTimeLabelFormats: {
day: '%H:%M',
},
tickInterval: 3600 * 1000,
title: {
text: '<b>Heures journalière</b> '
},
crosshair: true,
tickWidth: 1,
gridLineWidth: 2
}],
编辑 - 尝试 Date.parse()
我试过这个解决方案:
{
yAxis: 2,
name: '{{consomation1Name}}' ,
data: [{% for data in datasGraphique %}
[Date.parse({{data.dateIndex|date("Y-m-d H:i:s")}}), {{data.consoECS}}],
{% endfor %}],
tooltip: {
valueSuffix: ' kW'
},
},
它 returns 我这个错误 Uncaught SyntaxError: missing ) after argument list
:
data: [
[Date.parse(2015-05-05 00:03:08), 0], // the error occured on the first data render here
[Date.parse(2015-05-05 01:19:34), -2],
[Date.parse(2015-05-05 02:44:16), 0],
[Date.parse(2015-05-05 03:18:19), 0],
[Date.parse(2015-05-05 04:24:06), 0],
[Date.parse(2015-05-05 05:12:44), 0],
[Date.parse(2015-05-05 06:55:08), 0],
[Date.parse(2015-05-05 07:26:08), 0],
[Date.parse(2015-05-05 08:32:05), 1],
[Date.parse(2015-05-05 09:34:50), 2],
[Date.parse(2015-05-05 10:42:16), 3],
[Date.parse(2015-05-05 11:16:03), 3],
[Date.parse(2015-05-05 12:27:07), 1],
[Date.parse(2015-05-05 13:22:08), 1],
[Date.parse(2015-05-05 14:18:51), 3],
[Date.parse(2015-05-05 15:06:27), -1],
[Date.parse(2015-05-05 16:40:00), 2],
[Date.parse(2015-05-05 17:30:24), 1],
[Date.parse(2015-05-05 18:36:24), 0],
[Date.parse(2015-05-05 19:43:57), 0],
[Date.parse(2015-05-05 20:32:41), 0],
[Date.parse(2015-05-05 21:28:29), 0],
[Date.parse(2015-05-05 22:37:14), 0],
[Date.parse(2015-05-05 23:53:14), 0],
],
要使用 unix 时间戳并避免日期格式问题,您可以替换
[Date.UTC({{data.dateIndex|date("Y,m,d,H,i,s"")}})
来自
[Date.UTC({{data.dateIndex.date("U") }})
拖东西让它工作:
- 将
useUTC
设置为 false
- 更改日期格式并使用
Date.parse()
:Date.parse('{{data.dateIndex|date("Y-m-d H:i:s")}}')
我正在使用 Symfony、doctrine、twig 和 highchart.js。
我需要用 js 库和 doctrine ORM 数据对象创建图表。
在这里查看我的控制器:
public function chartTestAction() {
$em=$this->getDoctrine()->getManager();
$datasGraphique = $em->getRepository('mySpaceMyBundle:Graphique2')->findAll();
return $this->render('MySpaceMyBundle:myFolder:chartTest.html.twig',
array('datasGraphique' => $datasGraphique)
);
}
所以,我的实体在 table 架构中看起来像这样:
----------------
|Graphique entity|
|----------------|
|id |
|consomation1 |
|consomation2 |
|consomation3 |
|dateIndex |
|----------------|
为了解释,我的图表中有 3 个折线图,在我的折线图中,我需要像这样为每个组合设置 dateIndex example。
事实上,我在 xAxis 上有日期时间,一天中每小时每小时,这是一个固定的时间间隔。但是我需要在 yAxis 上为日期时间设置 xAxis 的参数,以便在正确的 dateIndex 上正确显示折线图。这意味着,xAxis 是固定的,每小时每小时,但是如果我在 2015-05-05 00:03:08
上有一个 dateIndex,则 yAxis 需要指向这个日期,而这个日期不会出现在 xAxis 每小时固定的小时上。
这是我的树枝视图,其中 javascript 部分包含 highchart.js 的系列代码:
series: [
//consomation1
{
yAxis: 1,
type: 'spline',
name: '{{consomation1Name}}',
data: [{% for data in datasGraphique %}
[Date.UTC({{data.dateIndex|date("Y,m,d,H,i,s"")}}), {{data.consomation1}}],
{% endfor %}],
tooltip: {
valueSuffix: ' kW'
},
},
//consomation2
{
yAxis: 2,
name: '{{consomation2Name}}' ,
data: [{% for data in datasGraphique %}
[Date.UTC({{data.dateIndex|date('Y,m,d,H,i,s"', "Europe/Paris")}}), {{data.consomation2}}],
{% endfor %}],
tooltip: {
valueSuffix: ' kW'
},
},
//consomation3
{
name: '{{consomation3Name}}' ,
data: [{% for data in datasGraphique %}
[Date.UTC({{data.dateIndex|date("Y,m,d,H,i,s"")}}), {{data.consomation3}}],
{% endfor %}],
type: 'spline',
tooltip: {
valueSuffix: ' °C'
},
dashStyle: 'shortdot',
}
]
Date.UTC 转换时出现问题。 事实上,js 脚本 returns 我没有错误,除了日期时间外,它运行良好。
例如在我的数据库中,我有这个字段 2015-05-05 00:03:08
与 2015 匹配年份,May 匹配 mounth,5 匹配天 00:03:08 匹配 hour:min:sec。但是在我的图表中,在 date.UTC 转换之后 returns 我这个日期的这个值 2015, Jun, 5, 00:03:08
与我的真实数据不匹配,只是这个月,因为在 UTC January = 0和 December = 11 而不是 datetime,其中 January = 1 和 December = 12。
有人知道解决办法吗?
我认为首先我需要在我的 dql 查询中将日期时间转换为 unix 时间戳,但我知道该学说不支持 UNIX_TIMESTAMP 函数转换。
我怎样才能获得正确的月份,我可以在 javascrip 中强制这样做吗?
这是我的 javascript 中用于 xAxis 的代码:
//xAxis setup to manage the chart
xAxis: [{
type: 'datetime',
dateTimeLabelFormats: {
day: '%H:%M',
},
tickInterval: 3600 * 1000,
title: {
text: '<b>Heures journalière</b> '
},
crosshair: true,
tickWidth: 1,
gridLineWidth: 2
}],
编辑 - 尝试 Date.parse()
我试过这个解决方案:
{
yAxis: 2,
name: '{{consomation1Name}}' ,
data: [{% for data in datasGraphique %}
[Date.parse({{data.dateIndex|date("Y-m-d H:i:s")}}), {{data.consoECS}}],
{% endfor %}],
tooltip: {
valueSuffix: ' kW'
},
},
它 returns 我这个错误 Uncaught SyntaxError: missing ) after argument list
:
data: [
[Date.parse(2015-05-05 00:03:08), 0], // the error occured on the first data render here
[Date.parse(2015-05-05 01:19:34), -2],
[Date.parse(2015-05-05 02:44:16), 0],
[Date.parse(2015-05-05 03:18:19), 0],
[Date.parse(2015-05-05 04:24:06), 0],
[Date.parse(2015-05-05 05:12:44), 0],
[Date.parse(2015-05-05 06:55:08), 0],
[Date.parse(2015-05-05 07:26:08), 0],
[Date.parse(2015-05-05 08:32:05), 1],
[Date.parse(2015-05-05 09:34:50), 2],
[Date.parse(2015-05-05 10:42:16), 3],
[Date.parse(2015-05-05 11:16:03), 3],
[Date.parse(2015-05-05 12:27:07), 1],
[Date.parse(2015-05-05 13:22:08), 1],
[Date.parse(2015-05-05 14:18:51), 3],
[Date.parse(2015-05-05 15:06:27), -1],
[Date.parse(2015-05-05 16:40:00), 2],
[Date.parse(2015-05-05 17:30:24), 1],
[Date.parse(2015-05-05 18:36:24), 0],
[Date.parse(2015-05-05 19:43:57), 0],
[Date.parse(2015-05-05 20:32:41), 0],
[Date.parse(2015-05-05 21:28:29), 0],
[Date.parse(2015-05-05 22:37:14), 0],
[Date.parse(2015-05-05 23:53:14), 0],
],
要使用 unix 时间戳并避免日期格式问题,您可以替换
[Date.UTC({{data.dateIndex|date("Y,m,d,H,i,s"")}})
来自
[Date.UTC({{data.dateIndex.date("U") }})
拖东西让它工作:
- 将
useUTC
设置为 false - 更改日期格式并使用
Date.parse()
:Date.parse('{{data.dateIndex|date("Y-m-d H:i:s")}}')