Solved

Date format "MM/dd/yy"

Posted on 2015-01-27
7
217 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 33

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 33

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 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error in page 3 45
Vb.net dynamic formulas in runtime 11 61
Split in Javascript 5 30
Angular - break the dataset in chunks? 7 39
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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