954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

getting error trying to convert date string in "hh:mm:ss tt MM/dd/yy" format to date

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?

tcon
Newbie Poster
13 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 
Dim myCoolTimeDate As String = Format(DateTime.Now, "hh:mm:ss tt MM/dd/yy")
        MsgBox(myCoolTimeDate)
codeorder
Posting Virtuoso
1,915 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

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")
Luc001
Posting Whiz
376 posts since Mar 2010
Reputation Points: 85
Solved Threads: 98
 

for time:

textbox1.text = TimeOfDay.ToShortTimeString


for date:

Today.ToShortDateString()
Netcode
Veteran Poster
1,021 posts since Jun 2009
Reputation Points: 43
Solved Threads: 67
 

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....

tcon
Newbie Poster
13 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 
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?

jbrock31
Junior Poster in Training
79 posts since Oct 2008
Reputation Points: 22
Solved Threads: 16
 

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!

tcon
Newbie Poster
13 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

simply use:

Textbox1.text = Now.Date
Netcode
Veteran Poster
1,021 posts since Jun 2009
Reputation Points: 43
Solved Threads: 67
 

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.

Luc001
Posting Whiz
376 posts since Mar 2010
Reputation Points: 85
Solved Threads: 98
 

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

tcon
Newbie Poster
13 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

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"))
Luc001
Posting Whiz
376 posts since Mar 2010
Reputation Points: 85
Solved Threads: 98
 

Hi,

You can find some more Time format structures, here.

Luc001
Posting Whiz
376 posts since Mar 2010
Reputation Points: 85
Solved Threads: 98
 

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
tcon
Newbie Poster
13 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Hi,

No problem.

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

Luc001
Posting Whiz
376 posts since Mar 2010
Reputation Points: 85
Solved Threads: 98
 

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

ah i see d link now. nice one.

tcon
Newbie Poster
13 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

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

Thanks for the reputation points :)

Luc001
Posting Whiz
376 posts since Mar 2010
Reputation Points: 85
Solved Threads: 98
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: