?
Solved

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

Posted on 2016-09-27
2
Medium Priority
?
89 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 20

Accepted Solution

by:
Russ Suter earned 2000 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 70

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

762 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