Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Pedro Gilbert   on Dec 03 In MS Office Category.

  
Question Answered By: Estella Mitchell   on Dec 03

Well, because constructing usable strings from a date is so troublesome,
the good folks at MS did do some other favors for you. Actually, in other
languages, what you're after is normally fulfilled by the use of regular
expressions and replace operations.

For a similar solution, VBA offers the Replace function. Check out this
demo:
'=========================================
Sub GetFullDate()
x = ReplaceChars(Now)
MsgBox x
End Sub
'=========================================
Function ReplaceChars(strString)

strString = Replace(strString, "-", "")
strString = Replace(strString, "/", "")
strString = Replace(strString, ":", "")
strString = Replace(strString, " ", "")

ReplaceChars = strString
End Function
'=========================================

Now, there's still a problem  in that months and days less than 10 cause
the returned string to change, there's that annoying PM or AM on the end
and...it's just not consistent enough. Ick!

So, to deal with this, I like to return a date format constructed in the
YearMonthDaySeconds format and get it so th eresult is always the same
number of characters. I use Seconds to make sure the returned name is
unique instead of checking the file system  and appending characters to
avoid duplicate file names. You can produce that result in a slightly
different way. Refer to the following code:

'=========================================
Sub GetDateString()

strDate = Year(Now) & _
PadZero(Month(Now)) & _
PadZero(Day(Now)) & _
PadZero(Second(Now))

MsgBox strDate

End Sub
'=========================================
Function PadZero(strString)

If Len(strString) < 2 Then
PadZero = "0" & strString
Else
PadZero = strString
End If

End Function
'=========================================

Share: 

 

This Question has 3 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on How to convert 11/5/2004 into11-5-2004 Or get search suggestion and latest updates.


Tagged: