Reply to topic
Return Dates in Range - ASP.NET
MBMunday


Joined: 06 Apr 2004
Posts: 76
Location: Dallas/Fort Worth
Reply with quote
I have been trying to figure this out for a while.........I've Googled and Yahoo'd to no avail.

I would like to have .NET return dates from a specified start and end date.

Here's what I've got.......A user will enter a start date in a text box and an end date in a text box (and other data that will be entered into a database, which isn't important to my question)

I then want .NET to determine the dates within the date range entered and conduct a loop to enter a record for each date (From the start date to the end date, and all of the dates in between those two).

Josh..........I'm thinkin you can give me a good swift kick in the a%$ to get me going in the right direction. Laughing

Thanks in advance,

MB
Josh
Forum Regular

Joined: 01 Apr 2004
Posts: 1029
Location: Felton, Delaware
Reply with quote
hehe... i've already done this before... even made it take out weekends and holidays Wink Lemme find the code and I'll send it your way.
Josh
Forum Regular

Joined: 01 Apr 2004
Posts: 1029
Location: Felton, Delaware
Reply with quote
Found it... here you go Smile Keep in mind... it's currently configged to remove weekends, but it leaves holidays (I hadn't yet worked that in). It's not "complete", but does work to an extent. It's also something that I did a long time ago, so ignore any sloppy stuff. Last, I never finished it, but It does everything you asked for so have at it!!!

TIMESHEET.ASPX
Code:
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<script runat="server">

' PAGE LOAD (REPLACES SUB MAIN())
'===============================================
Sub Page_Load()
   If Page.IsPostBack Then ' If page has been reposted do this...
   
   Else ' If it has not then do this...
   
   End If
   
   ' Anything that happens regardless of whether the page is posted back or not goes below.
   ' code
End Sub

' LOADS ALL VALID WORKDAYS INTO DLDATE (DROPDOWNLIST)
'===============================================
Sub btnStart_Click(ByVal Sender As Object, ByVal E As EventArgs)
   
   ResetControls()
   
   Dim Valid As Boolean = False                           ' Determines if the rest of the worksheet is visible...
   Dim Cnt As Integer = 0                                 ' Counter...
   Dim Days As Integer = DateDiff("d", txtStart.Text, txtEnd.Text) ' How many days are in the span provided...
   Dim Start As Date = txtStart.Text                        ' The start date...
   'Dim Sat As String = "Saturday"                           ' Short version of Saturday...
   'Dim Sun As String = "Sunday"                           ' Short version of Sunday...
   
   Try
      For Cnt = 0 To Days
         Dim D As Date = Start.AddDays(Cnt)                  ' Deternimes the date to be loaded into into ddlDate.
         Dim WD As Integer = Weekday(D)
         'Dim WD As String = WeekdayName(Weekday(D))            ' Determines the day of the week that this date is.
         If (chkRemWeekends.Checked) Then
            If (NOT(WD = 1) AND NOT(WD = 7)) Then ddlDate.Items.Add(D)
            'If (NOT(WD = Sat) AND NOT(WD = Sun)) Then ddlDate.Items.Add(D) ' If you want weekends removed from ddlDate, this skips a Sat or Sun...
         Else
            ddlDate.Items.Add(D)
         End If
      Next Cnt                                       ' Adds 1 to Cnt and continues the For loop...
      Valid = True                                    ' Everything completed successfully, info is valid...
   Catch Exc As Exception                                 ' Catches Errors and stores them in the variable Exc
      response.Write(Exc.ToString.Replace(Environment.NewLine(), "<br />")) ' Writes error to the top of the page
      Valid = False                                    ' If theres an error, page is NOT valid...
   End Try
   
   plcWorksheet.Visible = Valid                           ' Determines whether the rest of the worksheet gets displayed...
   
End Sub

' LOADS ALL VALID WORKDAYS INTO DLDATE (DROPDOWNLIST)
'===============================================
Sub btnAdd_Click(ByVal Sender As Object, ByVal E As EventArgs)
   
   lbDate.Items.Add(ddlDate.SelectedItem.Text)
   lbIn.Items.Add(txtIn.Text)
   lbLunchStart.Items.Add(txtLunchStart.Text)
   lbLunchEnd.Items.Add(txtLunchEnd.Text)
   lbOut.Items.Add(txtOut.Text)
   lbTotalHrs.Items.Add(AddHours())
   ddlDate.Items.RemoveAt(ddlDate.SelectedIndex)
   
   lbDate.Rows = lbDate.Items.Count
   lbIn.Rows = lbIn.Items.Count
   lbLunchStart.Rows = lbLunchStart.Items.Count
   lbLunchEnd.Rows = lbLunchEnd.Items.Count
   lbOut.Rows = lbOut.Items.Count
   lbTotalHrs.Rows = lbTotalHrs.Items.Count
   
   If ddlDate.Items.Count = 0 Then btnAdd.Enabled = False
   
End Sub

' ADDS ALL SUPPLIED HOURS UP TO GET THE TOTAL HOURS
'===============================================
Function AddHours() As String
   Dim TotalHours as string
   Dim tIn as Date = ddlDate.SelectedItem.Text & " " & txtIn.Text
   
   return TotalHours
End Function

' SETS ALL LISTBOXES TO THE SAME HIGHLIGHTED INDEX
'===============================================
Sub ListBox_Click(ByVal Sender As Object, ByVal E As EventArgs)

   Dim idx as integer = Sender.SelectedIndex
   
   lbDate.SelectedIndex = idx
   lbIn.SelectedIndex = idx
   lbLunchStart.SelectedIndex = idx
   lbLunchEnd.SelectedIndex = idx
   lbOut.SelectedIndex = idx
   
End Sub

' RESETS FORM TO INITIAL STATE
'===============================================
Sub btnReset_Click(ByVal Sender As Object, ByVal E As EventArgs)

   ResetControls()
   
End Sub

' RESETS FORM TO INITIAL STATE
'===============================================
Sub ResetControls()
   
   dim NumRows as Integer = 1                              ' # of rows to reset listboxes to...
   With ddlDate
      .Items.Clear
   End With
   
   With lbDate
      .Items.Clear
      .Rows = NumRows
   End With
   
   With lbIn
      .Items.Clear
      .Rows = NumRows
   End With
   
   With lbLunchStart
      .Items.Clear
      .Rows = NumRows
   End With
   
   With lbLunchEnd
      .Items.Clear
      .Rows = NumRows
   End With
   
   With lbOut
      .Items.Clear
      .Rows = NumRows
   End With
   
   plcWorksheet.Visible = False
   btnAdd.Enabled = True
   
End Sub

</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Timesheet</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form runat="server" id="frmPage">
<table width="760" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="33%">Start Date</td>
    <td width="33%">End Date</td>
    <td width="34%"><asp:CheckBox ID="chkRemWeekends" runat="server" Text="Remove weekends from date list?" /></td>
  </tr>
  <tr>
    <td><asp:TextBox ID="txtStart" runat="server" /></td>
    <td><asp:TextBox ID="txtEnd" runat="server" /></td>
    <td><asp:button ID="btnStart" runat="server" Text="Start" OnClick="btnStart_Click" />
      <asp:button ID="btnReset" runat="server" Text="Reset" OnClick="btnReset_Click" /></td>
  </tr>
</table>
<asp:PlaceHolder ID="plcWorksheet" runat="server" Visible="false">
<hr width="760px" align="left" />
<table width="760" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>INPUT</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>Date</td>
    <td>In</td>
    <td>Lunch Start </td>
    <td>Lunch End </td>
    <td>Out</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td><asp:DropDownList ID="ddlDate" runat="server" Width="110px" Enabled="false" /></td>
    <td><asp:TextBox ID="txtIn" runat="server" width="100px" /></td>
    <td><asp:TextBox ID="txtLunchStart" runat="server" width="100px" /></td>
    <td><asp:TextBox ID="txtLunchEnd" runat="server" width="100px" /></td>
    <td><asp:TextBox ID="txtOut" runat="server" width="100px" /></td>
    <td><asp:button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td width="110px">Date</td>
    <td width="110px">In</td>
    <td width="110px">Lunch Start </td>
    <td width="110px">Lunch End </td>
    <td width="110px">Out</td>
    <td>Total Hours </td>
  </tr>
  <tr>
    <td><asp:ListBox ID="lbDate" runat="server" width="110px" SelectionMode="Single" OnSelectedIndexChanged="ListBox_Click" AutoPostBack="true" /></td>
    <td><asp:ListBox ID="lbIn" runat="server" width="110px" SelectionMode="Single" Enabled="false" /></td>
    <td><asp:ListBox ID="lbLunchStart" runat="server" width="110px" SelectionMode="Single" Enabled="false" /></td>
    <td><asp:ListBox ID="lbLunchEnd" runat="server" width="110px" SelectionMode="Single" Enabled="false" /></td>
    <td><asp:ListBox ID="lbOut" runat="server" width="110px" SelectionMode="Single" Enabled="false" /></td>
    <td><asp:ListBox ID="lbTotalHrs" runat="server" width="110px" SelectionMode="Single" Enabled="false" /></td>
  </tr>
</table>
</asp:PlaceHolder>
</form>
</body>
</html>
Josh
Forum Regular

Joined: 01 Apr 2004
Posts: 1029
Location: Felton, Delaware
Reply with quote
I'm sure this did way more than you needed it to do, but did it at least send you in the right direction? Let me know... because if not I'll give you a hand getting things straight. Wink
MBMunday


Joined: 06 Apr 2004
Posts: 76
Location: Dallas/Fort Worth
Reply with quote
Thank you, thank you, thank you. I've been in a school the past couple of days and haven't been into the office. I'll work on it tomorrow and let you know how things go.

Again, thanks Josh!

MB
MBMunday


Joined: 06 Apr 2004
Posts: 76
Location: Dallas/Fort Worth
Reply with quote
I finally got the opportunity to really sit down and dig in. Thanks again! It worked great. You commented your code great and I was able to modify it to do what I needed.

Again, thank you, thank you, thank you!

Regards,

MB
Josh
Forum Regular

Joined: 01 Apr 2004
Posts: 1029
Location: Felton, Delaware
Reply with quote
Good Deal!!! That's what I like to hear. If you need anything else lemme know.
MBMunday


Joined: 06 Apr 2004
Posts: 76
Location: Dallas/Fort Worth
Reply with quote
I'd show you the page I ended up creating using the altered code you provided me, but it's on an Intranet at my work. People there won't appreciate it as much. Confused

It's too bad.........

Thanks again.

MB
Return Dates in Range - ASP.NET
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic