12 Replies - 10272 Views - Last Post: 28 June 2011 - 08:21 AM Rate Topic: -----

#1 kalliereyneke  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 22-February 11

Conversion from string to date in label

Posted 28 June 2011 - 04:02 AM

hi there i am trying to put my string that i receive in yyyy/mm/dd format in my label

i am how ever getting the following error

Conversion from string "20110627" to type 'Date' is not valid.

below is my code any suggestions or help will be appreciated


LBLORDD.Text = dr.Item("ORDRCVDATE").ToString
            Label1.Text = FormatDateTime([LBLORDD].Text, DateFormat.ShortDate)




Is This A Good Question/Topic? 0
  • +

Replies To: Conversion from string to date in label

#2 Nightfish  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 74
  • View blog
  • Posts: 158
  • Joined: 24-May 11

Re: Conversion from string to date in label

Posted 28 June 2011 - 04:35 AM

Well, assuming you always get your dates like this, I'd suggest you use substring to get the year, month and day into one variable each, convert them to integer and then use

Dim date as New Date(year, month, day)


I'm sure there's a more spiffy way of doing it, but this works for me.

This post has been edited by Nightfish: 28 June 2011 - 04:37 AM

Was This Post Helpful? 1
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,351
  • Joined: 29-May 08

Re: Conversion from string to date in label

Posted 28 June 2011 - 05:12 AM

I would also recommend do that Nightfish suggested.
Converting it to Date Object makes format independent, as it can reformatted with format string.
Was This Post Helpful? 1
  • +
  • -

#4 kalliereyneke  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 22-February 11

Re: Conversion from string to date in label

Posted 28 June 2011 - 05:17 AM

hi there yes i always get date in 20110627 format, i am not sure how to do the substring for the year , month , day and how to convert them to integer.

could i possibly ask for an example of how to do that
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,351
  • Joined: 29-May 08

Re: Conversion from string to date in label

Posted 28 June 2011 - 05:27 AM

Firstly you need for work out the correct Date FormatStrings for your format.
Then use Date.TryParseExact
Was This Post Helpful? 1
  • +
  • -

#6 Nightfish  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 74
  • View blog
  • Posts: 158
  • Joined: 24-May 11

Re: Conversion from string to date in label

Posted 28 June 2011 - 05:29 AM

If you're using visual studio, intellisense tells you all you need to know about substring, really.

There's multiple options for substring, I'd use this one: First parameter is where you start, second parameter is how long your substring is supposed to be.

            Dim i As Integer = 12345678
            i.ToString.Substring(0, 4)



gives you "1234" (starts at the beginning and is 4 characters long.

As I said, I would go and slice your big number into 3 smaller ones and then construct my date from that.

This post has been edited by Nightfish: 28 June 2011 - 05:30 AM

Was This Post Helpful? 1
  • +
  • -

#7 kalliereyneke  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 22-February 11

Re: Conversion from string to date in label

Posted 28 June 2011 - 06:05 AM

thanks for the response


   Dim orderdate As String
            Dim ordyear As String
            Dim ordmonth As String
            Dim ordday As String
            orderdate = LBLORDD.Text
            ordyear = orderdate.Substring(1, 4)
            ordmonth = orderdate.Substring(4, 2)
            ordday = orderdate.Substring(6, 2)



i have those values now but not sure how to get the in the yyyy/mm/dd format in label1
Was This Post Helpful? 0
  • +
  • -

#8 Nightfish  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 74
  • View blog
  • Posts: 158
  • Joined: 24-May 11

Re: Conversion from string to date in label

Posted 28 June 2011 - 06:12 AM

You should start counting at 0 for the year or you'll get 0110 as your substring. Probably not what you want.

I already told you how to create a date from those 3 values in my first post. I don't need to quote myself, do I? ;) Then you just set the labeltext to that date and you should be done. Label1.Text = ... should do it.
Was This Post Helpful? 1
  • +
  • -

#9 kalliereyneke  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 22-February 11

Re: Conversion from string to date in label

Posted 28 June 2011 - 06:31 AM

sorry got it right now

 Dim orderdate As String
            Dim ordyear As String
            Dim ordmonth As String
            Dim ordday As String
            orderdate = LBLORDD.Text
            ordyear = orderdate.Substring(0, 4)
            ordmonth = orderdate.Substring(4, 2)
            ordday = orderdate.Substring(6, 2)
            Dim orderdate1 As New Date(ordyear, ordmonth, ordday)
            Label1.Text = orderdate1



i receive date as 2011-06-27 how do i get it now as 2011/06/27

i apogolise for all the questions
Was This Post Helpful? 0
  • +
  • -

#10 Nightfish  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 74
  • View blog
  • Posts: 158
  • Joined: 24-May 11

Re: Conversion from string to date in label

Posted 28 June 2011 - 06:37 AM

I don't know if you can change the separator from a - to a /. Tbh I'd just make my own string then and screw the date format.

Dim orderdate1 As String = ordyear & "/" & ordmonth & "/" & ordday


If you're just showing this in a label it doesn't really need to be anything other than a string anyway.

This post has been edited by Nightfish: 28 June 2011 - 06:44 AM

Was This Post Helpful? 1
  • +
  • -

#11 kalliereyneke  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 22-February 11

Re: Conversion from string to date in label

Posted 28 June 2011 - 06:47 AM

thank you that is perfect thanks for all the help :-)
Was This Post Helpful? 0
  • +
  • -

#12 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,351
  • Joined: 29-May 08

Re: Conversion from string to date in label

Posted 28 June 2011 - 06:48 AM

View PostNightfish, on 28 June 2011 - 02:37 PM, said:

I don't know if you can change the separator from a - to a /. Tbh I'd just make my own string then and screw the date format.

Why not because screwing around with strings is soo much better.

The parsing problem has already be solved for, this is format strings sort out.

Dim d="20110627"
Dim date_out As Date
Date.TryParseExact(s,"yyyyMMdd",Nothing, System.Globalization.DateTimeStyles.None,dat_out)

Dim f=date_out.ToString("dd/MM/yyyy")


This post has been edited by AdamSpeight2008: 28 June 2011 - 06:51 AM

Was This Post Helpful? 0
  • +
  • -

#13 Nightfish  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 74
  • View blog
  • Posts: 158
  • Joined: 24-May 11

Re: Conversion from string to date in label

Posted 28 June 2011 - 08:21 AM

View PostAdamSpeight2008, on 28 June 2011 - 06:48 AM, said:

Why not because screwing around with strings is soo much better.


Never said it was better, just said that I knew this gets the job done and wasn't sure about what date can actually do. ;)Yours is the proper way of doing things, no doubt. Mine's the "too lazy to do more research right now" way.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1