• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Date format question

My system date format is yy-MM-dd

I want my program to always format date as dd/mm/yyyy

I used

dim dd as date

dd=format(dd,"dd/mm/yyyy")

but dd keeps displaying yy-MM-dd

What is the correct code please?
0
NessieB
Asked:
NessieB
  • 5
  • 4
  • 2
  • +1
2 Solutions
 
ADSaundersCommented:
Hi NessieB,
In VB, a date is always stored as a decimal value, with the whole part being the julian date, and the fractional part the time. The format is _ONLY_ used when converting the date to a string (e.g. to display it)
The construct dd=format(dd,"dd/mm/yyyy") should give a mismatched type error if dd is of type date, as format(dd,"dd/mm/yy") is a string.
If however you use string type functions e.g.
Text1.Text = format(dd,"dd/mm/yyyy") or
MsgBox format(dd,"dd/mm/yyyy")
You should get the result you are expecting.

Regards .. Alan
0
 
mugman21Commented:
NessieB,

Your close, but not quite right.... Do this below:

Private Sub Command1_Click()
Dim dd
Dim FormatedDate
dd = Date
FormatedDate = Format(dd, "dd/mm/yyyy")
MsgBox FormatedDate
End Sub

Mugman
0
 
softplusCommented:
Hi NessieB,
You're mixing strings with dates :) - date fields always store just the date, the formatting part is in the string itself.
In other words, what you probably want:
dim dd as date
dim ss as string
ss = format(dd, "dd/MM/yyyy")
here you are storing the formatted date as a string (MM needs to be in upper case)
John
0
Independent Software Vendors: 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!

 
mugman21Commented:
ADSaunders, a little behind you...

Another thing to add, the Date Function always returns a Varient, at least that is what the MSDN says... "Returns a Variant (Date) containing the current system date." - MSDN

Stick with varients here NessieB
0
 
ADSaundersCommented:
NessieB,
The only way to always get the date formatted in that way is to change the default date format in Regional settings in the control panel. However you must be aware that this will not just affect output from VB, and if your program is to be run on other systems, you may not be able to predict (or control) the regional date format on the target machine.
.. Alan
0
 
softplusCommented:
Don't stick with variants, ouch! You're just saving a string of a few characters, why add the massiv overhead for variants? You could also use ss = format$(...) if you want to be sure (not needed in vb.net), but please don't save the result in a variant  :)
John
0
 
mugman21Commented:
oh come on softy, a couple of varients never hurt anyone..... If this guy is writing some huge enterprise software, I could understand not wanting to use varients, but I doubt he is....
0
 
NessieBAuthor Commented:
Ok - thanks for the help - split points for you.
0
 
softplusCommented:
> oh come on softy, a couple of varients never hurt anyone
:)
0
 
mugman21Commented:
Softplus,

I feel like the friendly neighborhood drug dealer now.... Hey kid, "a couple of varients never hurt anyone"

hehe
0
 
softplusCommented:
what else are you dealing? :)) Got any good queries?
0
 
mugman21Commented:
Well,

I usually only deal in bad data types... But since your apparently a query junk, I would recommend googling "marble" + "dynamite", I hear it will blow your rocks off.

Mugman
0

Featured Post

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!

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now