Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Date from a line string

Status
Not open for further replies.

jessiejane

Programmer
Oct 29, 2009
32
US
I have a string "John-2/3/2010". How would I get just the date from the string.

Given: John-2/3/2010
Output: 2/3/2010
 
Given = "John-2/3/2010"
Output = Mid(Given, 1+InStr(Given,"-"))

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Doesn't work
Any chance you may have more descriptive reply to help us to help you ?
 
The string is in the text file
----------------------------
John-03/12/2010
Mary-05/01/2010

----------------------------

I have used this code:

Do Until objPurge.AtEndOfStream
Redim Preserve arrPurge(intPurgeCounter)
arrPurge(intPurgeCounter) = objPurgeFile.ReadLine
Dim intx
intx = len(arrPurge(intPurgeCounter))
strDateValue = Mid(arrPurge(intPurgeCounter),(1+(instr(arrPurge(intPurgeCounter), "-"))))
MsgBox "StrDateValue" & strDateValue
Loop
 
little unfair to say PHV's suggestion didnt work given the context.
are you incrementing intPurgeCounter?
 

jessiejane,

I notice that over the past 8 months, you have posted twelve threads and have received many good tips related to your stated needs. Yet, you have responded NOT ONCE, to
[blue]
Thank Tek-Tip Contributor
for this valuable post!
[/blue].
The [purple]little purple Stars[/purple] have several purposes.

First, they [purple]give positive feedback to Tek-Tip Contributors[/purple], that their posts have been valuable.

Second, they [purple]identify threads as containing valuable posts[/purple], so that other members can share in the benefit.

And third, they identify the original poster (that's YOU, BTW), as a grateful member, that not only has received, but is also willing to [purple]give visible tokens of thanks[/purple].


Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Another problem:
Code:
Do Until [b][COLOR=blue]objPurge[/color][/b].AtEndOfStream
  Redim Preserve arrPurge(intPurgeCounter)    
  arrPurge(intPurgeCounter) = [b][COLOR=blue]objPurgeFile[/color][/b].ReadLine

 
IMHO: The best method for parsing dates in several formats is using regular expressions - see this example:
regex.vbs
Code:
[COLOR=#0000ff]'# Example for using regular expressions in VBscript[/color]

string01 [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"John-2/3/2010"[/color]
wscript[COLOR=#804040][b].[/b][/color]echo string01 [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]" -> "[/color] [COLOR=#804040][b]&[/b][/color] extract_date[COLOR=#804040][b]([/b][/color]string01[COLOR=#804040][b])[/b][/color]
string02 [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"Mary-05/01/2010"[/color]
wscript[COLOR=#804040][b].[/b][/color]echo string02 [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]" -> "[/color] [COLOR=#804040][b]&[/b][/color] extract_date[COLOR=#804040][b]([/b][/color]string02[COLOR=#804040][b])[/b][/color]
string03 [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"abc xyz 29/6/2010"[/color]
wscript[COLOR=#804040][b].[/b][/color]echo string03 [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]" -> "[/color] [COLOR=#804040][b]&[/b][/color] extract_date[COLOR=#804040][b]([/b][/color]string01[COLOR=#804040][b])[/b][/color]

string01 [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"John-2.3.2010"[/color]
wscript[COLOR=#804040][b].[/b][/color]echo string01 [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]" -> "[/color] [COLOR=#804040][b]&[/b][/color] extract_date[COLOR=#804040][b]([/b][/color]string01[COLOR=#804040][b])[/b][/color]
string02 [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"Mary-05.01.2010"[/color]
wscript[COLOR=#804040][b].[/b][/color]echo string02 [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]" -> "[/color] [COLOR=#804040][b]&[/b][/color] extract_date[COLOR=#804040][b]([/b][/color]string02[COLOR=#804040][b])[/b][/color]
string03 [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"abc   29.6.2010"[/color]
wscript[COLOR=#804040][b].[/b][/color]echo string03 [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]" -> "[/color] [COLOR=#804040][b]&[/b][/color] extract_date[COLOR=#804040][b]([/b][/color]string01[COLOR=#804040][b])[/b][/color]

string01 [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"John-2010-03-02"[/color]
wscript[COLOR=#804040][b].[/b][/color]echo string01 [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]" -> "[/color] [COLOR=#804040][b]&[/b][/color] extract_date[COLOR=#804040][b]([/b][/color]string01[COLOR=#804040][b])[/b][/color]

string_nodate [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"abc 2010-x6-29"[/color]
wscript[COLOR=#804040][b].[/b][/color]echo string_nodate [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]" -> "[/color] [COLOR=#804040][b]&[/b][/color] extract_date[COLOR=#804040][b]([/b][/color]string_nodate[COLOR=#804040][b])[/b][/color]

[COLOR=#804040][b]function[/b][/color] extract_date[COLOR=#804040][b]([/b][/color]search_string[COLOR=#804040][b])[/b][/color]
 [COLOR=#0000ff] '# extract date from string[/color]
 [COLOR=#0000ff] ' create instance of RegExp [/color]
  [COLOR=#804040][b]set[/b][/color] re [COLOR=#804040][b]=[/b][/color] [COLOR=#804040][b]new[/b][/color] regexp
 [COLOR=#0000ff] 'or use[/color]
 [COLOR=#0000ff] 'set re = createobject("vbscript.regexp")[/color]


 [COLOR=#0000ff] ' regexp for matching several dates formats alternatively:[/color]
 [COLOR=#0000ff] ' dd/mm/yyyy, dd.mm.yyyy, dd-mm-yyyy, yyyy-mm-dd[/color]
  re[COLOR=#804040][b].[/b][/color]pattern [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"(\d{1,2}\/\d{1,2}\/\d{4})|"[/color] [COLOR=#804040][b]& _[/b][/color]
               [COLOR=#ff00ff]"(\d{1,2}\.\d{1,2}\.\d{4})|"[/color] [COLOR=#804040][b]&[/b][/color] _ 
               [COLOR=#ff00ff]"(\d{1,2}\-\d{1,2}\-\d{4})|"[/color] [COLOR=#804040][b]&[/b][/color] _ 
               [COLOR=#ff00ff]"(\d{4})\-\d{1,2}\-\d{1,2}"[/color]
 [COLOR=#0000ff] ' search for pattern[/color]
  [COLOR=#804040][b]set[/b][/color] re_result [COLOR=#804040][b]=[/b][/color] re[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]execute[/color][COLOR=#804040][b]([/b][/color]search_string[COLOR=#804040][b])[/b][/color]

 [COLOR=#0000ff] ' return first element of the result collection[/color]
  [COLOR=#804040][b]if[/b][/color] re[COLOR=#804040][b].[/b][/color]test[COLOR=#804040][b]([/b][/color]search_string[COLOR=#804040][b])[/b][/color] [COLOR=#804040][b]then[/b][/color]
    extract_date [COLOR=#804040][b]=[/b][/color] re_result[COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]0[/color][COLOR=#804040][b]).[/b][/color][COLOR=#a020f0]value[/color]
  [COLOR=#804040][b]else[/b][/color]
    extract_date [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"date not found!"[/color]
  [COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]if[/b][/color]
[COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]function[/b][/color]
Output:
Code:
C:\Users\Roman\Work\VBscript>cscript /Nologo regex.vbs
John-2/3/2010 -> 2/3/2010
Mary-05/01/2010 -> 05/01/2010
abc xyz 29/6/2010 -> 2/3/2010
John-2.3.2010 -> 2.3.2010
Mary-05.01.2010 -> 05.01.2010
abc   29.6.2010 -> 2.3.2010
John-2010-03-02 -> 2010-03-02
abc 2010-x6-29 -> date not found!
 
Oops - I found a typo in my code above:
Change this
Code:
wscript.echo string03 & " -> " & extract_date([COLOR=red]string01[/color])
to this
Code:
wscript.echo string03 & " -> " & extract_date([COLOR=green]string03[/color])
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top