Solved

Date format "MM/dd/yy"

Posted on 2015-01-27
7
224 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 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 34

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
Technology Partners: 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 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 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 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