Julian DateThe next program will use an array with the number of days in each month. When a date is selected from the DateTimePicker datetimepicker, we calculate and display the day of the year. This is done by starting with the day of the month, then add the number of days in each previous month. For example if the date selected is May 21, we would start with a total of 21, then add the number of days in January, February, March, and April. One little problem is leap year. We use a function called IsDate and test if 2/29 is a date for the year selected. If so, we set the number of days in February to 29, otherwise it is 28.

  1. Start a new Windows application and name it Julian. Add a DateTimePicker datetimepicker, control to the form. (It will be named DateTimePicker1.)
  2. Add a Label and name it LblJulian.
  3. Write the code as shown below:
Public Class Form1
    Dim Days() As Integer = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

    Private Sub DateTimePicker1_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        Dim Month As Integer = DateTimePicker1.Value.Month
        Dim Julian As Integer = DateTimePicker1.Value.Day
        Dim Year As Integer = DateTimePicker1.Value.Year

        If IsDate("2/29/" & Year) Then Days(2) = 29 Else Days(2) = 28
        Dim M As Integer
        For M = 1 To Month - 1
            Julian = Julian + Days(M)
        Next M
        Me.Text = "Day " & Julian
    End Sub 'DateTimePicker1_ValueChanged
End Class

Experiment: Add an array with the names of the months in Spanish or some other language. When a date is selected display the name of the month in the other language.

Experiment: Using the IsDate function and a loop print a list of all years that were leap years from 1900 to the present.


INDEX, Scalar Variables vs. Arrays, Example 1: Initialize, Out of Bounds Exception, Arrays of Objects, Julian Date, ReDim: Change the Size of the Array, No Duplicates, Collections
Next lesson: Strings in Visual Basic

Copyright © Zebra0.com
All rights reserved worldwide.

 
 

Julian Date