Solved

using Format with dates

Posted on 1998-08-04
8
160 Views
Last Modified: 2010-05-03
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")
0
Comment
Question by:majala
8 Comments
 
LVL 9

Expert Comment

by:cymbolic
ID: 1467732
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.
0
 
LVL 6

Expert Comment

by:anthonyc
ID: 1467733
if newdate is a date,then you are not going to change it... do this

msgbox format$(newdate, "dd/mm/yyyy")
0
 

Author Comment

by:majala
ID: 1467734
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.
0
 
LVL 6

Expert Comment

by:anthonyc
ID: 1467735
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!

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:majala
ID: 1467736
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?
0
 
LVL 6

Accepted Solution

by:
clifABB earned 30 total points
ID: 1467737
From the VB help file:
(/) Date separator. In some locales, other characters may be used to represent the date separator. The date separator separates the day, month, and year when date values are formatted. The actual character used as the date separator in formatted output is determined by your system settings.

Actually what you want is to display the / as a literal.  To do this, you need to put a backslash just before it:

The correct answer is:
MsgBox Format$(DateAdd("d", 1, Now), "DD\/MM\/YYYY")

(By the way, thanks for using four digit year :) )
0
 
LVL 2

Expert Comment

by:swilt
ID: 1467738
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

0
 

Author Comment

by:majala
ID: 1467739
Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now