Solved

Date format

Posted on 2015-01-28
3
200 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 250 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 250 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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…
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.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

690 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