I have this string that is a date called strRDate and another string called strColor.
The cutoff date is this weeks Monday.
I would like to be something like this:
'// strRDate format is MM/DD/YYYY
Dim strRDate,strColor
strRDate="1/1/1999"
strColor="none"
If strRDate is this weeks Monday or older then <-- HOW DO I DO THIS ???
strColor="green"
else
strColor="red"
end if
So anything older then Oct 21, 2013 would be green, else it would be red.
' for successful parsing of mm/dd/yyyy dates (1033 is EN_US)
Response.LCID = 1033
Dim strRDate, strColor
strRDate = "10/21/2013"
strColor = GetColor(ParseDate(strRDate))
and a few helper functions:
Function GetColor(d)
GetColor = "none"
If IsDate(d) Then
If d <= GetMondayForWeek(Now()) Then
GetColor = "green"
Else
GetColor = "red"
End If
End If
End Function
Function ParseDate(strDate)
ParseDate = vbEmpty
If IsDate(strDate) Then
ParseDate = CDate(strDate)
End If
End Function
Function GetMondayForWeek(d)
' midnight
GetMondayForWeek = CDate(Fix(d))
While Weekday(GetMondayForWeek) <> vbMonday
GetMondayForWeek = GetMondayForWeek - 1
Wend
End Function
I'd probably do the calculation like this:
strRDate = "1/1/1999"
strColor = "none"
monday = Date - (Weekday(Date, vbMonday) - 1)
If CDate(strRDate) <= monday Then
strColor="green"
Else
strColor="red"
End If
Weekday(Date, vbMonday) returns a value between 1 and 7 for each day of the week, with Monday being the first day:
Monday → 1
Tuesday → 2
Wednesday → 3
Thursday → 4
Friday → 5
Saturday → 6
Sunday → 7
Subtract 1 from the return value of the function and you get the difference in days between the current date and the most recent Monday. Subtracting that difference from the current date gives you the date of the most recent Monday, which you can then compare to your input date (use the CDate function to convert the string to an actual date).
This date's monday can be calculated by substracting the Weekday() and adjusting for the Weekday of mondays:
WScript.Echo "german locate (dd.mm.yyyy):"
Dim dtCur : dtCur = #10/10/2013#
Do Until dtCur > #10/24/2013#
Dim dtThisMonday : dtThisMonday = DateAdd("d", -WeekDay(dtCur) + 2, dtCur)
Dim isAfterThisMonday : isAfterThisMonday = dtCur > dtThisMonday
WScript.Echo dtCur, WeekDay(dtCur), WeekdayName(WeekDay(dtCur), True), dtThisMonday, CStr(isAfterThisMonday)
dtCur = DateAdd("d", 1, dtCur)
Loop
output:
german locate (dd.mm.yyyy):
10.10.2013 5 Thu 07.10.2013 True
11.10.2013 6 Fri 07.10.2013 True
12.10.2013 7 Sat 07.10.2013 True
13.10.2013 1 Sun 14.10.2013 False
14.10.2013 2 Mon 14.10.2013 False
15.10.2013 3 Tue 14.10.2013 True
16.10.2013 4 Wed 14.10.2013 True
17.10.2013 5 Thu 14.10.2013 True
18.10.2013 6 Fri 14.10.2013 True
19.10.2013 7 Sat 14.10.2013 True
20.10.2013 1 Sun 21.10.2013 False
21.10.2013 2 Mon 21.10.2013 False
22.10.2013 3 Tue 21.10.2013 True
23.10.2013 4 Wed 21.10.2013 True
24.10.2013 5 Thu 21.10.2013 True
Related
I have a series of arrival and leaving dates. I want to know if those dates overlap a weekend. I could write a custom function to walk each of the days in the interval and see if they are a weekend. Is there a simpler way to do this that will scale better?
I am using lubridate, but I'm happy to use a different date package if that would make my job easier.
How about this base-R solution:
# make a set of sequences from beginning and ending dates and test with:
as.POSIXlt(x)$wday %in% c(0,6)
That would deliver a vector of TRUE/FALSE and you could determine whether any of the items in the sequence were TRUE with:
max( as.POSIXlt(x)$wday %in% c(0,6) )
With a_date as arrival date and d_date ansd departure date, something like this could work:
require(lubridate)
weekend_overlap <- ifelse(wday(a_date) %in% c(1, 7) ||
wday(d_date) %in% c(1, 7) ||
interval(a_date,d_date)/ddays(1) > 4,TRUE,FALSE)
Here is a more general (vectorized) function that checks whether the interval from-to contains a certain weekday (1-7):
#' Check if a weekday is within an interval
#'
#' #param wday Day of week (integer 1-7)
#' #param from Date. Can be a vector.
#' #param to Date. Same length as `from` and must be greater than `from`.
#' #param week_start 1 = Monday. 7 = Sunday
#'
wday_in_interval = function(wday, from, to, week_start = 1) {
if (wday < 1 | weekday > 7)
stop("wday must be an integer from 1 to 7.")
if (week_start)
wday = 1 + (((wday - 2) + week_start ) %% 7) # Translate wday to week_start = 1 (ISO standard)
if (any(from > to, na.rm = TRUE))
stop("`from` must come before `to`")
# If the interval is greater than a week, it trivially contains any weekday
over_a_week = difftime(from, to, units = "days") >= 7
# Check if weekday is both smaller/greater than "from" and "to"
days_from = as.numeric(strftime(from, "%u"))
days_to = as.numeric(strftime(to, "%u"))
contains_weekday = ifelse(
strftime(from, "%V") == strftime(to, "%V"), # Dates are in the same week?
yes = wday >= days_from & wday <= days_to,
no = wday >= days_from | wday <= days_to #
)
return(over_a_week | contains_weekday)
}
Finding out whether an interval includes a weekend is then just a matter of checking that the interval does not include Saturdays OR Sundays:
library(dplyr)
tibble::tibble(
timestamp = seq(as.POSIXct("2020-09-03 0:00"), as.POSIXct("2020-09-8 12: 00"), length.out = 10),
overlaps_saturday = wday_in_interval(6, from = lag(timestamp), to = timestamp),
overlaps_sunday = wday_in_interval(7, from = lag(timestamp), to = timestamp),
overlaps_weekend = overlaps_saturday | overlaps_sunday
)
Result:
# A tibble: 10 x 4
timestamp overlaps_saturday overlaps_sunday overlaps_weekend
<dttm> <lgl> <lgl> <lgl>
1 2020-09-03 00:00:00 NA NA NA
2 2020-09-03 14:40:00 FALSE FALSE FALSE
3 2020-09-04 05:20:00 FALSE FALSE FALSE
4 2020-09-04 20:00:00 FALSE FALSE FALSE
5 2020-09-05 10:40:00 TRUE FALSE TRUE
6 2020-09-06 01:20:00 TRUE TRUE TRUE
7 2020-09-06 16:00:00 FALSE TRUE TRUE
8 2020-09-07 06:40:00 FALSE TRUE TRUE
9 2020-09-07 21:20:00 FALSE FALSE FALSE
10 2020-09-08 12:00:00 FALSE FALSE FALSE
I have a list of dates, which are in this format: YYYY-MM-DD
I would like to be able to sort them in descending order by year (2013, 2012, 2011), and then ascending order by month (January, February, March,...). So what I'm looking for is:
2013-01-01
2013-02-01
2013-03-01
2013-04-01
2013-05-01
2012-01-01
2012-02-01
2012-03-01
...
2012-12-01
2011-01-01
2011-02-01
2011-03-01
...
2011-12-01
Note, the list for the current year would be incomplete until December, so that's why it only goes to 2013-05-01. Previous years would be complete from January-December.
I did a bubble-sort similar to this:
For i = 0 to Ubound(dateArray)
For j = i + 1 to Ubound(dateArray)
if dateArray(i) > dateArray(j) then
tempDate = dateArray(i)
dateArray(i) = dateArray(j)
dateArray(j) = tempDate
end if
Next
Next
but that gives me a list that looks like this:
2011-01-01
2011-02-01
2011-03-01
...
2011-12-01
2012-01-01
2012-02-01
2012-03-01
...
2012-12-01
2013-01-01
2013-02-01
2013-03-01
2013-04-01
2013-05-01
Close, but not quite.
You need to sort ascending when the year is the same in both dates, and descending when the year is different:
Sub SwapValues(ByRef a, ByRef b)
buf = a : a = b : b = buf
End Sub
...
If Year(dateArray(i)) = Year(dateArray(j)) Then
If dateArray(i) > dateArray(j) Then
SwapValues dateArray(i), dateArray(j)
End If
Else
If dateArray(i) < dateArray(j) Then
SwapValues dateArray(i), dateArray(j)
End If
End If
I added a procedure for swapping the values to simplify the code a little.
I've seen this one before... I think this is how I solved it.
For i = 0 to Ubound(dateArray)
For j = i + 1 to Ubound(dateArray)
if (year(dateArray(i)) < year(dateArray(j))) or (year(dateArray(i)) = year(dateArray(j)) and month(dateArray(i)) > month(dateArray(j)))then
tempDate = dateArray(i)
dateArray(i) = dateArray(j)
dateArray(j) = tempDate
end if
Next
Next
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Find the day of a week in R
I have a data for days like 11-01-2011 etc. But I want to add the data corresponding
the date as Monday, Tuesday etc. Is there any R package that contains the information of the dates with days?
weekdays(as.Date('16-08-2012','%d-%m-%Y'))
[1] "Thursday"
The lubridate package is great for this sort of stuff.
> wday(as.Date('16-08-2012','%d-%m-%Y'))
[1] 5
> wday(as.Date('16-08-2012','%d-%m-%Y'), label=TRUE)
[1] Thurs
Levels: Sun < Mon < Tues < Wed < Thurs < Fri < Sat
> wday(as.Date('16-08-2012','%d-%m-%Y'), label=TRUE, abbr = FALSE)
[1] Thursday
Levels: Sunday < Monday < Tuesday < Wednesday < Thursday < Friday < Saturday
Here is some information to create your own library or routine
Constants:
day_of_month
the day of the month
e.g. if input mm-dd-yyy then dd
month:
march = 1
april = 2
may = 3
...
year
yy[yy] (last to digits from yyyy)
*subtract 1 if month jan or feb
e.g. if input date is 02-01-2012 (mm-dd-yyyy)
year = (12-1) = 11
century
[yy]yy (first two digits from yyyy)
e.g. if input year is 2012 then 20 = century
* year 2000, 1900, ... are 20-1, 19-1 respectively
ALGORITHM
step1: floor(century / 4)
step2: year
step3: floor(year/4)
step4: floor(month*2.6 -0.2) #this is the leap year correction
step5: day_of_month
step6: add step1...step5
step7: divide by 7 # modulo 7 in codespeak
step8: the remainder is the day of the week
To Interpret Results:
Sun = 0, Mon = 1, Tues = 3, etc..
Not a library, but as the public service jingle goes...
"Read: The More you Know"
Ref: http://www.faqs.org/faqs/sci-math-faq/dayWeek/
Is there a good way to get a year + week number converted a date in R? I have tried the following:
> as.POSIXct("2008 41", format="%Y %U")
[1] "2008-02-21 EST"
> as.POSIXct("2008 42", format="%Y %U")
[1] "2008-02-21 EST"
According to ?strftime:
%Y Year with century. Note that whereas there was no zero in the
original Gregorian calendar, ISO 8601:2004 defines it to be valid
(interpreted as 1BC): see http://en.wikipedia.org/wiki/0_(year). Note
that the standard also says that years before 1582 in its calendar
should only be used with agreement of the parties involved.
%U Week of the year as decimal number (00–53) using Sunday as the
first day 1 of the week (and typically with the first Sunday of the
year as day 1 of week 1). The US convention.
This is kinda like another question you may have seen before. :)
The key issue is: what day should a week number specify? Is it the first day of the week? The last? That's ambiguous. I don't know if week one is the first day of the year or the 7th day of the year, or possibly the first Sunday or Monday of the year (which is a frequent interpretation). (And it's worse than that: these generally appear to be 0-indexed, rather than 1-indexed.) So, an enumerated day of the week needs to be specified.
For instance, try this:
as.POSIXlt("2008 42 1", format = "%Y %U %u")
The %u indicator specifies the day of the week.
Additional note: See ?strptime for the various options for format conversion. It's important to be careful about the enumeration of weeks, as these can be split across the end of the year, and day 1 is ambiguous: is it specified based on a Sunday or Monday, or from the first day of the year? This should all be specified and tested on the different systems where the R code will run. I'm not certain that Windows and POSIX systems sing the same tune on some of these conversions, hence I'd test and test again.
Day-of-week == zero in the POSIXlt DateTimesClasses system is Sunday. Not exactly Biblical and not in agreement with the R indexing that starts at "1" convention either, but that's what it is. Week zero is the first (partial) week in the year. Week one (but day of week zero) starts with the first Sunday. And all the other sequence types in POSIXlt have 0 as their starting point. It kind of interesting to see what coercing the list elements of POSIXlt objects do. The only way you can actually change a POSIXlt date is to alter the $year, the $mon or the $mday elements. The others seem to be epiphenomena.
today <- as.POSIXlt(Sys.Date())
today # Tuesday
#[1] "2012-02-21 UTC"
today$wday <- 0 # attempt to make it Sunday
today
# [1] "2012-02-21 UTC" The attempt fails
today$mday <- 19
today
#[1] "2012-02-19 UTC" Success
I did not come up with this myself (it's taken from a blog post by Forester), but nevertheless I thought I'd add this to the answer list because it's the first implementation of the ISO 8601 week number convention that I've seen in R.
No doubt, week numbers are a very ambiguous topic, but I prefer an ISO standard over the current implementation of week numbers via format(..., "%U") because it seems that this is what most people agreed on, at least in Germany (calendars etc.).
I've put the actual function def at the bottom to facilitate focusing on the output first. Also, I just stumbled across package ISOweek, maybe worth a try.
Approach Comparison
x.days <- c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
x.names <- sapply(1:length(posix), function(x) {
x.day <- as.POSIXlt(posix[x], tz="Europe/Berlin")$wday
if (x.day == 0) {
x.day <- 7
}
out <- x.days[x.day]
})
data.frame(
posix,
name=x.names,
week.r=weeknum,
week.iso=ISOweek(as.character(posix), tzone="Europe/Berlin")$weeknum
)
# Result
posix name week.r week.iso
1 2012-01-01 Sun 1 4480458
2 2012-01-02 Mon 1 1
3 2012-01-03 Tue 1 1
4 2012-01-04 Wed 1 1
5 2012-01-05 Thu 1 1
6 2012-01-06 Fri 1 1
7 2012-01-07 Sat 1 1
8 2012-01-08 Sun 2 1
9 2012-01-09 Mon 2 2
10 2012-01-10 Tue 2 2
11 2012-01-11 Wed 2 2
12 2012-01-12 Thu 2 2
13 2012-01-13 Fri 2 2
14 2012-01-14 Sat 2 2
15 2012-01-15 Sun 3 2
16 2012-01-16 Mon 3 3
17 2012-01-17 Tue 3 3
18 2012-01-18 Wed 3 3
19 2012-01-19 Thu 3 3
20 2012-01-20 Fri 3 3
21 2012-01-21 Sat 3 3
22 2012-01-22 Sun 4 3
23 2012-01-23 Mon 4 4
24 2012-01-24 Tue 4 4
25 2012-01-25 Wed 4 4
26 2012-01-26 Thu 4 4
27 2012-01-27 Fri 4 4
28 2012-01-28 Sat 4 4
29 2012-01-29 Sun 5 4
30 2012-01-30 Mon 5 5
31 2012-01-31 Tue 5 5
Function Def
It's taken directly from the blog post, I've just changed a couple of minor things. The function is still kind of sketchy (e.g. the week number of the first date is far off), but I find it to be a nice start!
ISOweek <- function(
date,
format="%Y-%m-%d",
tzone="UTC",
return.val="weekofyear"
){
##converts dates into "dayofyear" or "weekofyear", the latter providing the ISO-8601 week
##date should be a vector of class Date or a vector of formatted character strings
##format refers to the date form used if a vector of
## character strings is supplied
##convert date to POSIXt format
if(class(date)[1]%in%c("Date","character")){
date=as.POSIXlt(date,format=format, tz=tzone)
}
# if(class(date)[1]!="POSIXt"){
if (!inherits(date, "POSIXt")) {
print("Date is of wrong format.")
break
}else if(class(date)[2]=="POSIXct"){
date=as.POSIXlt(date, tz=tzone)
}
print(date)
if(return.val=="dayofyear"){
##add 1 because POSIXt is base zero
return(date$yday+1)
}else if(return.val=="weekofyear"){
##Based on the ISO8601 weekdate system,
## Monday is the first day of the week
## W01 is the week with 4 Jan in it.
year=1900+date$year
jan4=strptime(paste(year,1,4,sep="-"),format="%Y-%m-%d")
wday=jan4$wday
wday[wday==0]=7 ##convert to base 1, where Monday == 1, Sunday==7
##calculate the date of the first week of the year
weekstart=jan4-(wday-1)*86400
weeknum=ceiling(as.numeric((difftime(date,weekstart,units="days")+0.1)/7))
#########################################################################
##calculate week for days of the year occuring in the next year's week 1.
#########################################################################
mday=date$mday
wday=date$wday
wday[wday==0]=7
year=ifelse(weeknum==53 & mday-wday>=28,year+1,year)
weeknum=ifelse(weeknum==53 & mday-wday>=28,1,weeknum)
################################################################
##calculate week for days of the year occuring prior to week 1.
################################################################
##first calculate the numbe of weeks in the previous year
year.shift=year-1
jan4.shift=strptime(paste(year.shift,1,4,sep="-"),format="%Y-%m-%d")
wday=jan4.shift$wday
wday[wday==0]=7 ##convert to base 1, where Monday == 1, Sunday==7
weekstart=jan4.shift-(wday-1)*86400
weeknum.shift=ceiling(as.numeric((difftime(date,weekstart)+0.1)/7))
##update year and week
year=ifelse(weeknum==0,year.shift,year)
weeknum=ifelse(weeknum==0,weeknum.shift,weeknum)
return(list("year"=year,"weeknum"=weeknum))
}else{
print("Unknown return.val")
break
}
}
Hey all, i have the following code to populate a dropdown to coinside with a companies 2008-2010 calander year. This is the output:
2008 Period 1: 11/30/2008 - 12/27/2008
2008 Period 2: 12/28/2008 - 1/24/2009
2009 Period 3: 1/25/2009 - 2/21/2009
2009 Period 4: 2/22/2009 - 3/21/2009
2009 Period 5: 3/22/2009 - 4/18/2009
2009 Period 6: 4/19/2009 - 5/16/2009
2009 Period 7: 5/17/2009 - 6/13/2009
2009 Period 8: 6/14/2009 - 7/11/2009
2009 Period 9: 7/12/2009 - 8/8/2009
2009 Period 10: 8/9/2009 - 9/5/2009
2009 Period 11: 9/6/2009 - 10/3/2009
2009 Period 12: 10/4/2009 - 10/31/2009
2009 Period 13: 11/1/2009 - 11/28/2009
2009 Period 1: 11/29/2009 - 12/26/2009
2009 Period 2: 12/27/2009 - 1/23/2010
2010 Period 3: 1/24/2010 - 2/20/2010
2010 Period 4: 2/21/2010 - 3/20/2010
2010 Period 5: 3/21/2010 - 4/17/2010
2010 Period 6: 4/18/2010 - 5/15/2010
2010 Period 7: 5/16/2010 - 6/12/2010
2010 Period 8: 6/13/2010 - 7/10/2010
2010 Period 9: 7/11/2010 - 8/7/2010
2010 Period 10: 8/8/2010 - 9/4/2010
2010 Period 11: 9/5/2010 - 10/2/2010
and here is the code for it:
Dim dt2009Start As DateTime
Dim dtTempStart, dtTempEnd As DateTime
Dim dtTempNow As DateTime
Dim nTemp As Integer
Dim itemPeriod As ListItem
Dim timesAround As Integer
dt2009Start = Convert.ToDateTime("11/30/2008")
dtTempStart = dt2009Start
dtTempEnd = dtTempStart.AddDays(27)
ddlInvoicePeriods.Items.Clear()
dtTempNow = DateTime.Now()
nTemp = 1
timesAround = 0
While (dtTempNow > dtTempEnd)
If nTemp = 12 Then
If timesAround = 0 Then
'dtTempStart = Convert.ToDateTime("10/25/2009")
'dtTempEnd = Convert.ToDateTime("11/18/2009")
End If
ElseIf nTemp = 14 Then
If timesAround = 0 Then
dtTempStart = Convert.ToDateTime("11/29/2009")
dtTempEnd = Convert.ToDateTime("12/26/2009")
nTemp = 1
timesAround += 1
End If
End If
itemPeriod = New ListItem()
itemPeriod.Text = dtTempStart.Date.Year.ToString() & " Period " & nTemp.ToString() & ": " & dtTempStart.Date.ToShortDateString() & " - " & dtTempEnd.Date.ToShortDateString()
itemPeriod.Value = dtTempStart.Date.ToShortDateString() & "-" & dtTempEnd.Date.ToShortDateString()
ddlInvoicePeriods.Items.Add(itemPeriod)
itemPeriod = Nothing
Debug.Print(dtTempStart.Date.Year.ToString() & " Period " & nTemp.ToString() & ": " & dtTempStart.Date.ToShortDateString() & " - " & dtTempEnd.Date.ToShortDateString())
dtTempStart = dtTempEnd.AddDays(1)
dtTempEnd = dtTempStart.AddDays(27)
nTemp += 1
End While
If nTemp = 12 Then
dtTempStart = Convert.ToDateTime("12/27/2009")
dtTempEnd = Convert.ToDateTime("11/18/2009")
ElseIf nTemp = 13 Then
dtTempStart = Convert.ToDateTime("11/19/2009")
dtTempEnd = Convert.ToDateTime("12/16/2009")
End If
itemPeriod = New ListItem
itemPeriod.Text = dtTempStart.Date.Year.ToString() & " Period " & nTemp.ToString() & ": " & dtTempStart.Date.ToShortDateString() & " - " & dtTempEnd.Date.ToShortDateString()
itemPeriod.Value = dtTempStart.Date.ToShortDateString() & "-" & dtTempEnd.Date.ToShortDateString()
ddlInvoicePeriods.Items.Add(itemPeriod)
itemPeriod = Nothing
As you may have noticed, the output only goes to 2010 (10/2/2010) which should go all the way to 12/31/2011.
Problem being:
It should start out like so:
2008 Period 13: Nov 30 - Dec 27
2009 Period 1: Dec 28 - Jan 24
2009 Period 2: Jan 25 - Feb 21
2009 Period 3: Feb 22 - Mar 21
...
2009 Period 13: Nov 29 - Dec 26
2010 Period 1: Dec 27 - Jan 23
2010 Period 2: Jan 24 - Feb 20
..
2010 Period 13: Nov 28 - Dec 25
2011 Period 1: Dec 26 - Jan 22
2011 Period 2: Jan 23 - Feb 19
...etc
Any help would be awesome as i am at my end of trying to figure out what i am doing wrong! :o)
David
There is a lot I would change (including using Iain's class for representing the period), and recognizing that nTemp needs to start at 13 for this example and the list is stopping because the loop was told to break at DateTime.Now for no apparent reason, eventhough you said it should continue to run to the end of 2011. But here is how to make the code you posted work:
Dim dt2009Start As DateTime
Dim dtTempStart, dtTempEnd As DateTime
Dim dtTempNow As DateTime
Dim nTemp As Integer
Dim itemPeriod As ListItem
Dim timesAround As Integer
Dim dtReallyEnd As DateTime
Dim year As Integer
year = 2008
dt2009Start = Convert.ToDateTime("11/30/2008")
dtTempStart = dt2009Start
dtTempEnd = dtTempStart.AddDays(27)
dtReallyEnd = Convert.ToDateTime("12/31/2011")
dtTempNow = DateTime.Now()
nTemp = 13
timesAround = 0
While (dtReallyEnd > dtTempEnd)
If nTemp = 14 Then
nTemp = 1
timesAround += 1
year += 1
End If
itemPeriod = New ListItem()
itemPeriod.text = year.ToString() & " Period " & nTemp.ToString() & ": " & dtTempStart.Date.ToShortDateString() & " - " & dtTempEnd.Date.ToShortDateString()
itemPeriod.value = dtTempStart.Date.ToShortDateString() & "-" & dtTempEnd.Date.ToShortDateString()
ddlInvoicePeriods.Items.Add(itemPeriod)
itemPeriod = Nothing
Debug.Print(dtTempStart.Date.Year.ToString() & " Period " & nTemp.ToString() & ": " & dtTempStart.Date.ToShortDateString() & " - " & dtTempEnd.Date.ToShortDateString())
dtTempStart = dtTempEnd.AddDays(1)
dtTempEnd = dtTempStart.AddDays(27)
nTemp += 1
End While
If nTemp = 14 Then
nTemp = 1
timesAround += 1
year += 1
End If
itemPeriod = New ListItem
itemPeriod.text = dtTempStart.Date.Year.ToString() & " Period " & nTemp.ToString() & ": " & dtTempStart.Date.ToShortDateString() & " - " & dtTempEnd.Date.ToShortDateString()
itemPeriod.value = dtTempStart.Date.ToShortDateString() & "-" & dtTempEnd.Date.ToShortDateString()
ddlInvoicePeriods.Items.Add(itemPeriod)
itemPeriod = Nothing
I would still probably make the end date more dynamic (so you don't have to change code in the future when you want more Periods to show) and the start date might be dynamic as well (to keep the list at a manageable length).
As you may have noticed, the output
only goes to 2010 (10/2/2010) which
should go all the way to 12/31/2011.
It stops when dtTempNow > dtTempEnd is no longer True. Plus, you might be interesting in String.Format
It should start out like so:
2008 Period 13: Nov 30 - Dec 27
Why? You set nTemp to 1 at the start, why should it start at 13?
I would create an object to hold your data, similar to this
Public Class Period
Public Property Period As Integer
Public Property StartOfPeriod As Date
Public Property EndOfPeriod As Date
End Class
Then for each iteration of the While loop I would enter the values as list(of Period), once your code has process, use a linq query to sort the data use Period and Date, then bind the list to the drop down.
Hope this helps