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
Solved

Date format

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…

792 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