Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Date format "MM/dd/yy"

Posted on 2015-01-27
7
Medium Priority
?
230 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

610 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