Solved

using Format with dates

Posted on 1998-08-04
8
158 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

705 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

22 Experts available now in Live!

Get 1:1 Help Now