Link to home
Start Free TrialLog in
Avatar of WinPE
WinPE

asked on

How can I convert this date format YYYYMMDD into this date format MM/DD/YY using vbscript.

How can I convert this date format YYYYMMDD into this date format MM/DD/YY using vbscript.


This is comnig from a registry key. Need to put this into a function.


Thanks a bunch
Avatar of sr75
sr75
Flag of United States of America image

Here is a script that breaks it as I couldn't get the FormatDate function to work.  
strDate = "19991230"
strYear = left(strDate, 4)
strMonth = left(right(strDate, 4), 2)
strDay = right(strDate, 2)
 
strDate = strMonth & "/" & strDay & "/" & strYear
 
wscript.echo strDate

Open in new window

Avatar of almilyo
almilyo

Try

newlyformattedstring = Mid(mystring, 5, 2) & "/" & Mid(mystring, 7, 2) & "/" & Mid(mystring, 1, 4)
Avatar of WinPE

ASKER

genius, that works...

However I get the proper date 2/22/08 but for values are null I get a //

How can I script a scrub of some sort to remove those // entries and just leave them blank, without wiping the entries that have a valid date.
Taking sr71's much less lazy suggestion...
strDate = "19991230"
strYear = left(strDate, 4)
strMonth = left(right(strDate, 4), 2)
strDay = right(strDate, 2)
 
if isNull(strDate) or strDate="" then 
  strDate = strMonth & "/" & strDay & "/" & strYear
else
  strDate = ""
 
wscript.echo strDate

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of almilyo
almilyo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Glad you got it working...

If you splitting  the points when two people contribute to the answer it's the fairest way to work things out, especially as sr75 took the time to write a much more readable script than I did.
It really is no big deal.  I am glad that he got what he needed.  Besides, I forgot how to use Mid() and you reminded me how.
Avatar of WinPE

ASKER

Thanks to both of you, I put in the request to split the points... Both were very helpful!