For this week Around this time : show this to-do list

Xancholy

Well-known member
Joined
Aug 29, 2006
Messages
143
Programming Experience
Beginner
Can someone please show me how to properly code for the following logic. I can't seem to figure this one out.

I am trying to get my app to show a weekly TO DO list for THIS week. (this week Mon -> Sun night)

VB.NET:
        ' check if we should open this week's list NOT last week's list

IF today() is between last Monday & this Monday AND _

    if time () is between 12 midnight AND 1pm then 'check if we should show morning or evening shift schedule

            Me.datagridview1.datasource = Sql

        End If

Thanks for any help
 
sql?
between this monday and last monday is NOT a week. Between is inclusive, so monday - monday also includes all events occurring at midnight on both days. I think you mean

last monday <= today < next monday

if today is monday, then it qualifies as last_monday, not next monday

Also, thanks for saying what database youre using, makes my job a lot easier. Here is what you'd do for oracle:

SELECT * FROM schedule WHERE

sched_date >= trunc(sysdate) - MOD(to_char(sysdate, 'D') + 5, 7) AND
sched_date < trunc(sysdate) + (7 - MOD(to_char(sysdate, 'D') + 5, 7))

to_char(sysdate, 'D') gives the day of the week. sun = 1, mon = 2 etcMOD ensures our values cycle

if its monday:
sched date >= trunc(sysdate) - MOD(2+5,7) AND sched_date < trunc(sysdate) + (7 - MOD(2+5,7))
sched date >= trunc(sysdate) - 0 AND sched_date < trunc(sysdate) + (7 - 0)

MOD(x+5,7) where x is day of week:
2->0
3->1
4->2
5->3
6->4
7->5
1->6

Its the last case that makes just normal math impossible. no arithmetic addition of a constant can reduce number in a sequence by 2, apart from the last one which it increases by 5
 
Thanks. I will try your sql solution as well.

I found a good working solution here
First add this class to your project

VB.NET:
Public Structure TimeRange

        ' Store the range as TimeSpans as the comparisons are "easier"
        Private ReadOnly m_start As TimeSpan
        Private ReadOnly m_finish As TimeSpan

        ' used to handle special case of the range spanning midnight
        Private ReadOnly m_midnight As Boolean

        Public Sub New(ByVal start As DateTime, ByVal finish As DateTime)
            m_start = start.TimeOfDay
            m_finish = finish.TimeOfDay
            m_midnight = (TimeSpan.Compare(m_start, m_finish) > 0)
        End Sub

        Public ReadOnly Property Start() As DateTime
            Get
                Return DateTime.MinValue.Add(m_start)
            End Get
        End Property

        Public ReadOnly Property Finish() As DateTime
            Get
                Return DateTime.MinValue.Add(m_finish)
            End Get
        End Property

        Public Function Contains(ByVal value As DateTime) As Boolean
            Dim timeOfDay As TimeSpan = value.TimeOfDay
            If m_midnight Then
                Return TimeSpan.Compare(m_start, timeOfDay) <= 0 OrElse TimeSpan.Compare(timeOfDay, m_finish) <= 0
            Else
                Return TimeSpan.Compare(m_start, timeOfDay) <= 0 AndAlso TimeSpan.Compare(timeOfDay, m_finish) <= 0
            End If
        End Function
    End Structure


Next add this to your form_load

VB.NET:
Public Class Form1

 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Select Case Weekday(Date.Now)

            Case 2 To 7 ‘check if this is a working day

                MessageBox.Show("Today is a working day")

                Dim morningRange As New TimeRange(#12:00:00 AM#, #1:00:00 PM#)

                Dim eveningRange As New TimeRange(#1:01:00 PM#, #8:00:00 PM#)

                If morningrange.Contains(Now) Then

                    MessageBox.Show("It is now the Morning Shift")

'now show the morning shift for dates between Day 1 and Day 6
                End If

 

                If eveningRange.Contains(Now) Then

                    MessageBox.Show("It is now the Evening Shift")
'now show the evening shift for dates between Day 1 and Day 6
                End If

 

            Case 1 ‘check if it is a day off

                MessageBox.Show("Today is Sunday")

 

        End Select

    End Sub
End Class
 
I think the person who wrote that class doesnt understand the difference between TimeSpan and DateTime

TimeRange IS a TimeSpan.. a span of time..

Oh well.. if it works.
 
er.. i'd get the db to do it, after all you seem to be using LINQ and it should be able to handle it.. ?
 

Latest posts

Back
Top