• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 110
  • Last Modified:

I need help formating a date variable in my VB.Net application

Good afternoon experts,
I have a date variable that i want to format so that the date value as 9/27/2016
displays as follows 0927.

I am using the following format:

but when it places the value of the date field it only displays 927.  It drops the leading 0.
How can I fix this?

Thanks in advance,
1 Solution
Russ SuterCommented:
Interestingly I didn't have the same problem. However you can always use the string.Format method like this:
string.Format("{0:00}{1:00}", DateTime.Now.Month, DateTime.Now.Day)

Open in new window

That will ensure a 2 digit value ALWAYS.
Éric MoreauSenior .Net ConsultantCommented:
I am pretty sure that your MMdd format was returning the correct value (including the leading 0) but if you stored the returned value in a numerical variable, it is getting rid of the 0. You need to store the result in a string:
dim x as string = MyDate.ToString("MMdd")

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now