如何mysql日期到var obj [新日期?

时间:2014-03-18 12:21:19

标签: javascript php mysql datetime google-visualization

我正在处理一个项目,我遇到日期格式问题。我不明白如何将日期从mysql(phpmyadmin)格式化为这种格式,这是在phpmyadmin或date中的日期时间吗?

Jsbin:http://jsbin.com/yaqew/1/edit

[new Date(1387717021701),   1],
[new Date(1387719023801),   2],
[new Date(1387721025901),   4],
[new Date(1387723027001),   8],

代码:

<!DOCTYPE html>
<html>
<head>
<style>
    #chart_div {
        width: 50%;
        height: 100%;
    }   
</style>

<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">

    google.load("visualization", "1", {packages:["corechart"]});
    google.setOnLoadCallback(drawChart);

    function drawChart() {

        var obj = [
            [new Date(1387717021701),   1],
            [new Date(1387719023801),   2],
            [new Date(1387721025901),   4],
            [new Date(1387723027001),   8],
        ];

        var data = new google.visualization.DataTable();
        data.addColumn('datetime', 'Date');
        data.addColumn('number', 'Buy');
        data.addRows(obj)


        var options = {
            curveType: "function",
            width: 500, 
            height: 400,
            vAxis: {maxValue: 10}, 
            title: 'test',
            hAxis: {
                format: "HH:mm"
                //format: "HH:mm:ss"
                //format:'MMM d, y'
            },
            explorer: { 
                actions: ['dragToZoom', 'rightClickToReset'], 
                axis: 'vertical'
            }
        };

        var chart = new google.visualization.LineChart(
                            document.getElementById('chart_div'));
        chart.draw(data, options);
    }
</script>
</head>

<body>
    <!--Div that will hold the pie chart-->
    <div id="chart_div"></div>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

使用CBroe的建议:

SQL有一个时间戳字段,或者您可以使用int(11)来存储该单元。

如果您不喜欢时间戳,也可以在mysql内部发送之前通过执行转换它 来自变量的.toUTCString()来获取此处所解释的内容http://www.w3schools.com/jsref/jsref_toutcstring.asp

答案 1 :(得分:0)

Javascript还可以选择构建这样的Date对象:

new Date(year, month, day, hours, minutes, seconds, milliseconds)

year为4位数年份,month为零索引(因此1月为0,而非1),day为这个月的哪一天。 month之后的所有参数都是可选的。如果停用,默认设置为day = 1hoursminutessecondsmilliseconds0。任何超出预期范围的参数都将溢出到适当的程度。例子:

new Date(2014, 0) // January 1, 2014 00:00:00:000
new Date(2014, 1, 6) // February 6, 2014 00:00:00:000
new Date(2014, 4, 12, 0, 14) // May 12, 2014 00:14:00:000
new Date(2014, 0, 100) // April 10, 2014 00:00:00:000
new Date(2014, 1, 100) // May 11, 2014 00:00:00:000
new Date(2014, 0, 1000) // September 26, 2016 00:00:00:000
new Date(2014, 0, 0) // December 31, 2013 00:00:00:000
new Date(2014, -1) //  December 1, 2013 00:00:00:000

非整数输入被截断,但是:

new Date(2014, 1, 6.8) // February 6, 2014 00:00:00:000