Home > ASP Development > How to format date/time from input box ?

How to format date/time from input box ?



Actually, i have found the solution to the formatting of date and time i needed but i have to get date from input and then format the date submitted in the form to the desired output.



HTML codes - Interface


Code:



<html>
<head><title>Date Formatting Application</title></head>
<body>
<form name="frmDate" method="post" action="Date.asp">

<h3>Enter a date: </h3><p><input type="text" name="Date">
<p><h3>Select format string (Choices: 1 for mm/dd/yyyy &nbsp &nbsp 2 for dd/mm/yyy &nbsp &nbsp 3 for /dd/mm/yy):</h3>

<p>
<select name="Choice">
<option value="none" selected>
<option value="1">1
<option value="2">2
<option value="3">3
</select>

<p><h3>Click any date format string:<br>
<input type="radio" name="DateFormat" value="0">General Date (mm/dd/yyyy hh:mm:ss AM/PM)</input><br>
<input type="radio" name="DateFormat" value="2">Short Date (dd/mm/yy)</input><br>
<input type="radio" name="DateFormat" value="1">Long Date (day of week, month Name day, year)</input><br>
<input type="submit" value="submit">
</form>
</body>
</html>




Sample interface for the form can be available at here.



Date.asp


Code:



<%
dim todaysDate
todaysDate=now()

Function Choice1(DateValue)
Dim strYYYY
Dim strMM
Dim strDD

strYYYY = CStr(DatePart("yyyy", DateValue))

strMM = CStr(DatePart("m", DateValue))
If Len(strMM) = 1 Then strMM = "0" & strMM

strDD = CStr(DatePart("d", DateValue))
If Len(strDD) = 1 Then strDD = "0" & strDD

Choice1 = strMM & "/" & strDD & "/" & strYYYY

End Function

Function Choice2(DateValue)
Dim strYYYY
Dim strMM
Dim strDD

strYYYY = CStr(DatePart("yyyy", DateValue))

strMM = CStr(DatePart("m", DateValue))
If Len(strMM) = 1 Then strMM = "0" & strMM

strDD = CStr(DatePart("d", DateValue))
If Len(strDD) = 1 Then strDD = "0" & strDD

Choice2 = strDD & "/" & strMM & "/" & strYYYY

End Function

Function Choice3(DateValue)
Dim strYY
Dim strMM
Dim strDD

strYY = CStr(DatePart("yyyy", DateValue))
If Len(strYY) = 4 Then strYY = right(strYY,2)

strMM = CStr(DatePart("m", DateValue))
If Len(strMM) = 1 Then strMM = "0" & strMM

strDD = CStr(DatePart("d", DateValue))
If Len(strDD) = 1 Then strDD = "0" & strDD

Choice3 = strDD & "/" & strMM & "/" & strYY

End Function

Response.write ("<u><Strong>Drop Down Choices</strong></u><br>")

Response.write ("Choice 1 : ")
Response.Write Choice1(todaysDate) & (" [mm/dd/yyyy]<br>")

Response.write ("Choice 2 : ")
Response.Write Choice2(todaysDate) & (" [dd/mm/yyyy]<br>")

Response.write ("Choice 3 : ")
Response.Write Choice3(todaysDate) & (" [dd/mm/yy]<br>")

Response.write ("<br>")

Response.write ("<u><Strong>Radio Box Choices</strong></u><br>")

Response.write ("Format 0 : ")
Response.write FormatDateTime(todaysDate,0) & (" [General Date]<br>")

Response.write ("Format 1 : ")
Response.write FormatDateTime(todaysDate,1) & (" [Long Date]<br>")

Response.write ("Format 2 : ")
Response.write FormatDateTime(todaysDate,2) & (" [Short Date]<br>")
%>




Sample of Date.asp can be viewed at here. For this asp document, when i try for currrent date function Now(), there's no problem showing the formatted date but when i need to change to get from input box, i just having problems.



Partly of the questions

A program code required to response error message text for non-date entry in Date text box as

Error msg: You must enter a date.



Is there any way or how can i set a format input so that visitor enter only date in the text box ? How to do the checking ? If non-date input, it should be able to display error message.



I hope there's someone out there which is good in ASP coding skills able to help me, I'm looking forward to solve this problem. God bless me...Thx for any help in advance.

    
Guest


Is there any way or how can i set a format input so that visitor enter only date in the text box ? How to do the checking ? If non-date input, it should be able to display error message.



Well if you want to force people to select a date correctly then have drop down boxes for the date and month.



Assuming date dd/mm/yyyy



Checks you can run though

Date less than 32

Month less than 13

Also check if character 3 and 6 are "/"

Check characters which should be 0 - 9 are!

If year say less than 1900 respond.write("are you sure") etc...





I'd stick to one type of date though. If you want to do USA system vs say UK then detect where they come from or ask as a question the show as required

Was this answer helpful ? Yes No   
Guest


I think that you are always going to have problems with allowing a date input.

There are so many date formats that a user could enter.



I would suggest using 3 select boxes to hold the values, you can then let them input their date and you can check it using IsDate.



From there you can do your functions to reformat it.

Was this answer helpful ? Yes No   
Guest


URL

take a look at the screenshot above, actually it's required by my assigment questions to answer the question exactly to the screenshots given. Otherwise i would choose to let user to select rather than enter in input box coz input box cause error and hard to do checking or force formatting when typed ? Any more helps or suggestions ? Assigments duedate on Friday...i'm DEAD.

Was this answer helpful ? Yes No   
Guest
 
 
Home - About Infoqu - Contact - Privacy Statement - Link to Infoqu - Bookmark Infoqu

Copyright 2007-2008 by Infoqu. All rights reserved