Form entry to Time part of database Date entry?

N

Noozer

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!!!
 
R

RobG

Noozer said:
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. ???

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

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?

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 );
">
Finally, if the entered time is earlier than the current time, how can I
create the above date/time combination using tomorrows date?

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!
 
D

Dr John Stockton

JRS: In article <w9lHe.80648$5V4.77990@pd7tw3no>, dated Mon, 1 Aug 2005
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. ???
Yes.

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?

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().
Finally, if the entered time is earlier than the current time, how can I
create the above date/time combination using tomorrows date?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top