Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

php - JavaScript Date Object's month index begins with 0

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"]));
?>

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;?>));

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?

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...