Results 1 to 8 of 8

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

  1. #1
    New Member
    Join Date
    Sep 12
    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
    Fanatic Member
    Join Date
    Sep 12
    Location
    To the moon and then left
    Posts
    580

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

    Look up the DateDiff-Function in your Help-File
    For health reasons i try to avoid reading unformatted Code

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    81,249

    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.

  4. #4
    Fanatic Member
    Join Date
    Sep 12
    Location
    To the moon and then left
    Posts
    580

    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
    For health reasons i try to avoid reading unformatted Code

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    81,249

    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.

  6. #6
    Lively Member
    Join Date
    Nov 06
    Posts
    110

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


  7. #7
    Fanatic Member
    Join Date
    Sep 12
    Location
    To the moon and then left
    Posts
    580

    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
    For health reasons i try to avoid reading unformatted Code

  8. #8
    Super Moderator Hack's Avatar
    Join Date
    Aug 01
    Location
    Searching for mendhak
    Posts
    58,292

    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
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

Posting Permissions

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