Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to store time which is entered from angular js frontend.The time format is
HH: MM AM/PM.

When I console log time I am getting user entered time like

Thu Jan 01 1970 01:01:00 GMT+0530 (India Standard Time) but in my database time value is 00:00:00

What I have tried:

Angular js
<input type="text"  ng-model="name" >

<input type="time" ng-model="booktime">


Controller

JavaScript
$http.post("url", {

name=name;
booktime=booktime; 
 })
    
    .then(function(response){
       console.log("Data Inserted Successfully");
           console.log(booktime);
});

Mysql
booktime coloumn datatype is Time

SQL
$data = json_decode(file_get_contents("php://input"));
<pre>$name = $data->name;

$booktime=$data->booktime; 

$sql = "INSERT INTO cust(name,booktime)VALUES ('$name','$booktime')";
Posted
Updated 18-Feb-17 23:33pm
v3
Comments
Peter Leow 19-Feb-17 1:18am    
what is the datatype for the booktime column? Did you really name that table name as table?
Danuuu 19-Feb-17 2:13am    
Datatype is Time.And Table name is user
Peter Leow 19-Feb-17 2:26am    
user is a reserved word in mysql and it should not be used. However, you can get away with it by wrapping it in back ticks, e.g `user`. I wonder if you encounter any error in running that sql query.
Danuuu 19-Feb-17 2:35am    
Okay i will change table name but I haven't got any errors but time coloumn values are 00:00:00

1 solution

SQL time datatype is not compatible with JavaScript datetimes (which comes as a string) and therefore the implicit conversion doesn't work. Look here for examples how to convert the value: javascript - Convert JS date time to MySQL datetime - Stack Overflow[^]
 
Share this answer
 
Comments
Danuuu 19-Feb-17 6:48am    
Thank you but example is of DateTime and I need to store only TIME how to convert only Time to mysqltime
NightWizzard 19-Feb-17 7:58am    
The samples demonstrate how to access all single parts (incl. hours, minutes and seconds) from the datetime string. Simply extract the ones you're needing.
Danuuu 19-Feb-17 9:07am    
am not getting how to do?And the example is for Current DateTime and i need to store specific time
NightWizzard 19-Feb-17 9:30am    
Time is a part of a datetime that you have to extract. Sorry, but if you are not able to adapt the samples, you shouldn't deal with programming. I won't do the job for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900