• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Trying to format a date with VBA

PipBoy808

Member
Hi,

I'm having an issue with formatting a date:

Code:
Dim MyDate as Date
MyDate = Format(Left(Now,10), "dd.mm.yy")

Weirdly, this is returning MyDate as "03:06:14" rather than "03.06.14". Can anyone tell me why?

Thanks!
 
Format is outputting a string, but you have MyDate defined as a Date, so it's getting converted back. Also, there's no need to call Now if you only want the Date.
Code:
Dim MyDate as String
MyDate = Format(Date,"dd.mm.yy")
 
Hi PipBoy808!
Below will also work.. as Now can also works as Date.
Code:
    Dim MyDate As Date
    MyDate = Now
    MsgBox Format(MyDate, "dd.mm.yy")
 
Back
Top