Solved

Date format

Posted on 2015-01-28
3
189 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
3 Comments
 
LVL 32

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

14 Experts available now in Live!

Get 1:1 Help Now