?
Solved

Date format

Posted on 2015-01-28
3
Medium Priority
?
202 Views
Last Modified: 2015-01-28
I want to format date from "2014-11-21 00:00:00" to 11/21/14.  It works fine on test_1, but on test_2 I got String was not recognized as a valid DateTime.
I am not sure why?

String was not recognized as a valid DateTime.

 Dim test_1 As DateTime = "2014-11-21 00:00:00"
 Dim test_2 As DateTime = "2014-11-06 00:00:00"

 Dim result As String = Date.ParseExact(test_1, "MM/dd/yyyy", Globalization.CultureInfo.InvariantCulture).ToString("MM/dd/yy")
 Dim result1 As String = Date.ParseExact(test_2, "MM/dd/yyyy", Globalization.CultureInfo.InvariantCulture).ToString("MM/dd/yy")

Open in new window

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 Comments
 
LVL 34

Accepted Solution

by:
it_saige earned 1000 total points
ID: 40576023
It's because you already have test_1 and test_2 defined as DateTime objects and ParseExact is using their ToString() methods to retrieve their string values.  Date.ParseExact is used to convert (or parse) strings into DateTime objects.

The ToString() method would be called on each of the DateTime objects, whereby the string representation for test_2 is 11/6/2014.  This does not match the format of MM/dd/yyyy as the day portion contains one character (test_1 on the other hand has a two-digit date).  Proof of Concept:
Module Module1
	Sub Main()
		Dim test_2 As DateTime = "2014-11-06 00:00:00"
		Console.WriteLine(test_2)
		Console.ReadLine()
	End Sub
End Module

Open in new window

Which produces the following output:Capture.JPGTo correct, either remove the parse lines (as they are already DateTime objects):
Module Module1
	Sub Main()
		Dim test_1 As DateTime = "2014-11-21 00:00:00"
		Dim test_2 As DateTime = "2014-11-06 00:00:00"

		Console.WriteLine(test_1.ToString("MM/dd/yy"))
		Console.WriteLine(test_2.ToString("MM/dd/yy"))
		Console.ReadLine()
	End Sub
End Module

Open in new window

Which produces the following:Capture.JPGOr remove the DateTime specification from test_1 and test_2 thereby making them strings.  But you will have to change your format in the Date.ParseExact() method:
Module Module1
	Sub Main()
		Dim test_1 = "2014-11-21 00:00:00"
		Dim test_2 = "2014-11-06 00:00:00"

		Dim result As String = Date.ParseExact(test_1, "yyyy-MM-dd hh:mm:ss", Globalization.CultureInfo.InvariantCulture).ToString("MM/dd/yy")
		Dim result1 As String = Date.ParseExact(test_2, "yyyy-MM-dd hh:mm:ss", Globalization.CultureInfo.InvariantCulture).ToString("MM/dd/yy")
		Console.WriteLine(result)
		Console.WriteLine(result1)
		Console.ReadLine()
	End Sub
End Module

Open in new window

Which would produce the same output as above.

-saige-
0
 
LVL 47

Assisted Solution

by:Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs) earned 1000 total points
ID: 40576029
The ParseExact  method expects a String to be provided for the date, not a Date (see MSDN).

As you already have Dates, you don't need to parse them to show different formats with the ToString method...

Dim test_1 As DateTime = "2014-11-21 00:00:00"
 Dim test_2 As DateTime = "2014-11-06 00:00:00"

 Dim result As String = test_1.ToString("MM/dd/yy")
 Dim result1 As String = test_2.ToString("MM/dd/yy")

Open in new window


This will tell you how it is interpreting the date strings you initially provided.
0
 

Author Closing Comment

by:VBdotnet2005
ID: 40576053
Thank you both
0

Featured Post

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!

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
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