SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Norway
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Parse timestring into milliseconds

    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?

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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>

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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'

    Code:
    function gettimestamp(V){	
    	var H= V.split(/\D+/);
    	V=new Date();
    	V.setHours(H[0],H[1]);
    	return V.getTime();
    }
    returns integer milliseconds since midnight, January 1,1970 GMT


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •