Solved

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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

830 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