Link to home
Start Free TrialLog in
Avatar of majala
majala

asked on

using Format with dates

I wanna change date format in variable.

Example:
Date is in format "24.7.1998"
and I want it to be in format "24/7/1998"

I have tryed this but when I look at the NewDate after Format-clause like this "MsgBox NewDate" it has't change at all?

NewDate = Format(Date, "dd/mm/yyyy")
Avatar of cymbolic
cymbolic

If NewDate is a date type variable, it will display in your default date format.  As a string variable, it should be displaying as you have formatted it.
if newdate is a date,then you are not going to change it... do this

msgbox format$(newdate, "dd/mm/yyyy")
Avatar of majala

ASKER

I did this:
Dim NextDay As String

NextDay = Day  'Day is in dd/mm/yyyy format and so is NextDay after this
   
NextDay = DateAdd("d", 1, NextDay) 'NextDay is in dd.mm.yyyy format

NextDay = format(NextDay, "dd/mm/yyyy") 'NextDay is still in dd.mm.yyyy format!

Why this isn't working? That $ in anthonyc's answer didn't help either.
by the way.DAY returns an integer..  

?day(now)
 4

?dateadd("d",1, day(now))
1/4/1900


I'm SURE this is not what you want.....

if I run this:
msgbox format$(dateadd("d", 1, now), "ddmmyyyy")

I get this in the box
05081998

so this works!

Avatar of majala

ASKER

That's correct. I did this
MsgBox Format$(DateAdd("d", 1, Now), "DDMMYYYY")
and I got this in box 05081998.

But when I did this
MsgBox Format$(DateAdd("d", 1, Now), "DD/MM/YYYY")
I got this in box 05.08.1998 and I want it to be 05/08/1998.
Why?
ASKER CERTIFIED SOLUTION
Avatar of clifABB
clifABB

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
You could replace the "." for "/"


Option Explicit

Private Sub Form_Load()
    Dim dte As Date
    Dim sNewDate As String
   
    dte = "24.7.1998"
    sNewDate = sRepl(Format$(dte, "dd/mm/yyyy"), ".", "/")
    MsgBox sNewDate
End Sub

Private Function sRepl(ByVal sInp As String, _
                       ByVal sChar As String, _
                       ByVal sRChar As String) As String
    Dim p As Integer
   
    p = InStr(sInp, sChar)
    While p > 0
        Mid$(sInp, p) = sRChar
        p = InStr(sInp, sChar)
    Wend
    sRepl = sInp
End Function

Avatar of majala

ASKER

Thanks!