Solved

Date format

Posted on 2015-01-28
3
191 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 33

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

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 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

17 Experts available now in Live!

Get 1:1 Help Now