Connecting Tech Pros Worldwide Forums | Help | Site Map

Form entry to Time part of database Date entry?

Noozer
Guest
 
Posts: n/a
#1: Aug 1 '05
I have a textbox on one of my forms that is used to accept a time from the
user. I need to write this value to a database to trigger an event later on.

What I'd like to know is...

Are there any functions in javascript that can convert different time
formats to a known format (24 hour clock)... 5pm -> 17:00 or 5:00 to 05:00,
etc. ???

Assuming that I get the input into a 24 hour format, how can I create a full
time/date variable with todays date and the users entered time?

Finally, if the entered time is earlier than the current time, how can I
create the above date/time combination using tomorrows date?

Thanks!!!



RobG
Guest
 
Posts: n/a
#2: Aug 1 '05

re: Form entry to Time part of database Date entry?


Noozer wrote:[color=blue]
> I have a textbox on one of my forms that is used to accept a time from the
> user. I need to write this value to a database to trigger an event later on.
>
> What I'd like to know is...
>
> Are there any functions in javascript that can convert different time
> formats to a known format (24 hour clock)... 5pm -> 17:00 or 5:00 to 05:00,
> etc. ???[/color]

You need to parse whatever is input and feed it to your own date
functions, probably all you need to know is here:

<URL:http://www.merlyn.demon.co.uk/js-dates.htm>
[color=blue]
>
> Assuming that I get the input into a 24 hour format, how can I create a full
> time/date variable with todays date and the users entered time?[/color]

A date object representing what the user's machine thinks is the
current local time can be created with:

var now = new Date();

Adjusting the the time is shown below in a short example:

<input type="text" name="aTime" onblur="
var theTime = new Date();
var t = this.value.split(':');
theTime.setHours( t[0] );
theTime.setMinutes( t[1] );
theTime.setSeconds( 0 );
alert( theTime );
var now = new Date();
if ( now > theTime ) {
theTime.setDate( theTime.getDate() + 1 );
}
alert ( theTime );
">
[color=blue]
>
> Finally, if the entered time is earlier than the current time, how can I
> create the above date/time combination using tomorrows date?[/color]

Yes, see above. But there are three big caveats:

1. You must validate whatever the user enters to ensure it is
something your functions will like, which means checking they entered
properly formatted numbers and that they are within an acceptable
range. For example, if you enter say 37:102 to the above example, it
will quite happily interpret it as 14:42 tomorrow.

2. The clock on the users machine may be wrong, so that 'now' is not
today, or it may be sufficiently inaccurate as to cause spurious
results for certain times.

3. The user may not have javascript enabled/available.

Over to you!

--
Rob
Dr John Stockton
Guest
 
Posts: n/a
#3: Aug 1 '05

re: Form entry to Time part of database Date entry?


JRS: In article <w9lHe.80648$5V4.77990@pd7tw3no>, dated Mon, 1 Aug 2005
08:35:40, seen in news:comp.lang.javascript, Noozer <dont.spam@me.here>
posted :
[color=blue]
>Are there any functions in javascript that can convert different time
>formats to a known format (24 hour clock)... 5pm -> 17:00 or 5:00 to 05:00,
>etc. ???[/color]

Yes.
[color=blue]
>Assuming that I get the input into a 24 hour format, how can I create a full
>time/date variable with todays date and the users entered time?[/color]

with (D = new Date()) setHours(H, M, S)
or with (D = new Date()) setUTCHours(H, M, S)

where H M S are numbers or strings for hours minutes seconds.

N.B. VERY ancient browsers may not allow multiple parameters in
setHours().
[color=blue]
>Finally, if the entered time is earlier than the current time, how can I
>create the above date/time combination using tomorrows date?[/color]

Add
if (D < new Date()) D.setDate(D.getDate()+1)
or if (D < new Date()) D.setUTCDate(D.getUTCDate()+1)

If there is any possibility of the actual time, or of H M S, being
affected to Summer Time clock changes in Spring and Autumn, then test
carefully that what happens is satisfactory.

If efficiency matters, which is unlikely, do parameterless new Date()
only once. That's also vital if the user may be working at local (or
UTC) midnight.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Closed Thread


Similar JavaScript / Ajax / DHTML bytes