tags:

views:

727

answers:

3

Hi

I need to print the date in the format of mm/dd/yyyy. if the date is 4/24/2009 it should print the date as 04/24/2009. that is zero padding is also needed.. i used date function to get the current date...but the date is getting in the format of m/dd/yyyy... please help me on this...

A: 

Format(dt,"MM/dd/yyyy")

Vikram
This is not working for me...
sona
du u have any other solution?Please help me this if u know...
sona
+1  A: 

Note that the "/" character in date formatting functions has a special meaning, as "date separator". This means that i may be replaced with the date separator for the current locale that the program is executed in (here in Sweden it would be replaced with "-" for instance). In order to ensure that you do indeed get the "/" character in the output, I think this would work (I don't have a VB installation to verify with):

Format(date, "MM'/'dd'/'yyyy")
Fredrik Mörk
yes you are write this will work only if the date type is of format string... but i am using Date format...i am having one table..that has one column of data type dateso even if i convert the date to string as you mentioned.it will automatically will get convert to date ven i insert the value to tabel..for eg: dim dt as stringdt=Format(Date,"mm/dd/yyyy")'this will convert to 04/24/2009but ven i insert this to table it will convert back to 4/24/2009 because that particular column is of type date
sona
The reason it converts back when put in the table is that the table does not store the date as a string, but as a date object (which deep down is a numeric value). What you see is only a textual representation of it. I guess your DB software will automatically use your locales default to show the value to you. If you want it displayed in some other way in your application, you will need to format it before putting it into the control used for showing it.
Fredrik Mörk
Thank you very much for your help
sona
+1  A: 

Tested in the immediate window and is working for me (output as a comment)

Format(Now, "MM/dd/yyyy") '04/29/2009
Format(Date, "MM/dd/yyyy") '04/29/2009
Format(CStr(Now), "MM/dd/yyyy") '04/29/2009
Format(Date$, "MM/dd/yyyy") '04/29/2009
Format(CDate(Date), "MM/dd/yyyy")'04/29/2009

So whether it is string or datetime should not matter.

Edit: Saw your comment to Fredrik. It doesn't matter how it looks like when you save it to the db table (column date format would be a property of the db and not your program's (or vb's) responsibility). Just format the value as and when you retrieve it from the db.

jkchong
if you try Msgbox(Format(Date, "MM/dd/yyyy")) it will work.. but i tried like... Dim dt as Date d = Format(Date, "MM/dd/yyyy") for this it wont work.
sona
The date data type, if unformatted, always displays as per system regional settings. You just need to format it when you want to display it.
jkchong
Like what Fredrik said, even VB stores dates as a number, not a literal m/d/y string. Explanation of the storage here: http://www.codeguru.com/vb/gen/vb_misc/algorithms/article.php/c7495
jkchong
thanks a lot guys...lastly i changed the data type of column to string...now it is working fine...
sona