Click here to Skip to main content
15,886,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a file that has 3 different date time displays and i m trying to take the odd one and convert it to the other format and when i do i get this error below

Message=Conversion from string "Sept 21, 2009 21:18:55" to type 'Date' is not

If someone could look at the code i m using and tell me if im doing this wrong that would be great.

VB
If Not Value5 = "" Then
    Dim time As DateTime = Value5
    Dim format As String = "MMM d, yyyy HH:mm"
    Value5 = time.ToString(format)
End If
Posted
Updated 4-Jun-12 10:24am
v2

You are trying to instantiate a DateTime object with string. It should be:

VB
If Not Value5 = "" Then
    Dim time As DateTime = Convert.ToDateTime(Value5)
    Dim format As String = "MMM d, yyyy HH:mm"
    Value5 = time.ToString(format)
End If


Look at MSDN documentation:

http://msdn.microsoft.com/en-us/library/xhz1w05e#Y0[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 4-Jun-12 16:38pm    
My 5.
--SA
use this:
C#
var Value5 = string.Format("{0:MMM d, yyyy HH:mm:ss}", time);

this work just fine.
in .ToString() method you set the culture not format!
 
Share this answer
 
Comments
taha bahraminezhad Jooneghani 4-Jun-12 16:35pm    
this is the code in vb:
Dim Value5 = String.Format("{0:MMM d, yyyy HH:mm:ss}", time)
only a Dim!:)
If I understand correctly, the problem isn't to convert a DateTime value to a String one but a String to a String formatted with another DateTime format.

I found that the problem is in the abbreviated month name.

So if you try the follow code I think you'll solve the problem:

VB
Sub Main()

    Dim Value5 As String = "Sept 21, 2009 21:18:55"

    Dim ci As New CultureInfo("en-us", False)
    Dim dtfi As DateTimeFormatInfo = ci.DateTimeFormat

    ' This are the default abbreviated month names that cause the problem
    'dtfi.AbbreviatedMonthNames = {"Gen", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ""}

    ' Try the follow updated ...
    dtfi.AbbreviatedMonthNames = {"Gen", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec", ""}

    If Not Value5 = "" Then

        Dim time As DateTime = Convert.ToDateTime(Value5, dtfi)

        Dim format As String = "MMM d, yyyy HH:mm"
        Value5 = time.ToString(format)

    End If

    Console.WriteLine(Value5)

    Console.ReadLine()

End Sub



I hope I was helpful

Gianlorenzo
 
Share this answer
 
Comments
VJ Reddy 4-Jun-12 20:31pm    
Good answer. 5!
Sergey Alexandrovich Kryukov 5-Jun-12 0:32am    
Those month names are already available in format -- useless code.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900