Solved

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

Posted on 2016-09-27
2
74 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 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 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

738 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