?
Solved

Date/Time validation in VBScript

Posted on 2003-02-21
11
Medium Priority
?
3,827 Views
Last Modified: 2012-05-05
I have a simple html form that allows a user to enter a time.  I need to do two things:

1.  verify that they have entered the time in the format like 11:30 am

2. Redisplay the time in the format like 11:30 am  (Right now the database converts to the format 11:30:00 am)

I am a novice ASP person, so the simpler, the better!

Thanks!
0
Comment
Question by:natrose78
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
11 Comments
 

Author Comment

by:natrose78
ID: 7995501
P.S. These 2 things will be done on two totally seperate pages.  They will enter the time, and I'll put it in the database.  Then, later, I will pull it back out of the database and display it on an HTML page.

Thanks
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 7995563
ok.. well... for making sure that the time is correct:

Function IsTime(strTime)

Dim dattest

On Error Resume Next
dattest = CDate(strTime)
If Err.Number <> 0 Then
IsTime = False
End If
If dattest >= 1 Then
IsTime = False
End If

IsTime = True

End Function

so you would go something like

if IsTime(TextFieldValue) then
   do something if time is valid
else
   give error message
end if


and as for displaying the time as you want it, you must save the database time in a string variable (CStr), then we simply get the first 5 characters from the left of the string (11:30) and then we get the last 2 characters from the string (am, pm). We must them put it all together to create the date to display:

TheDateFromDatabase=RS("TheDate")
TheDateFromDatabase=CStr(TheDateFromDatabase)

TheTime=Left(TheDateFromDatabase,5)
TheAmPm=Right(TheDateFromDatabase,2)

TheDateToDisplay=TheTime & " " & TheAmPm

and you display it wherever you'd like...

pretty simple huh... well.. not THAT simple but still... no biggie...

Enjoy!

MaxOvrdrv2
0
 

Expert Comment

by:jecahill
ID: 7995620
1.  Use the 'isDate' function to validate the input.
2.  There is no date format for your desired display, so you can:

A) Retrieve the date from the db and parse the segments  using datpart for example hour=cstr(datepart("h",somedatetime)) - yields the hour value of the var. some date time.
minutes=cstr(datepart("m",somedatetime)) - yields the mintues value

B) Reassemble the parts of the date/time you would like for display.

Good luck.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:jecahill
ID: 7995630
1.  Use the 'isDate' function to validate the input.
2.  There is no date format for your desired display, so you can:

A) Retrieve the date from the db and parse the segments  using datpart for example hour=cstr(datepart("h",somedatetime)) - yields the hour value of the var. some date time.
minutes=cstr(datepart("m",somedatetime)) - yields the mintues value

B) Reassemble the parts of the date/time you would like for display.

Good luck.
0
 
LVL 4

Expert Comment

by:whammy
ID: 7996488
jecahill is correct about displaying the info from that database, there are quite a few ways to accomplish that, and using datepart() as he described above is perfectly fine.

If you want to validate that they entered EXACTLY two digits, followed by a colon, followed by EXACTLY two digits before this information is entered into a database, you will need to use a regular expression:

^\d{2}\:\d{2}\s(AM|PM)$

0
 
LVL 4

Expert Comment

by:whammy
ID: 7996496
P.S. You would be better off though forcing your users to select from a dropdown box the minutes, seconds, and whether AM or PM. That would solve the whole problem with the data entry in the first place. :)
0
 

Author Comment

by:natrose78
ID: 8059422
Okay, still working on this problem.  I have changed my input into dropdown boxes, so I have taken care of the input problem.  However, I need to be able to order the times using the Order By statement in SQL when I output them.  When times are outputted as strings they display correctly, but do not order correctly.  They do not pay attention to the AM/PM so 1:00PM is considered before 2:00AM  when really 1pm comes after 2am.  When I change the type back to a date/time, they order correctly but go back to displaying as 1:00:00 PM  Any suggestions there?

Thanks in advance
0
 

Expert Comment

by:jecahill
ID: 8059465
When you insert your date/time value in to the db, are you inserting as string?  If you insert as date/time, order by should operate as expected.  If you are inserting as string, suggest using cdate to convert string to date/time.  Since you are validating the entry with your drop-down, all values should be valid dates and sort just fine.

Cheers.
0
 
LVL 10

Accepted Solution

by:
MaxOvrdrv2 earned 200 total points
ID: 8059568
well... as i said before... you should simply cut out the last 2  0's... now... what you do is this... you leave them as date/time format... but WHEN YOU DISPLAY THEM IN THEIR ORDERED FASHION, you then convert them into strings and format them properly... something like this:

RS.Open="SELECT DateFieldName FROM TableName ORDER BY DateFieldName"

WHILE NOT RS.EOF

      --All Your Other Code That Goes BEFORE Displaying The Date(s)--      

      TheDateFromDatabase=RS("TheDate")
      TheDateFromDatabase=CStr(TheDateFromDatabase)

      TheTime=Left(TheDateFromDatabase,5)
      TheAmPm=Right(TheDateFromDatabase,2)

      TheDateToDisplay=TheTime & " " & TheAmPm
     
      Response.Write TheDateToDisplay

      --All Your Other COde That Goes AFTER Displaying The Date(s)--

RS.MoveNext
WEND

and that's all you have to do... then... if you asre taking those string values to store in the database... you simply convert them back into Date/Time formats using CDate and the PC will do the rest... in the previous example, i have tested a range of times from 12:00 am to 12:00 p.m. and here is the order i got back from that same function (mind you the names of the fields in the database should be your own and all that but... pretty much the same!):

12:00 AM
1:00 AM
2:00 AM
3:00 AM
4:00 AM
5:00 AM
6:00 AM
7:00 AM
8:00 AM
9:00 AM
10:00 AM
11:00 AM
12:00 PM


is this not what you wanted?????

MaxOvrdrv2
0
 

Author Comment

by:natrose78
ID: 8064270
Sorry, Maxovrdrv2, when you wused the cstr, I thought the dates had to be a string and they are a date, but apparently I was wrong. So yes this is what I wanted, comment accepted.
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 8064293
ah... now i see the confusion... but that's what CStr is for... it's to take something that is not a string to begin with and turn it into one on the server... whilest leaving the original intact... :)

Thanks for the points...

MaxOvrdrv2
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question