Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does VBScript have a DateTime.TryParse equivalent?

Given a variant, does VBScript have an equivalent of C#'s DateTime.TryParse method?

like image 421
Jim G. Avatar asked Mar 25 '10 16:03

Jim G.


People also ask

What is DateTime TryParse?

TryParse(String, DateTime) Converts the specified string representation of a date and time to its DateTime equivalent and returns a value that indicates whether the conversion succeeded.


1 Answers

Use IsDate(stringDate) in conjunction with CDate(stringDate).

Use the IsDate() function to determine if date can be converted to a date or time.

CDate() recognizes date literals and time literals as well as some numbers that fall within the range of acceptable dates. When converting a number to a date, the whole number portion is converted to a date. Any fractional part of the number is converted to a time of day, starting at midnight.

CDate recognizes date formats according to the locale setting of your system. The correct order of day, month, and year may not be determined if it is provided in a format other than one of the recognized date settings. In addition, a long date format is not recognized if it also contains the day-of-the-week string.

The following example uses the CDate function to convert a string to a date.

MyDate = "October 19, 1962"   ' Define date.
MyShortDate = CDate(MyDate)   ' Convert to Date data type.
MyTime = "4:35:47 PM"         ' Define time.
MyShortTime = CDate(MyTime)   ' Convert to Date data type.
like image 154
backslash17 Avatar answered Oct 09 '22 03:10

backslash17