?
Solved

Date format "MM/dd/yy"

Posted on 2015-01-27
7
Medium Priority
?
228 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
[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
  • 3
  • 2
  • 2
7 Comments
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 1336 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 34

Assisted Solution

by:it_saige
it_saige earned 664 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Accepted Solution

by:
Éric Moreau earned 1336 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 34

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 34

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

765 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