asp.net timespan - 0.5 (half day) - asp.net

I've been using the following timespan function to work out the number of days between two dates which works correctly.
Dim dtStart As Date = txtHStart_Date.Text
Dim dtEnd As Date = txtHEnd_Date.Text
Dim ts As TimeSpan = dtEnd - dtStart
txtNoofDays.Text = ts.TotalDays.ToString()
Console.WriteLine(ts.TotalDays)
I've now tried to add a check box (as a half day selection). If the check box is selected I want it to minus 0.5 off the total days. But i'm getting the blue line telling me its an error on "ts = (dtEnd - dtStart) - 0.5"
Dim dtStart As Date = txtHStart_Date.Text
Dim dtEnd As Date = txtHEnd_Date.Text
Dim ts As TimeSpan = dtEnd - dtStart
For Each li As ListItem In CheckBoxList1.Items
If li.Value = "Half Day" Then
ts = (dtEnd - dtStart) - 0.5
Else
ts = dtEnd - dtStart
End If
Next
txtNoofDays.Text = ts.TotalDays.ToString()
Console.WriteLine(ts.TotalDays)
Any suggestions on how to correct

ts is your TimeSpan value. You can't subtract 0.5 from a TimeSpan - I know what you want it to mean in this case, but why would "days" be the right unit by default? You can subtract 0.5 days explicitly though:
ts = (dtEnd - dtStart) - TimeSpan.FromDays(0.5)
It's not clear why you're iterating over all the items but only actually using the last item, by the way. If your plan was actually to use the number of days for each item, I'd be tempted to do the subtraction after working out the number of days:
Dim days = (dtEnd - dtStart).TotalDays
If li.Value = "Half Day" Then
days = days - 0.5
End If
' Use days here

Related

Get days every other weeks using Enumerable.Range

I need to get value using Enumerable.Range in every two weeks. In this code it get the date in every weeks from selected days as i mention below on the code i select every Tuesday and it return all Tuesday in a month but i need every two weeks of the month and i don't know how to do it. Please i need help.
Dim datesThatAreSundays As IEnumerable = Enumerable.Range(start.DayOfYear, [end].Subtract(start).Days + 1).[Select](Function(n) start.AddDays(n - start.DayOfYear)).Where(Function(d) d.DayOfWeek = DayOfWeek.Tuesday)
Why do you need Enumerable.Range?
Public Iterator Function TheDayEveryTwoWeeks(ByVal TheDay As DayOfWeek, ByVal [start] As Date, ByVal [end] As Date) As IEnumerable(Of Date)
Dim closest_day = [start].AddDays((TheDay - [start].DayOfWeek + 7) Mod 7)
Dim steps_at_most = CType(([end] - closest_day).TotalDays, Integer) \ 14 + 1
For i As Integer = 0 To steps_at_most
Dim d As Date = closest_day.AddDays(i * 14)
If d <= [end] Then
Yield d
Else
Exit Function
End If
Next
End Function
Dim datesThatAreTuesdays = TheDayEveryTwoWeeks(DayOfWeek.Tuesday, [start], [end])

Between 2 dates, & detecting todays date (if in between range)

Trying another attempt at this question:
With the below code, I'm trying to articulate a date range; May 10 - June 8th. I'm outputting a unique thumbnail image per date in this range (coming together as a stylized calendar of sorts); I need to also detect, within this range, today's date. As a .today style class will be appending for today's date. I achieved this previously when the date range was just within one month, 1 - 31 (and that code is under the most recent attempt / which is original code) this same code could not work because now it's not as simple as 1 - 31 and declaring the month name statically, now it's two months and 10 - 31 and then 1 - 8. Also, not my most recent attempt fails so hard the page doesn't even compile and is just white.
<%
Dim d1 As New Date(2015, 5, 10)
Dim d2 As New Date(2015, 6, 8)
Dim DaysBetween As Long = DateDiff(DateInterval.Day, d1, d2)
Dim d3 As Date
For d As Long = 0 To DaysBetween
d3 = d1.AddDays(d)
If d3 < Today() Then
time = "past"
ElseIf d3 = Today Then
time = "today"
Else
time = "future"
End If
Dim suffix As String = Suffixer(d3.Day)
response.write("<section id='day_"& i &"' class='calSquare " & time &"'><article class='dateImage' style='background-image: url(images/Calendar_Thumbnails/Day_"&i&".jpg)'></article></article><article class='dateTitle'> "&i&suffix&"</article></section>")
Next
<!--response.write(products(0))-->
%>
Original functional code; articulating one month.
<%
For i = 1 to 31
dim time
If i < day_part Then
time = "past"
ElseIf i = day_part Then
time = "today"
Else
time = "future"
End If
suffix = Suffixer(i)
response.write("<section id='day_"& i &"' class='calSquare " & time &"'><article class='dateImage' style='background-image: url(images/Calendar_Thumbnails/Day_"&i&".jpg)'></article></article><article class='dateTitle'>May "&i&suffix&"</article></section>")
Next
<!--response.write(products(0))-->
%>
Your first code sample isn't valid VBScript. The language doesn't support constructs like Dim var As type = value, so that's probably why the page isn't displayed.
As for listing the dates within a range while highlighting the current date, you could do something like this:
today = Date
firstDay = DateValue("2015-05-10")
lastDay = DateValue("2015-06-08")
d = firstDay
While d <= lastDay
If d = today Then
response.write "today"
Else
response.write "other day in range"
End If
d = d + 1
Wend

Add Days to Date with 'YYYYMMDD' format

In VB6, I am trying to add days to a date which is in the format 'YYYYMMDD'. I can add days like this:
Pull_Date = Need_Date + Val(txtLeadTime.Text)
which works, until the resulting days is greater than the number of days in the month. I tried using DateAdd, but it doesn't accept the YYYYMMDD format - neither does CDate.
You need to convert your date string to a date so you can use the DateAdd function:
Dim Need_Date As String
Dim Pull_Date As Date
Dim tmpDate As Date
Need_Date = "20141113"
tmpDate = CDate(Mid$(Need_Date, 5, 2) & "/" & Right$(Need_Date, 2) & "/" & Left$(Need_Date, 4))
Pull_Date = DateAdd("d", Val(txtLeadTime.Text), tmpDate)
MsgBox Format$(Pull_Date, "yyyymmdd") '// if LeadTime is 25 days, displays 20141208
Please note, parsing your Need_Date by character position can blow up if it's not formatted exactly as expected.
After digging deeper into this old code, I found that a conversion function was already created:
Public Function Date_Format_YYYYMMDD(ByVal sDate As String) As String
If IsDate(sDate) Then
sDate = Format(sDate, "YYYYMMDD")
End If
Date_Format_YYYYMMDD = sDate
End Function
With this function, it's really easy:
Pull_Date = Date_Format_YYYYMMDD(Need_Date) + Val(txtLeadTime.Text)
The only thing I don't understand is why
Pull_Date = Format(Need_Date, "YYYYMMDD") + Val(txtLeadTime.Text)
doesn't work. Seems like it's doing the same thing as the function.

VBA Excel create hour column (like in a calendar) according to given time resolution

I need to create some sort of a calendar in VBA.
I need to create a column of hours. The time difference between 2 adjacent cells is determined by an integer read from a text file, which represents the time resolution in minutes.
For example - if Res = 60, the hour column should look like this:
12:00
13:00
14:00
...
if Res = 30, the hour column should look like this:
12:00
12:30
13:00
13:30
14:00
....
I've calculated the number of cells according to the given resultion (if Res = 60, nCells = 24, if Res = 30 nCells = 48 and so on). I just don't know how to create the hour column (in VBA code of course).
Thanks,
Li
You could use DateAdd to increment dates: http://www.techonthenet.com/excel/formulas/dateadd.php
Sub createTimeColumn()
intIncr = 60 'minutes to add each cell
intCellCnt = 1440 / intIncr '24h * 60m = 1440 minutes per day
datDate = CDate("01/11/2013 06:00:00") 'start date+time for first cell
For i = 1 To intCellCnt 'loop through n cells
Cells(i, 1) = Format(datDate, "hh:mm") 'write and format result
datDate = DateAdd("n", intIncr, datDate) 'add increment value
Next i
End Sub
Result will look like
You need a simple loop to which you pass the start range, begin & end time and increment. I recommend to strictly work with dates/times; the output range should be formatted as time
Sub CallTest()
FillIt [A1], #12:00:00 PM#, #1:00:00 PM#, #12:10:00 AM#
End Sub
Sub FillIt(RStart As Range, TStart As Date, TEnd As Date, Inc As Date)
Dim Idx As Integer, TLoop
Idx = 1
TLoop = TStart
Do
RStart(Idx, 1) = TLoop
TLoop = TLoop + Inc
Idx = Idx + 1
Loop Until TLoop > TEnd + #12:00:01 AM# ' need to add 1 second to really
' break the loop where we want
End Sub
Don't worry about the somewhat strange looking Inc parameter .... in VBA editor just enter #0:10:0# ... it will automatically expand to a full 24 hrs AM/PM notation.
The 1 second in the Loop Until is added because I found that the loop is left 1 pass too early (it seems that within the loop #16:0:0# < #16:0:0# resolves to True)
Public Sub MakeTime(RangeA As Range, iRes As Long)
Dim dDate As Date
Dim rCell As Range
Dim X As Variant
Set rCell = RangeA
dDate = CDate(RangeA.Value)
Do
dDate = DateAdd("n", iRes, dDate)
Set rCell = rCell.Offset(1, 0)
rCell.Value = dDate
Loop Until DateDiff("h", CDate(RangeA.Value), dDate) >= 24
End Sub
Sub test()
Call MakeTime(Sheet1.Range("A1"), 45)
End Sub
They beat me to it... But since I've already written a routine... Might as well post it :)
Try this in a new workbook
Sub Main()
' ask for column input
Dim myColumn As String
myColumn = InputBox("Please enter the column letter where the hours will be stored")
' Clear the column
Columns(myColumn & ":" & myColumn).ClearContents
' initial hour
Dim firstHour As String
firstHour = InputBox("Please enter the start time in the hh:mm format i.e. 12:00")
' interval
Dim interval As Long
interval = CLng(InputBox("Please enter the interval in minutes"))
' duration
Dim duration As Long
duration = CLng(InputBox("Please enter the duration (hrs)"))
' apply formatting to column
Columns(myColumn & ":" & myColumn).NumberFormat = "hh:mm;#"
' enter the initial time into cell
Range(myColumn & 1) = CDate(firstHour)
' fill in remaining hours / interval
Dim i As Long
For i = 1 To (60 / interval) * duration
Range(myColumn & 1).Offset(i, 0) = DateAdd("n", interval, CDate(Range(myColumn & 1).Offset(i - 1, 0)))
Next i
End Sub

Checking the values of value of concated values of multiple dropdowns in VB.Net

I have 4 fields as:
• calStartDate = Control for the start Date Calendar (drop downs for Day, Month and Year)
• ddlTime = Start time drop down
• calEndDate = Control for the End Date Calendar (drop downs for Day, Month and Year)
• ddlTime2 = End time drop down
Now I have managed to save the following values in DB as;
• STARTDATETIME = “2/24/2012 6:30:00 AM”
• ENDDATETIME = “2/24/2013 6:30:00 AM”
As In the following code;
Dim EndDateTime As DateTime
Dim StartDateTime As DateTime
If chkbxAccAppBasedOnTimeInterval.Checked = True Then
Dim StartDate As System.DateTime? = calStartDate.CurrentDate
If StartDate.HasValue Then
StartDateTime = StartDate.Value.AddMinutes(CType(ddlTime.SelectedValue, Double))
EditRelTbl.STARTDATETIME = StartDateTime
End If
Dim EndDate As System.DateTime? = calEndDate.CurrentDate
If EndDate.HasValue Then
EndDateTime = EndDate.Value.AddMinutes(CType(ddlTime2.SelectedValue, Double))
EditRelTbl.ENDDATETIME = EndDateTime
End If
If EndDateTime > StartDateTime Then
DisplayAlert("End date time, must be less than Start Date and time", "Warning")
EndDateTime = StartDateTime
End If
Else
EditRelTbl.STARTDATETIME = Nothing
EditRelTbl.ENDDATETIME = Nothing
End If
Issue is that I have to check that STARTDATETIME must be greater than ENDDATETIME and if the End dateTime is greater than Start DateTime then the fill the End datetime as Start DateTime as I tried to done in the code
If EndDateTime > StartDateTime Then
DisplayAlert("End time must be greater than Start time", "Warning")
EndDateTime = StartDateTime
End If
But this not checking the check I want. Can anyone guide me how to perform the check, or where I am mistaken.
Note: ‘EditRelTbl’ is the table, where I am saving the values of End/Start date time.

Resources