• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 437
  • Last Modified:

How can I convert this string 'Jan 28 2009, 17:28 PM' in to a date variable?

I have strings containing dates in the following format

Jan 28 2009, 17:28 PM

How do I convert this into a date and assign to a date variable?
0
mindwarpltd
Asked:
mindwarpltd
  • 4
  • 4
  • 3
  • +1
1 Solution
 
GrahamSkanRetiredCommented:
Try
MyDateVariable = Cdate(Replace("Jan 28 2009, 17:28 PM", ",",""))
0
 
souvik2008Commented:
use this code :-

string str="Jan 28 2009, 17:28 PM";
datetime dt=DateTime.Parse(str);

Hope this will work.
0
 
mindwarpltdAuthor Commented:
Graham,

An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll

Additional information: Cast from string "Jan 28 2009 17:28 PM" to type 'Date' is not valid.

Souvik,

Thats not VB.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
GrahamSkanRetiredCommented:
Are you running VB.Net?
I haven't tested for .Net
0
 
mindwarpltdAuthor Commented:
Yes Vb.Net
0
 
souvik2008Commented:
Yes I have posted it in C#. Now thats your VB code.

Thanks.
Dim str As String = "Jan 28 2009, 17:28 PM"
Dim dt As datetime = DateTime.Parse(str)

Open in new window

0
 
mindwarpltdAuthor Commented:
Souvik,

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: String was not recognized as a valid DateTime.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
What if the day was less than 10?...would it be "1" or "01"?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The PM is puzzling too...why add AM/PM when it's using 24 hour military time?...just an observation.
0
 
mindwarpltdAuthor Commented:
Yes there would be a zero in front for day less that 10.

Yes it is puzzling about PM with 24 hour.

Its a date provided by some PHP.
Don't want to change the PHP.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Oh...last question...

Which are you using?

    VB.Net 2003 (or below)
    VB.Net 2005 (or above)
0
 
GrahamSkanRetiredCommented:
I have, but rarely use, .Net. If I chop the redundant PM off the end, the code works without error

Programatically, in VB6
str= Left("Jan 28 2009, 17:28 PM", 18)
does that

I don't know how to do it in .Net
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can use DateTime.ParseExact(), or DateTime.TryParseExact() for VB.Net 2005 (or above).

See here for formatting codes: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

In addition to the day being zero padded, this example also assumes that hours less than 12 will be preceded by a zero:
        Dim dt As DateTime
        Dim strDT As String = "Jan 28 2009, 17:28 PM"
        Dim ci As New System.Globalization.CultureInfo("en-US")
 
        If DateTime.TryParseExact(strDT, "MMM dd yyyy, HH:mm tt", ci, Globalization.DateTimeStyles.None, dt) Then
            ' ...use "dt" somehow...
            MessageBox.Show(dt.ToString, "Parse Successful", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(strDT, "Invalid Date", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now