Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

using Format with dates

Posted on 1998-08-04
8
Medium Priority
?
166 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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 120 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

972 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