Retrieving date from mysql to javascript gives invalid date

This is the code and the mysql table

Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/2a", "root", "root");
Statement st=con.createStatement();
ResultSet rsta=st.executeQuery("Select * from data");
while(rsta.next()) {
  x14=rsta.getTime("strtime");
}
%>
<div id="TextBoxesGroup">
<input type="hidden" id="time" name="time" value="<%=x14%>"/>
</div>
<script>

$(document).ready(function(){     
    var dateString=$('#time').val();
    var d1 = new Date(dateString);


    var newTextBoxDiv = $(document.createElement('div'))
         .attr("id", 'TextBoxDiv' + counter);
    newTextBoxDiv.after().html('<div><label style="float:left;">'+d1+'</label>);
    newTextBoxDiv.appendTo("#TextBoxesGroup");  
</script>


+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| date    | date        | YES  |     | NULL    |                |
| strtime | time        | YES  |     | NULL    |                |
| endtime | time        | YES  |     | NULL    |                |
| freq    | varchar(30) | YES  |     | NULL    |                |
| inter   | int(11)     | YES  |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+

The variable d1(it is saved as time in db) gives me invalid date.why is this happening?Am I missing something?

Let me know incase more info is required but do help.

Upvotes: 0

Views: 475

Answers (3)

I think type Time in Mysql has format HH:mm:ss So you can not parse it as date use javascript. You need to combine date and strtime when get data from mysql

 x14= rsta.getDate("date") + " " +  rsta.getTime("strtime");

Upvotes: 0

Vishal
Vishal

Reputation: 1209

Here is a helper function to get proper date object from your custom time format

var helper = function(time) {
    var regex = /^\s*((0?[0-9])|(1?[0-9])|(2?[0-3]))\s*\.\s*[0-5]?[0-9]\s*:\s*[0-5]?[0-9]\s*$/g;
    time = time.match(regex);

    if(time && time.length === 1) {
        time = time[0];
        var date = new Date();
        var endIndex = time.indexOf('.');
        var hr = Number(time.substr(0, endIndex).trim());
        time = time.substr(endIndex + 1).trim();
        endIndex = time.indexOf(':');             
        var min = Number(time.substr(0, endIndex).trim());
        time = time.substr(endIndex + 1).trim();                
        var sec = Number(time);
        date.setHours(hr);
        date.setMinutes(min);
        date.setSeconds(0);
        return date;
    }

    return null;
};

This will accept the time in HH.MM:SS in 24 hour format and return the date object initialized to current date.

Upvotes: 0

There are four ways of initiating a date:

new Date() // current date and time
new Date(milliseconds) //milliseconds since 1970/01/01
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)

Most parameters above are optional. Not specifying, causes 0 to be passed in.

Once a Date object is created, a number of methods allow you to operate on it. Most methods allow you to get and set the year, month, day, hour, minute, second, and milliseconds of the object, using either local time or UTC (universal, or GMT) time.

All dates are calculated in milliseconds from 01 January, 1970 00:00:00 Universal Time (UTC) with a day containing 86,400,000 milliseconds.

Some examples of initiating a date:

var today = new Date()
var d1 = new Date("October 13, 1975 11:13:00")
var d2 = new Date(1979,5,24)
var d3 = new Date(1979,5,24,11,33,0)

Its date object so u should need dateString not timeString.

Upvotes: 1

Related Questions