Solved

Date format "MM/dd/yy"

Posted on 2015-01-27
7
215 Views
Last Modified: 2015-01-27
I need date to be in this format "MM/dd/yy". I kept getting "String was not recognized as a valid DateTime."

Dim date_str As String = "11/18/2014"
Dim provider As Globalization.CultureInfo = Globalization.CultureInfo.InvariantCulture
Dim result As Date = Date.ParseExact(date_str, "MM/dd/yy", provider)
0
Comment
Question by:VBdotnet2005
  • 3
  • 2
  • 2
7 Comments
 
LVL 69

Assisted Solution

by:Éric Moreau
Éric Moreau earned 334 total points
ID: 40573381
you 4 digits in your year, try this:
Dim result As Date = Date.ParseExact(date_str, "MM/dd/yyyy", provider)
0
 
LVL 32

Assisted Solution

by:it_saige
it_saige earned 166 total points
ID: 40573383
Your code works fine for me.

Try this instead:
Module Module1
	Sub Main()
		Dim date_str = "11/18/2014"
		Dim format = "MM/dd/yyyy"
		Dim provider = Globalization.CultureInfo.InvariantCulture
		Dim parsed_date = DateTime.MinValue
		If DateTime.TryParseExact(date_str, format, provider, Globalization.DateTimeStyles.None, parsed_date) Then
			Console.WriteLine("Date: {0}", parsed_date.ToString("MM/dd/yyyy"))
		Else
			Console.WriteLine("Could not parse date")
		End If
		Console.ReadLine()
	End Sub
End Module

Open in new window

Which produces the following output:Capture.JPG-saige-
0
 

Author Comment

by:VBdotnet2005
ID: 40573387
Hi Eric, I want my result to return "11/18/14"
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 69

Accepted Solution

by:
Éric Moreau earned 334 total points
ID: 40573392
then you cannot store the result into a data variable:

Dim result As String = Date.ParseExact(date_str, "MM/dd/yyyy", provider).ToString("MM/dd/yy")
0
 
LVL 32

Expert Comment

by:it_saige
ID: 40573393
Ah, nevermind...  I just realized your format string only has two digits for the year.  You need to do what Eric has suggested in order to parse the date because the string must match the format of the provider.  Parsing is used to take a string and format it to a complete date.  Once there you can print the string in any way that you want; i.e. -
Module Module1
	Sub Main()
		Dim date_str = "11/18/2014"
		Dim format = "MM/dd/yyyy"
		Dim provider = Globalization.CultureInfo.InvariantCulture
		Dim parsed_date = DateTime.MinValue
		If DateTime.TryParseExact(date_str, format, provider, Globalization.DateTimeStyles.None, parsed_date) Then
			Console.WriteLine("Date: {0}", parsed_date.ToString("MM/dd/yy"))
		Else
			Console.WriteLine("Could not parse date")
		End If
		Console.ReadLine()
	End Sub
End Module

Open in new window

Produces the following output -Capture.JPG-saige-
0
 

Author Closing Comment

by:VBdotnet2005
ID: 40573398
Than you both :)
0
 
LVL 32

Expert Comment

by:it_saige
ID: 40573402
The format string tells the parse method, what format the incoming date will be presented it, for example -
Module Module1
	Sub Main()
		Dim date_str = "18:2014-11"
		Dim format = "dd:yyyy-MM"
		Dim provider = Globalization.CultureInfo.InvariantCulture
		Dim parsed_date = DateTime.MinValue
		If DateTime.TryParseExact(date_str, format, provider, Globalization.DateTimeStyles.None, parsed_date) Then
			Console.WriteLine("Date: {0}", parsed_date.ToString("MM/dd/yy"))
		Else
			Console.WriteLine("Could not parse date")
		End If
		Console.ReadLine()
	End Sub
End Module

Open in new window

Produces the following output -Capture.JPG-saige-
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

22 Experts available now in Live!

Get 1:1 Help Now