Solved

using Format with dates

Posted on 1998-08-04
8
163 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

751 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