Ravikant Upadhyay
Ravikant Upadhyay

Reputation: 405

Display date in dd/mm/yyyy format in vb.net

I want to display date in 09/07/2013 format instead of 09-jul-13.

Dim dt As Date = Date.Today

MsgBox(dt)

Upvotes: 20

Views: 398273

Answers (7)

Prasanna
Prasanna

Reputation: 19

if you want to display date along with time when you export to Excel then you can use this

xlWorkSheet.Cells(nRow, 3).NumberFormat = "dd/mm/yy h:mm AM/PM"

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460018

First, uppercase MM are months and lowercase mm are minutes.

You have to pass CultureInfo.InvariantCulture to ToString to ensure that / as date separator is used since it would normally be replaced with the current culture's date separator:

MsgBox(dt.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture))

Another option is to escape that custom format specifier by embedding the / within ':

dt.ToString("dd'/'MM'/'yyyy")

MSDN: The "/" Custom Format Specifier:

The "/" custom format specifier represents the date separator, which is used to differentiate years, months, and days. The appropriate localized date separator is retrieved from the DateTimeFormatInfo.DateSeparator property of the current or specified culture.

Upvotes: 41

Ken Ince
Ken Ince

Reputation: 11

I found this catered for dates in 21st Century that could be entered as dd/mm or dd/mm/yy. It is intended to print an attendance register and asks for the meeting date to start with.

Sub Print_Register()

Dim MeetingDate, Answer

    Sheets("Register").Select
    Range("A1").Select
GetDate:
    MeetingDate = DateValue(InputBox("Enter the date of the meeting." & Chr(13) & _
    "Note Format" & Chr(13) & "Format DD/MM/YY or DD/MM", "Meeting Date", , 10000, 10000))
    If MeetingDate = "" Then GoTo TheEnd
    If MeetingDate < 36526 Then MeetingDate = MeetingDate + 36526
    Range("Current_Meeting_Date") = MeetingDate
    Answer = MsgBox("Date OK?", 3)
    If Answer = 2 Then GoTo TheEnd
    If Answer = 7 Then GoTo GetDate
    ExecuteExcel4Macro "PRINT(1,,,1,,,,,,,,2,,,TRUE,,FALSE)"
TheEnd:
End Sub

Upvotes: 1

user5195203
user5195203

Reputation: 21

You could decompose the date into it's constituent parts and then concatenate them together like this:

MsgBox(Now.Day & "/" & Now.Month & "/" & Now.Year)

Upvotes: 2

matzone
matzone

Reputation: 5719

Like this ..

MsgBox(format(dt,"dd/MM/yyyy"))

Upvotes: 4

Darshan
Darshan

Reputation: 337

Try this.

 var dateAsString = DateTime.Now.ToString("dd/MM/yyyy");
// dateAsString = "09/07/2013"

and also check this link for more formatting data and time

Upvotes: 11

VB.NET LEARNER
VB.NET LEARNER

Reputation: 711

Dim formattedDate As String = Date.Today.ToString("dd/MM/yyyy")

Check link below

Upvotes: 1

Related Questions