Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print date in the format of mm/dd/yyyy in VB

Tags:

date

vb6

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

like image 350
sona Avatar asked Apr 29 '09 06:04

sona


2 Answers

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.

like image 111
jkchong Avatar answered Oct 19 '22 16:10

jkchong


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")
like image 42
Fredrik Mörk Avatar answered Oct 19 '22 16:10

Fredrik Mörk