Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to determine day number in a year?

Status
Not open for further replies.

PTW

Programmer
Jul 7, 2000
82
CA
I need to calculate the day number in a year. For example, Jan 1st would be 1, Feb 1st would be 32, and Dec 31st would be 365 or 366.

Are there any simple functions or formulas for this? Thanks!
 
? datediff("d", "1/1/" & Year(Now), Now)
187
MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
Thank-you very much! I had to include a "+1" to get the correct number.
 
PTW,

I have a program designed just for your issue. I designed a program that will display the current day number of the year (a Julian Date) in a label (lbl1, lbl2, lbl3, lbl4), based on the users input of a month (in a list box (lst1, lst2, lst3, lst4)) and day (in a text box (txt1, txt2, txt3, txt4)). Then if the current year is a leap year, there is a check box that the user will need to check (chkBox). After the user inputs the data, they will click on the command button for Calculate Date (cmdCalc). I have also provided them with a clear button that will clear all the data off of the form (cmdClear). I will try to draw the form, and will provide the code for it here:

The form:
Code:
**********************************************************
*  Julian Dates                                      * X *
**********************************************************
*                                                        *
*          Month              Day       Julian Date Is:  *
*           ______________     __        _______         *
* Date 1:  | July      |\/|   |  |      |       |        *  
*          ----------------   |__|      |_______|        *
*                                                        *
*           ______________     __        _______         *
* Date 2:  | July      |\/|   |  |      |       |        *  
*          ----------------   |__|      |_______|        *
*                                                        *
*           ______________     __        _______         *
* Date 3:  | July      |\/|   |  |      |       |        *  
*          ----------------   |__|      |_______|        *
*                                                        *
*           ______________     __        _______         *
* Date 4:  | July      |\/|   |  |      |       |        *  
*          ----------------   |__|      |_______|        *
*                                                        *
*                 _______________  _________  __________ *
*  Is Leap Year?  |             |  |       |  |        | *  
*      ___        |  Calc Date  |  | Clear |  |  Exit  | *  
*     |___|       |_____________|  |_______|  |________| *
*                                                        *
*                                                        *
**********************************************************
The code:
Code:
Option Explicit
Dim strDay1 As String
Dim strDay2 As String
Dim strDay3 As String
Dim strDay4 As String
Dim strText1 As String
Dim strText2 As String
Dim strText3 As String
Dim strText4 As String
Dim strStartDay As String

Private Sub cmdCalc_Click()
    If chkBox = False Then
        strStartDay = CStr("January 1, 1999")
        If txt1.Text = "" Then
        Else
            strDay1 = CStr(lst1.Text) + " " + CStr(txt1.Text) + ", " + CStr("1999")
            strText1 = DateDiff("d", strStartDay, strDay1) + 1
            lbl1.Caption = "The Julian Day for " + lst1.Text + " " + txt1.Text + " is " + strText1 + "."
        End If
        
        If txt2.Text = "" Then
        Else
            strDay2 = CStr(lst2.Text) + " " + CStr(txt2.Text) + ", " + CStr("1999")
            strText2 = DateDiff("d", strStartDay, strDay2) + 1
            lbl2.Caption = "The Julian Day for " + lst2.Text + " " + txt2.Text + " is " + strText2 + "."
        End If
        
        If txt3.Text = "" Then
        Else
            strDay3 = CStr(lst3.Text) + " " + CStr(txt3.Text) + ", " + CStr("1999")
            strText3 = DateDiff("d", strStartDay, strDay3) + 1
            lbl3.Caption = "The Julian Day for " + lst3.Text + " " + txt3.Text + " is " + strText3 + "."
        End If
        
               
        If txt4.Text = "" Then
        Else
            strDay4 = CStr(lst4.Text) + " " + CStr(txt4.Text) + ", " + CStr("1999")
            strText4 = DateDiff("d", strStartDay, strDay4) + 1
            lbl4.Caption = "The Julian Day for " + lst4.Text + " " + txt4.Text + " is " + strText4 + "."
        End If
    Else
        strStartDay = CStr("January 1, 2000")
        If txt1.Text = "" Then
        Else
            strDay1 = CStr(lst1.Text) + " " + CStr(txt1.Text) + ", " + CStr("2000")
            strText1 = DateDiff("d", strStartDay, strDay1) + 1
            lbl1.Caption = "The Julian Day for " + lst1.Text + " " + txt1.Text + " is " + strText1 + "."
        End If
    
        If txt2.Text = "" Then
        Else
            strDay2 = CStr(lst2.Text) + " " + CStr(txt2.Text) + ", " + CStr("2000")
            strText2 = DateDiff("d", strStartDay, strDay2) + 1
            lbl2.Caption = "The Julian Day for " + lst2.Text + " " + txt2.Text + " is " + strText2 + "."
        End If
                 
        If txt3.Text = "" Then
        Else
            strDay3 = CStr(lst3.Text) + " " + CStr(txt3.Text) + ", " + CStr("2000")
            strText3 = DateDiff("d", strStartDay, strDay3) + 1
            lbl3.Caption = "The Julian Day for " + lst3.Text + " " + txt3.Text + " is " + strText3 + "."
        End If
                 
        If txt4.Text = "" Then
        Else
            strDay4 = CStr(lst4.Text) + " " + CStr(txt4.Text) + ", " + CStr("2000")
            strText4 = DateDiff("d", strStartDay, strDay4) + 1
            lbl4.Caption = "The Julian Day for " + lst4.Text + " " + txt4.Text + " is " + strText4 + "."
        End If
        
    End If

End Sub

Private Sub cmdClear_Click()
    lbl1.Caption = ""
    lbl2.Caption = ""
    lbl3.Caption = ""
    lbl4.Caption = ""
    txt1.Text = ""
    txt2.Text = ""
    txt3.Text = ""
    txt4.Text = ""
    lst1.ListIndex = Month(Date) - 1
    lst2.ListIndex = Month(Date) - 1
    lst3.ListIndex = Month(Date) - 1
    lst4.ListIndex = Month(Date) - 1
    
    
End Sub

Private Sub Form_Load()
    lst1.AddItem "January"
    lst1.AddItem "February"
    lst1.AddItem "March"
    lst1.AddItem "April"
    lst1.AddItem "May"
    lst1.AddItem "June"
    lst1.AddItem "July"
    lst1.AddItem "August"
    lst1.AddItem "September"
    lst1.AddItem "October"
    lst1.AddItem "November"
    lst1.AddItem "December"
    lst2.AddItem "January"
    lst2.AddItem "February"
    lst2.AddItem "March"
    lst2.AddItem "April"
    lst2.AddItem "May"
    lst2.AddItem "June"
    lst2.AddItem "July"
    lst2.AddItem "August"
    lst2.AddItem "September"
    lst2.AddItem "October"
    lst2.AddItem "November"
    lst2.AddItem "December"
    lst3.AddItem "January"
    lst3.AddItem "February"
    lst3.AddItem "March"
    lst3.AddItem "April"
    lst3.AddItem "May"
    lst3.AddItem "June"
    lst3.AddItem "July"
    lst3.AddItem "August"
    lst3.AddItem "September"
    lst3.AddItem "October"
    lst3.AddItem "November"
    lst3.AddItem "December"
    lst4.AddItem "January"
    lst4.AddItem "February"
    lst4.AddItem "March"
    lst4.AddItem "April"
    lst4.AddItem "May"
    lst4.AddItem "June"
    lst4.AddItem "July"
    lst4.AddItem "August"
    lst4.AddItem "September"
    lst4.AddItem "October"
    lst4.AddItem "November"
    lst4.AddItem "December"
    lst1.ListIndex = Month(Date) - 1
    lst2.ListIndex = Month(Date) - 1
    lst3.ListIndex = Month(Date) - 1
    lst4.ListIndex = Month(Date) - 1
    
End Sub

Private Sub txt1_GotFocus()
    txt1.SelStart = 0
    txt1.SelLength = Len(txt1.Text)

End Sub

Private Sub txt2_GotFocus()
    txt2.SelStart = 0
    txt2.SelLength = Len(txt2.Text)
    
End Sub

Private Sub txt3_GotFocus()
    txt3.SelStart = 0
    txt3.SelLength = Len(txt3.Text)
    
End Sub

Private Sub txt4_GotFocus()
    txt4.SelStart = 0
    txt4.SelLength = Len(txt4.Text)
    
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top