JavaScript Date Object#39;s month index begins with 0(JavaScript Date 对象的月份索引从 0 开始)
问题描述
我的目标是以有效的方式将 MySQL 中的时间戳转换为 JavaScript Date 对象.这是我当前的片段,它将 MySQL 时间戳转换为 PHP 中的格式化日期:
My goal is to convert a timestamp from MySQL into a JavaScript Date object in an efficient manner. Here is my current snippet that converts the MySQL timestamp into a formatted date in PHP:
<?php
// formats timestamp into following format: 2009, 7, 30
$date = date("Y, n, j", strtotime($row["date"]));
?>
然后我将这个 $date
值用于使用需要 JavaScript Date 对象的 Google 图表 API 的图表:
I am then using this $date
value for a chart using Google's charting API which requires JavaScript Date object:
data.setValue(<?=$count;?>, 0, new Date(<?=$date;?>));
问题在于 JavaScript Date 对象以 0 开始月份索引,因此输出总是提前 1 个月.处理这个问题最有效的方法是什么?
The problem is that the JavaScript Date object begins the month index with 0 so the output is always 1 month in advance. What is the most efficient way in dealing with this issue?
提前致谢!
推荐答案
您可以为 Date 构造函数提供一个 mm/dd/yyyy 或 yyyy/mm/dd 格式的日期,它会转换它:
You can feed the Date constructor a date in mm/dd/yyyy or yyyy/mm/dd format and it will convert it:
>>> new Date('7/30/2009');
Thu Jul 30 2009 00:00:00 GMT-0700 (Pacific Daylight Time)
>>> new Date('2009/7/30');
Thu Jul 30 2009 00:00:00 GMT-0700 (Pacific Daylight Time)
这篇关于JavaScript Date 对象的月份索引从 0 开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JavaScript Date 对象的月份索引从 0 开始


- 如何定位 php.ini 文件 (xampp) 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- Laravel 仓库 2022-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01