Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to add a leading zero to day and month format?

Status
Not open for further replies.

cesark

Programmer
Joined
Dec 20, 2003
Messages
621
I receive from the DB a date (sql smalldatetime format) broken in three parts (day, month and year. Each one in a different parameter). And once in the web app I want to use day and month with the format (01, 02, 03,.., 10, 11,..), I mean with a leading 0 if necessary, since now I receive day and month with the format (1, 2, 3,.., 10, 11). How can I format the received data?

Here a piece of the sub that receives the data:
Code:
strConnection.open()
       CmdRecover.ExecuteNonQuery	  

		 If Not CmdRecover.Parameters("@day").Value Is DbNull.Value Then
		  day.SelectedValue = CmdRecover.Parameters("@day").Value
		 End If 

		 If Not CmdRecover.Parameters("@month").Value Is DbNull.Value Then
		  month.SelectedValue = CmdRecover.Parameters("@month").Value
		 End If 

		 If Not CmdRecover.Parameters("@year").Value Is DbNull.Value Then
		  year.Text() = CmdRecover.Parameters("@year").Value
		 End If


Thank you,
Cesar
 
If you're not planning on doing any more calculations with the int, you can convert it to a string for display purposes. Just test to see if the value is less than 10; if so, convert to a string and add a zero to the front. If it's 10 or greater, just convert to a string.

Be careful if you need to parse it back into an int value though; I've had problems in the past (and for the life of me I can't remember which language was the culprit) where parsing a string of '03' would return 0 instead of 3.


HTH

tigerjade

"Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live." -- Martin Golding


 
But... Isn’ t there a method or property in VB.NET to format automatically when I receive day and month? For example something like this:
Code:
If Not CmdRecover.Parameters("@day").Value Is DbNull.Value Then
day.SelectedValue = CmdRecover.Parameters("@day").Value.ToString.Format("{dd}")
End If 
 

If Not CmdRecover.Parameters("@month").Value Is DbNull.Value Then
month.SelectedValue = CmdRecover.Parameters("@month").Value.ToString.Format("{MM}")
End If


In VB.NET dd means day of month (always 2 digits, with a leading 0 if needed), and MM means month number (always 2 digits, with a leading 0 if needed):
 
Yes! :-). As I said there is a function in VB that does this operation automatically!. So, the problem it' s solved.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top