Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Display Date

Display Date

Scheduled Pinned Locked Moved Visual Basic
question
11 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    macca24
    wrote on last edited by
    #1

    I want to display the year on a label on my form. How do you get the year from the date. I have tried this to no avail: Dim DateItem As Date Num.Text = d.Year.ToString() Num is the label name Anyone any ideas?

    C G 3 Replies Last reply
    0
    • M macca24

      I want to display the year on a label on my form. How do you get the year from the date. I have tried this to no avail: Dim DateItem As Date Num.Text = d.Year.ToString() Num is the label name Anyone any ideas?

      C Offline
      C Offline
      chrismerrill
      wrote on last edited by
      #2

      'this would get today's year num.text = Date.Now.Year.ToString so you need to set DateItem to a date before using it. dim DateItem as new date(2005,05,23) num.text = DateItem.Year.ToString

      M 1 Reply Last reply
      0
      • C chrismerrill

        'this would get today's year num.text = Date.Now.Year.ToString so you need to set DateItem to a date before using it. dim DateItem as new date(2005,05,23) num.text = DateItem.Year.ToString

        M Offline
        M Offline
        macca24
        wrote on last edited by
        #3

        Chris, Thanks for the reply. Is it necessary to set the date as dim DateItem as new date(2005,05,23), could you not get the date from the system as System.Date ?

        K D 2 Replies Last reply
        0
        • M macca24

          Chris, Thanks for the reply. Is it necessary to set the date as dim DateItem as new date(2005,05,23), could you not get the date from the system as System.Date ?

          K Offline
          K Offline
          KreativeKai
          wrote on last edited by
          #4

          Here is what I've used in the past: label1.Text = CStr(Year(CDate(DateString))) Hope this helps :) Lost in the vast sea of .NET

          Visit my website at www.komputing.com

          1 Reply Last reply
          0
          • M macca24

            Chris, Thanks for the reply. Is it necessary to set the date as dim DateItem as new date(2005,05,23), could you not get the date from the system as System.Date ?

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            No. All you need to get the current date is:

            Dim myDate As Date = Date.Now()

            RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            A M 2 Replies Last reply
            0
            • D Dave Kreskowiak

              No. All you need to get the current date is:

              Dim myDate As Date = Date.Now()

              RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

              A Offline
              A Offline
              Anonymous
              wrote on last edited by
              #6

              Thanks for the reply. I realise this should bring back the date Now but how do I get the year from this date Now as it is bringing back the entire date?

              M 1 Reply Last reply
              0
              • D Dave Kreskowiak

                No. All you need to get the current date is:

                Dim myDate As Date = Date.Now()

                RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                M Offline
                M Offline
                macca24
                wrote on last edited by
                #7

                Thanks for the reply. That Anonymous posting was me also, I hadn't logged in, i logged in and reposted so you know who is posting. I realise the above code should bring back the date Now but how do I get the year from this date Now as it is bringing back the entire date?

                D 1 Reply Last reply
                0
                • A Anonymous

                  Thanks for the reply. I realise this should bring back the date Now but how do I get the year from this date Now as it is bringing back the entire date?

                  M Offline
                  M Offline
                  mtone
                  wrote on last edited by
                  #8

                  Dim myDate As Date = Date.Now() Dim myYear As String = myDate.Year()

                  1 Reply Last reply
                  0
                  • M macca24

                    I want to display the year on a label on my form. How do you get the year from the date. I have tried this to no avail: Dim DateItem As Date Num.Text = d.Year.ToString() Num is the label name Anyone any ideas?

                    C Offline
                    C Offline
                    chrismerrill
                    wrote on last edited by
                    #9

                    Are you still having trouble getting the year from the date? Have you tried the following (this will get the current year): num.text = Date.Now.Year.ToString For example, today is May 23, 2005 Date.Now.Year.ToString --> will print 2005 Date.Now.Month.ToString --> will print 5 Date.Now.Day.ToString --> will print 23 If you want some other year (let's say you're parsing a date string) dim DateItem as Date = Date.Parse(dateString) num.text = DateItem.Year.ToString or num.text = Date.Parse(dateString).Year.ToString Hope this helps, Chris

                    1 Reply Last reply
                    0
                    • M macca24

                      Thanks for the reply. That Anonymous posting was me also, I hadn't logged in, i logged in and reposted so you know who is posting. I realise the above code should bring back the date Now but how do I get the year from this date Now as it is bringing back the entire date?

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #10

                      The Year property is an Integer, so it work just like any other Interger number.

                      Dim myDate As Date = Date.Now
                      Dim currentYear As Integer = myDate.Year

                      or, in a single line of code:

                      Dim currentYear As Integer = Date.Now.Year

                      Docs for the DateTime structure[^] Now is a static property that always returns the current Date and Time in a single DateTime structure. You could use the Today static property to return just the date, but it still returns an entire DateTime structure, just with the time set to 00:00:00.0. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                      1 Reply Last reply
                      0
                      • M macca24

                        I want to display the year on a label on my form. How do you get the year from the date. I have tried this to no avail: Dim DateItem As Date Num.Text = d.Year.ToString() Num is the label name Anyone any ideas?

                        G Offline
                        G Offline
                        George Inacio
                        wrote on last edited by
                        #11

                        Try this. Label1.Caption = Year(Date) George

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups