Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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

just for the record, escaping the slash will work
Format(dt,"MM\/dd\/yyyy")

Try the next code:

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

When you enter date in whatever format it will convert default value so do one thing that in access change your data type date/time to text then it can't affect to your work sure.

I also use VB6 and need to format date in my txt report

this works for me

Format$(Now, "yyyy-mm-dd-00.00.00")

but only if I declare date as string

strDate = Format(strDate, "yyyy-mm-dd") BillTime = Format(BillTime, "hh:mm:ss")

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top