Solved

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

Posted on 2016-09-27
2
44 Views
Last Modified: 2016-09-28
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:
MyDate.ToString("MMdd")

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,
mrotor
0
Comment
Question by:mainrotor
2 Comments
 
LVL 20

Accepted Solution

by:
Russ Suter earned 500 total points
ID: 41818682
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.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41819688
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

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now