hi im sorry if this isnt in the right forum but i cant seem to find any threads in this.

i cant seem to create a datetime object in vb.net with datetime string that's in this format "hh:mm:ss tt MM/dd/yy".

The error im getting says its not a recognisable string datetime format.


Any ideas on this?

Dani AI

Generated

The string layout you have, time first then date (for example: 12:52:17 PM 11/16/10), is not one of the formats the general parser recognizes. That is why saw TryParse return False. Culture also matters, as pointed out: 11/16/10 only makes sense in an en-US culture. The most robust fix is to use DateTime.TryParseExact with the exact pattern and an explicit culture.

Imports System.Globalization

Dim s = "12:52:17 PM 11/16/10"
Dim formats = {
    "hh:mm:ss tt MM/dd/yy",   ' exact format from the source
    "h:mm:ss tt M/d/yy",      ' tolerate single-digit month/day
    "hh:mm:ss tt MM/dd/yyyy"  ' optionally accept 4-digit year
}
Dim dt As DateTime

If DateTime.TryParseExact(s, formats, CultureInfo.GetCultureInfo("en-US"),
                          DateTimeStyles.None, dt) Then
    ' Use dt as a DateTime. Only format when displaying:
    Dim display = dt.ToString("HH:mm:ss dd/MM/yyyy", CultureInfo.InvariantCulture)
Else
    ' Handle invalid input
End If

A few gotchas to avoid:

  • Use hh with tt for 12-hour input; HH is 24-hour and will not parse with AM/PM.
  • Do not round-trip by formatting to a string and then calling Convert.ToDateTime on that string again. Parse once into a DateTime, store that, and only call ToString for display.
  • With a 2-digit year (yy), .NET maps 00-29 to 2000-2029 and 30-99 to 1930-1999. If that pivot could bite you, require yyyy in the input or include both patterns as shown above.

This keeps the source culture fixed (en-US for MM/dd/yy) while letting you output in any display culture you prefer.

Recommended Answers

All 15 Replies

Dim myCoolTimeDate As String = Format(DateTime.Now, "hh:mm:ss tt MM/dd/yy")
        MsgBox(myCoolTimeDate)

Hi,

If I understand correct then it's this you want to accomplish:

TextBox1.Text = Now.ToString("hh:mm:ss: tt")

TextBox1.Text = Now.ToString("M/dd/yyyy")

for time:

textbox1.text = TimeOfDay.ToShortTimeString

for date:

Today.ToShortDateString()

I have a string with a value such as "12:52:17 PM 11/16/10"

I want to create a datetime instance with that string value but vb doen't seem to recognise datetime strings in that format.


To sum up: I need to take a string in "hh:mm:ss tt MM/dd/yy" format and create a datetime object with that string....

Dim dtTest As New Date()
        Dim test As String = "12:52:17 PM 11/16/10"
        Dim result As Boolean
        result = Date.TryParse(test, dtTest)
        Me.Label1.Text = dtTest.ToString

This will convert it with the Date first, then the time. Is that ok or do you need the time first?

hi jbrock31,

thanks for having a go but 'result' has a false value at debug plus the 'dtTest' date object doesnt take 'test' as its value.

so basically Date.TryParse(test, dtTest) didn't work.

I was wondering could it be a globalisation setting issue? is there a country or globalisation setting in visual studio 2008 that recognises datetime format "hh:mm:ss tt MM/dd/yy"

??


ps thanks to everyone else for trying to help, much appreciated. If ye have any rough ideas as to why it doesn't parse that string into a valid datetime then feel free to share them!

simply use:

Textbox1.text = Now.Date

Hi,

I think that your sytem time isn't the US time format.
To change the forùmat into US time format use this:

Imports System.Globalization 

        Label1.Text = Date.Now.ToString("hh:mm:ss tt MM/d/yyyy", CultureInfo.CreateSpecificCulture("en-US"))

Hope it helps.

hey luc001,

have you ever seen a datetime in "hh:mm:ss tt MM/dd/yy" format before?

i need to create a datetime object from a string that has a date string value that is in "hh:mm:ss tt MM/dd/yy" format.

I don't need to get the date and time of system.date.now

Hi,

No it's always first the date and then the time.

After some testing found this:

Dim date1 As Date = #3/7/2008 7:00:00 AM#
        Label1.Text = date1.ToString()
        Dim date2 As Date = #3/7/2008 7:00:00 AM#
        Label2.Text = date1.ToString(System.Globalization.CultureInfo.CreateSpecificCulture("en-US"))
commented: a combination of your posts lead me to a solution when i pieced bits of code together. i will post the final solution soon :) +1

Hi,

You can find some more Time format structures, here.

ok i finally got it doing what i want.

it was a culture issue indeed, thanks for that Luc001.

here is my code:

Imports System.Globalization.CultureTypes

Private Function tidyDate(ByVal datetimeval As String) As String
        
        Dim dt As New DateTime()
               dt = DateTime.Parse(datetimeval, System.Globalization.CultureInfo.CreateSpecificCulture("en-US"))
        
Return dt.ToString("HH:mm:ss dd/MM/yyyy")

    End Function

Private Sub form1_Load()

Dim tmpdate As New DateTime()
tmpdate = Convert.ToDateTime(tidyDate("12:57:17 PM 11/16/10"))
messagebox.show(tmpdate.tostring())

End Sub

Hi,

No problem.

If that solution solved your problem then mark your thread as resolved.

sorry i thought i marked it when i gave you a plus grade..

ah i see d link now. nice one.

Just to let the others know that your problem is solved.

Thanks for the reputation points :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.