Solved

how to find last sunday in march - help with british summer time

Posted on 2007-03-26
3
350 Views
Last Modified: 2012-05-05
Hi

I have a function that works out the start and end of british summer time.It's worked on previous years but this year it doesnt work. This function says the start of BST is 1/4/2007 this year but the start of bst this year is 25/3/2007. The start of bst is always the last sunday in march.

Please can you show me how to calculate the date that is the alst sunday in march given the year or helpme modify this function i already have

thanks a lot
andrea

    Public Shared Function BSTStart(ByVal year As Integer) As Date
        ' This function returns the start date for BST, given the year
        ' Daylight savings always starts on the last Sunday of March
        ' Written 20 Sep 2004 by GG Certain
        ' Modified for BST 19 Aug 2005

        Const april As Integer = 4
        ' Set the date to April 1st
        Dim dateTemp As Date = New Date(year, april, 1)
        Dim daySpan As TimeSpan = New TimeSpan(1, 0, 0, 0, 0)
        Do While dateTemp.DayOfWeek <> DayOfWeek.Sunday
            dateTemp = dateTemp.Subtract(daySpan)
        Loop
        Return dateTemp
    End Function

    Public Shared Function BSTEnd(ByVal year As Integer) As Date
        ' This function returns the end date for BST, given the year
        ' BST always ends on the last Sunday in October
        ' Written 20 Sep 2004 by GG Certain

        Const october As Integer = 10
        ' Set the date to October 31st
        Dim dateTemp As Date = New Date(year, october, 31)
        Dim daySpan As TimeSpan = New TimeSpan(1, 0, 0, 0, 0)
        Do While dateTemp.DayOfWeek <> DayOfWeek.Sunday
            dateTemp = dateTemp.Subtract(daySpan)
        Loop
        Return dateTemp
    End Function
0
Comment
Question by:andieje
3 Comments
 
LVL 27

Accepted Solution

by:
planocz earned 500 total points
ID: 18797479
Try this code...........

FORM 1

Public Class frmDateTypes
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

        TextBox1.Text = BST(3, 2007)
        TextBox2.Text = BST(10, 2007)
    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.TextBox2 = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(8, 32)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(64, 24)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "BST Start:"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(8, 80)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(64, 24)
        Me.Label2.TabIndex = 1
        Me.Label2.Text = "BST End:"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(72, 32)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(64, 20)
        Me.TextBox1.TabIndex = 2
        Me.TextBox1.Text = "TextBox1"
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(72, 80)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.Size = New System.Drawing.Size(64, 20)
        Me.TextBox2.TabIndex = 3
        Me.TextBox2.Text = "TextBox2"
        '
        'frmDateTypes
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(284, 144)
        Me.Controls.Add(Me.TextBox2)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Name = "frmDateTypes"
        Me.Text = "frmDateTypes"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Public Shared Function BST(ByVal Mth As Integer, ByVal Yr As Integer) As Date
        Dim dateTemp As Date = New Date(Yr, Mth, 31)
        Dim daySpan As TimeSpan = New TimeSpan(1, 0, 0, 0, 0)
        Do While dateTemp.DayOfWeek <> DayOfWeek.Sunday
            dateTemp = dateTemp.Subtract(daySpan)
        Loop
        Return dateTemp
    End Function
End Class
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now