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?

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.