Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4074
  • Last Modified:

Date/Time validation in VBScript

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
natrose78
Asked:
natrose78
  • 3
  • 3
  • 3
  • +1
1 Solution
 
natrose78Author Commented:
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
 
MaxOvrdrv2Commented:
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
 
jecahillCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jecahillCommented:
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
 
whammyCommented:
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
 
whammyCommented:
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
 
natrose78Author Commented:
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
 
jecahillCommented:
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
 
MaxOvrdrv2Commented:
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
 
natrose78Author Commented:
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
 
MaxOvrdrv2Commented:
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!

  • 3
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now