Tuesday, 19 February 2013

ASP: CDate


CDate is an ASP function that converts a valid date and time string to a Date Type value and returns it.

Because of the above we need to use another ASP function in order to be sure to convert a valid date. The function's IsDate.

In the following short post, we are going to see how to use both functions and create a short script that converts a date.

First of all, let's talk about the IsDate function. Because it uses the local settings to determine if a date is really a date, we should be careful when using it. For example, if we think about a date containing "April" as month, we should remember that the fourth month is not called "April" in all languages.
Because of that, remember to verify the language setting of your system.

The CDate function has a simple syntax:
where Date is a valid date expression. We can use functions like Date() or Now() as well.

Now, let's put the two aforementioned functions all together.
Dim myDate, myDateType
myDate = "April 18, 1920"
If IsDate(myDate) Then
     myDateType = CDate(myDate)
     Response.Write("Bad date formatting!")
End If
which will output:
depending on the date and time setting of the hosting system.

0 thoughts:

Post a Comment

Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.