Results 1 to 8 of 8

Thread: how to creat code with VB to calculate dat ot Year(2012)

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2012
    Posts
    1

    how to creat code with VB to calculate dat ot Year(2012)

    I would like to know how to write vb code about this:
    My question is
    1. a combobox to selct month
    2. a textbox to input Day
    3. a label to display Day of Year(2012)
    4. and click add the button to calculate and show the result

    For example:
    Feb. 14 is : the 45th day of this year.

    I'm new and still learning.

    Thank you so much for your help.

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,263

    Re: how to creat code with VB to calculate dat ot Year(2012)

    Look up the DateDiff-Function in your Help-File
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to creat code with VB to calculate dat ot Year(2012)

    There's no need to do any calculation. The DateTime data type has a DayOfYear property. You can construct a DateTime by invoking a constructor and passing the year, month and day and then just get that property value. If you want the current year then that would be DateTime.Now.Year.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,263

    Re: how to creat code with VB to calculate dat ot Year(2012)

    Quote Originally Posted by jmcilhinney View Post
    There's no need to do any calculation. The DateTime data type has a DayOfYear property. You can construct a DateTime by invoking a constructor and passing the year, month and day and then just get that property value. If you want the current year then that would be DateTime.Now.Year.
    And you're sure you're talking about VB6?
    Sounds like .NET to me
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to creat code with VB to calculate dat ot Year(2012)

    Quote Originally Posted by Zvoni View Post
    And you're sure you're talking about VB6?
    Sounds like .NET to me
    Ah bummer! I found this thread in the New Posts section and when I looked across to the right to see what forum it was in I must have gone a bit wonky. Not the first time I've done that and I suspect that it won't be the last. Sorry about that.

    That said, I have to wonder why anyone who is just starting out with programming, as it would appear that the OP is, would start with VB6 when VB.NET has been around for over 10 years now.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Addicted Member
    Join Date
    Nov 2006
    Posts
    129

    Re: how to creat code with VB to calculate dat ot Year(2012)


  7. #7
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,263

    Re: how to creat code with VB to calculate dat ot Year(2012)

    Quote Originally Posted by jmcilhinney View Post
    That said, I have to wonder why anyone who is just starting out with programming, as it would appear that the OP is, would start with VB6 when VB.NET has been around for over 10 years now.
    At a guess, because he's probably using Excel/VBA instead of the original VB-IDE
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: how to creat code with VB to calculate dat ot Year(2012)

    Welcome to the forums

    Here is another way using DatePart
    Code:
    Option Explicit
    
    Private Sub Form_Load()
    Dim i As Long
    For i = 1 To 12
        Combo1.AddItem MonthName(i)
        Combo1.ItemData(Combo1.NewIndex) = i
    Next
    End Sub
    
    Private Sub Command1_Click()
    Dim intDayOfYear As Integer
    intDayOfYear = DatePart("y", Combo1.ItemData(Combo1.ListIndex) & "/" & CInt(Text1.Text) & "/" & Year(Now))
    Text2.Text = intDayOfYear
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width