Hi!
I have a textbox where users enter a time, for example 12:30. I want to get the number of milliseconds that time represents. Any suggestions on how to do this?
| SitePoint Sponsor |
Hi!
I have a textbox where users enter a time, for example 12:30. I want to get the number of milliseconds that time represents. Any suggestions on how to do this?
Try this:
Code:<html> <head> <script type='text/javascript'> function timeconv() { var t_val = tform.ttext.value; var t_arr = t_val.split(':'); for(var i = 0; i < t_arr.length; i++) { t_arr[i] = parseInt(t_arr[i]); } var t_hours = t_arr[0]*60*1000; var t_minits = t_arr[0]*1000; var t_answer = t_hours+t_minits; alert(t_answer); } </script> </head> <body> <form name='tform'> <input type='text' name='ttext'> <input type='button' value='Convert' onclick='timeconv()'> </form> </body> </html>




You need more data, vidar-
Is the time this day's time, or next tuesday?
if it is today, is it 24 hour time, or do you need to adjust for am/pm?
All these details can trip up date calculations.
But to keep it simple, say it is today and 24 hour time, and the input value (V) is '12:30'
returns integer milliseconds since midnight, January 1,1970 GMTCode:function gettimestamp(V){ var H= V.split(/\D+/); V=new Date(); V.setHours(H[0],H[1]); return V.getTime(); }
Bookmarks