I have a dropdown which I have to populate with the last 5 Mondays, but I need to set the Monday of the previos week set as default
I have the following code to create a list of the last 5 Mondays
Public Sub GetMondays()
'populate the dateselection with the last 5 mondays to show the week starting
Dim dtMondays As New DataTable()
dtMondays.Columns.Add("Date")
Dim i As Integer = 1
Dim count As Integer
While (count < 5)
Dim Day As DateTime = Today.AddDays(-i)
If Day.DayOfWeek = 1 Then
DateSelection.Items.Add(New ListItem(Format(Day, "dd/MMM/yyyy"), Day))
count = count + 1
End If
i += 1
End While
End Sub
If today is the 26th of May 2014 I would see the 10th of May and the following full list in the dropdown:
19/05/2014 > Displayed
12/05/2014
05/05/2014
28/04/2014
21/04/2014
If today is the 27th of May 2014 I would see the 10th of May and the following full list in the dropdown:
26/05/2014 > Displayed
19/05/2014 > This one should be displayed
12/05/2014
05/05/2014
28/04/2014
What I need is to keep the order as per above, have the previous Monday selected in the dropdown as per above.
Any ideas on how to do this
Thanks
Compare week number for today and the first item in the list
GetMondays()
Dim c As Calendar = DateTimeFormatInfo.CurrentInfo.Calendar
If c.GetWeekOfYear(Today, CalendarWeekRule.FirstDay, DayOfWeek.Monday) _
= c.GetWeekOfYear(DateSelection.Items(0).Text, CalendarWeekRule.FirstDay, DayOfWeek.Monday) Then
DateSelection.SelectedIndex = 1
Else
DateSelection.SelectedIndex = 0
End If
if week number is the same (e.g. "27/05/2014" and "26/05/2014" will have same week number) then change selection to the second item (SelectedIndex = 1)
P.S.
You can also try more efficient code to populate the list. It does not need to enumerate each day in 4 weeks to find Mondays. You only need to find last Monday and then you can use AddDays(-7) to get a week back.
'Dim count As Integer
'While (count < 5)
' Dim Day As DateTime = (Today.AddDays(1)).AddDays(-i)
' If Day.DayOfWeek = 1 Then
' 'DateSelection.Items.Add(New ListItem(Format(Day, "dd/MMM/yyyy"), Day))
' Console.WriteLine(Format(Day, "dd/MMM/yyyy"), Day)
' count = count + 1
' End If
' i += 1
'End While
Dim Day As Date = Today.AddDays(-(Today.DayOfWeek - DayOfWeek.Monday))
For count As Integer = 1 To 4
DateSelection.Items.Add(New ListItem(Format(Day, "dd/MMM/yyyy"), Day))
Day = Day.AddDays(-7)
Next
Related
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])
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
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
I would like to specify a number that specifies the day of week and then have ASP get the upcoming date for that week day specified.
Example:
Dim xWeekDay
xWeekDay=1 ' <-- 1 would be a Monday...and Sunday would be 7
Dim NextDdate
NextDdate= ???? <-- I want to calculate and show the Upcoming Date here
So the above line would look like this when it's populated.
NextDdate=7/1/2013
Try this:
today = Weekday(Date, vbMonday)
If xWeekDay > today Then
NextDate = Date + (xWeekDay - today)
Else
NextDate = Date + (xWeekDay + 7 - today)
End If
Weekday(Date, vbMonday) is the number of the currend day of the week (with Monday being set as the first weekday). If xWeekDay is in the future (xWeekDay > today), then the next occurrence is xWeekDay - today days away. Otherwise it's xWeekDay + 7 - today days away. Add that difference to the current date and you have the date you're looking for.
I need to create an unordered list of dates for a news items archive... It should look like this
2011
Dec
Nov
etc..
2010
Dec
Nov
etc...
Older
Here is what I have so far..
Dim StartYear As DateFormat = Date.Now.Year
Dim EndYear As DateFormat = Date.Now.Year - 2
ltlArchives.Text = "<ul id=""ArchivesYears"">"
For StartYear = StartYear To EndYear Step -1
ltlArchives.Text = ltlArchives.Text + "<li>" + StartYear.ToString + "</li>"
Next
ltlArchives.Text = ltlArchives.Text + "<li>Other</li>"
ltlArchives.Text = ltlArchives.Text + "</ul>"
I can carry on with this adding in the for loops for months nested under each year however it doesn't seem very practical and will generate links for months even if there are no news item entries...
Is there a way I can build this tree automatically and only include the years/months that have news entries. I can pull a list of SQL Server timestamps from the DB for all the news items and then would like to populate the list based on that...
Can someone point me in the right direction?
You need to find out years and months with news item from SQL Server - say using distinct query. For example, assuming PostedDate column indicates entry date for new item
SELECT DISTINCT
datepart(year, n.PostedDate) as Year,
datepart(month, n.PostedDate) as Month,
FROM
dbo.NewsItem n
ORDER BY
Year desc, Month desc
This query will give you years and months that have news items. Now bind it with say repeater control to get what you want.
You could select out the distinct year, month values from the database and have the query order it for you.
SELECT DISTINCT DATEPART(YEAR, DateCreated), DATEPART(MONTH, DateCreated)
FROM [Blah]
ORDER BY 1 DESC, 2 DESC
When you loop thru the result remember the "previous year" in a variable and only display the year if it has changed (i.e. previousYear doesn't equal the row you are looking at)
Some simple code using a fictional YearMonth class just for demonstration
dim ltlArchives as string
ltlArchives = "<ul id=""ArchivesYears"">"
dim prevYear as Int32 = 0
For Each month as YearMonth in months
If (prevYear <> month.Year) Then
ltlArchives = ltlArchives + "<li>" + month.Year.ToString() + "</li>"
End If
ltlArchives = ltlArchives + "<li>" + month.Month.ToString() + "</li>"
prevYear = month.Year
Next
ltlArchives = ltlArchives + "</ul>"