Home > database >  How to get single-digit Days in a certain format in Visual Basic?
How to get single-digit Days in a certain format in Visual Basic?

Time:11-02

I am using the following Visual Basic code to get the Day from a Date:

Dim thisDate As Date
Dim thisDay As 
thisDate = Today
thisDay = Microsoft.VisualBasic.DateAndTime.Day(thisDate)

When the day is a single digit such as today (November 1st) then thisDay returns 1. Is there a way to adjust this so that single-digit days are returned as such 01, 02, 03,..., instead of 1, 2, 3,...?

Thank you!

CodePudding user response:

You can convert the date(using Now) to a string and then manipulate the string accordingly.

Example:

 Dim twodigitdate As String
 twodigitdate = Now.ToString("dd/MM/yyyy")
 twodigitdate = Mid(twodigitdate, 1, 2)
 MsgBox(twodigitdate)

or you can just use this one line code :-

dim twodigitdate as string = Now.ToString("dd")
  • Related