Thanks:  0
Likes:  0
Dislikes:  0

# Thread: Regular expressions for Date

1. ## Re: Regular expressions for Date

Hi,
Try this and see if it helps. Found this on another forum:

' 1 for mm/dd/yyyy, m/dd/yyyy, mm/d/yyyy or m/d/yyyy to yyyy-mm-dd
'
' 2 for mm/dd/yy, m/dd/yy, mm/d/yy or m/d/yy to yyyy-mm-dd
'
' 3 for yyyy-mm-dd to mm/dd/yyyy
'
' 4 for yyyy-mm-dd to mm/dd/yy

Public Function ReformatDate(inDate, format)

Select Case format
Case 1
Select Case Len(inDate)
Case 8
ReformatDate = Mid(inDate, 5, 4) & "-0" & Mid(inDate, 1, 1) & "-0" & Mid(inDate, 3, 1)
Case 9
If InStr(inDate, "/") = 2 Then
ReformatDate = Mid(inDate, 6, 4) & "-0" & Mid(inDate, 1, 1) & "-" & Mid(inDate, 3, 2)
Else
ReformatDate = Mid(inDate, 6, 4) & "-" & Mid(inDate, 1, 2) & "-0" & Mid(inDate, 4, 1)
End If
Case 10
ReformatDate = Mid(inDate, 7, 4) & "-" & Mid(inDate, 1, 2) & "-" & Mid(inDate, 4, 2)
End Select
Case 2
Select Case Len(inDate)
Case 6
ReformatDate = "20" & Mid(inDate, 5, 2) & "-0" & Mid(indate, 1, 1) & "-0" & Mid(inDate,3 ,1)
Case 7
If InStr(inDate, "/") = 2 Then
ReformatDate = "20" & Mid(inDate, 6, 2) & "-0" & Mid(inDate, 1, 1) & "-" & Mid(inDate, 3, 2)
Else
ReformatDate = "20" & Mid(inDate, 6, 2) & "-" & Mid(inDate, 1, 2) & "-0" & Mid(inDate, 4, 1)
End If
Case 8
ReformatDate = "20" & Mid(inDate, 7, 2) & "-" & Mid(inDate, 1, 2) & "-" & Mid(inDate, 4, 2)
End Select
Case 3
ReformatDate = mid(inDate, 6, 2) & "/" & Mid(inDate, 9, 2) & "/" & mid(inDate, 1, 4)
Case 4
ReformatDate = mid(inDate, 6, 2) & "/" & Mid(inDate, 9, 2) & "/" & mid(inDate, 3, 2)
End Select

End Function

2. ## Regular expressions for Date

I try to check whether a date has a valid format of MM/DD/200Y, and I followed EXACTLY the instruction in Lesson 7 (Using Regular Expressions) of the QTP Tutorial.

I entered [0-1][0-9]/[0-3][0-9]/200[0-9] in the Value box and did as direction, but the corresponding script showed

Browser("Welcome to MSN.com").Page("United States").Check CheckPoint("[0-1][0-9]/[0-3][0-9_4")

You can image that I ran into error on that line. Even though I tried several times, and/or manually change the script as … CheckPoint("[0-1][0-9]/[0-3][0-9]/200[0-9]"). The error remains.

Any thoughts? Thanks!

3. ## Re: Regular expressions for Date

Maxj,

Great thanks for the ReformatDate. It suits my needs for now.

But, not for my curiosity [img]images/icons/smile.gif[/img] ... part of my original question remains:

Why does QTP not generate the correct script even though I followed EXACTLY the instruction in Lesson 7 of the QTP Tutorial? Maybe I shall post this question to Mercurry Tech Support.

Anyway, great thanks again.

--Chin

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Search Engine Optimisation provided by DragonByte SEO v2.0.40 (Pro) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.