How can I calculate/find the week-number of a given date?
var currentCulture = CultureInfo.CurrentCulture;
var weekNo = currentCulture.Calendar.GetWeekOfYear(
new DateTime(2013, 12, 31),
currentCulture.DateTimeFormat.CalendarWeekRule,
currentCulture.DateTimeFormat.FirstDayOfWeek);
Be aware that this is not ISO 8601 compatible. In Sweden we use ISO 8601 week numbers but even though the culture is set to "sv-SE", CalendarWeekRule is FirstFourDayWeek, and FirstDayOfWeek is Monday the weekNo variable will be set to 53 instead of the correct 1 in the above code.
I have only tried this with Swedish settings but I'm pretty sure that all countries (Austria, Germany, Switzerland and more) using ISO 8601 week numbers will be affected by this problem.
Peter van Ooijen and Shawn Steele has different solutions to this problem.
Here's a compact solution
private static int WeekOfYearISO8601(DateTime date)
{
var day = (int)CultureInfo.CurrentCulture.Calendar.GetDayOfWeek(date);
return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(date.AddDays(4 - (day == 0 ? 7 : day)), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}
It's been tested for the following dates
var datesAndISO8601Weeks = new Dictionary<DateTime, int>
{
{new DateTime(2000, 12, 31), 52},
{new DateTime(2001, 1, 1), 1},
{new DateTime(2005, 1, 1), 53},
{new DateTime(2007, 12, 31), 1},
{new DateTime(2008, 12, 29), 1},
{new DateTime(2010, 1, 3), 53},
{new DateTime(2011, 12, 31), 52},
{new DateTime(2012, 1, 1), 52},
{new DateTime(2013, 1, 2), 1},
{new DateTime(2013, 12, 31), 1},
};
foreach (var dateWeek in datesAndISO8601Weeks)
{
Debug.Assert(WeekOfYearISO8601(dateWeek.Key) == dateWeek.Value, dateWeek.Key.ToShortDateString() + " should be week number " + dateWeek.Value + " but was " + WeekOfYearISO8601(dateWeek.Key));
}
public static int GetWeekNumber(DateTime dtPassed)
{
CultureInfo ciCurr = CultureInfo.CurrentCulture;
int weekNum = ciCurr.Calendar.GetWeekOfYear(dtPassed, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
return weekNum;
}
Check out GetWeekOfYear on MSDN has this example:
using System;
using System.Globalization;
public class SamplesCalendar {
public static void Main() {
// Gets the Calendar instance associated with a CultureInfo.
CultureInfo myCI = new CultureInfo("en-US");
Calendar myCal = myCI.Calendar;
// Gets the DTFI properties required by GetWeekOfYear.
CalendarWeekRule myCWR = myCI.DateTimeFormat.CalendarWeekRule;
DayOfWeek myFirstDOW = myCI.DateTimeFormat.FirstDayOfWeek;
// Displays the number of the current week relative to the beginning of the year.
Console.WriteLine( "The CalendarWeekRule used for the en-US culture is {0}.", myCWR );
Console.WriteLine( "The FirstDayOfWeek used for the en-US culture is {0}.", myFirstDOW );
Console.WriteLine( "Therefore, the current week is Week {0} of the current year.", myCal.GetWeekOfYear( DateTime.Now, myCWR, myFirstDOW ));
// Displays the total number of weeks in the current year.
DateTime LastDay = new System.DateTime( DateTime.Now.Year, 12, 31 );
Console.WriteLine( "There are {0} weeks in the current year ({1}).", myCal.GetWeekOfYear( LastDay, myCWR, myFirstDOW ), LastDay.Year );
}
}
My.Computer.Info.InstalledUICulture.DateTimeFormat.Calendar.GetWeekOfYear(yourDateHere, CalendarWeekRule.FirstDay, My.Computer.Info.InstalledUICulture.DateTimeFormat.FirstDayOfWeek)
Something like this...
I know this is late, but since it came up in my search I thought I would throw a different solution in. This was a c# solution.
(int)(Math.Ceiling((decimal)startDate.Day / 7)) + (((new DateTime(startDate.Year, startDate.Month, 1).DayOfWeek) > startDate.DayOfWeek) ? 1 : 0);
If you want the ISO 8601 Week Number, in which weeks start with a monday, all weeks are seven days, and week 1 is the week containing the first thursday of the year, this may be a solution.
Since there doesn't seem to be a .Net-culture that yields the correct ISO-8601 week number, I'd rater bypass the built in week determination alltogether, and do the calculation manually, instead of atempting to correct a partially correct result.
What I ended up with is the following extension method:
public static int GetIso8601WeekNumber(this DateTime date)
{ var thursday = date.AddDays(3 - ((int)date.DayOfWeek + 6) % 7);
return 1 + (thursday.DayOfYear - 1) / 7;
}
First of all, ((int)date.DayOfWeek + 6) % 7) determines the weekday number, 0=monday, 6=sunday.
date.AddDays(-((int)date.DayOfWeek + 6) % 7) determines the date of the monday preceiding the requested week number.
Three days later is the target thursday, which determines what year the week is in.
If you divide the (zero based) day-number within the year by seven (round down), you get the (zero based) week number in the year.
In c#, integer calculation results are round down implicitly.
var cultureInfo = CultureInfo.CurrentCulture;
var calendar = cultureInfo.Calendar;
var calendarWeekRule = cultureInfo.DateTimeFormat.CalendarWeekRule;
var firstDayOfWeek = cultureInfo.DateTimeFormat.FirstDayOfWeek;
var lastDayOfWeek = cultureInfo.LCID == 1033 //En-us
? DayOfWeek.Saturday
: DayOfWeek.Sunday;
var lastDayOfYear = new DateTime(date.Year, 12, 31);
//Check if this is the last week in the year and it doesn`t occupy the whole week
var weekNumber = calendar.GetWeekOfYear(date, calendarWeekRule, firstDayOfWeek);
return weekNumber == 53 && lastDayOfYear.DayOfWeek != lastDayOfWeek
? 1
: weekNumber;
It works well both for US and Russian cultures. ISO 8601 also will be correct, `cause Russian week starts at Monday.
Related
I have to show a date's calendar week using the same numbering used in Outlook.
The numbering has to follow the italian (it-IT) culture.
The Outlook calendar is configured using Monday as first day of week and "1st of January is always on Week 1".
An example of another calendar using the same numbering logic is here (maybe you need to turn week numbers on).
I've tried different methods but I don't get the correct week number with some dates.
In particular, I have tried using:
Calendar returned from CultureInfo.GetCulture("it-IT")
GregorianCalendar
ISOWeek
Some of the dates I tried are:
31st December 200
29th March 2021
31 December 2012
21 December 1992
30 December 2019
The only reliable way I found to get the correct week number is to calculate it myself, but I don't really like the idea to have to depend on my date calculations code (I can't be 100% sure to have found the correct logic has I can't test every possible date).
There is a program below showing the various results for every method I tried and how I manually calculate week numbers.
You can also try it online here.
The output of the example (as an image to show the various colors):
So the question is: is there a built-in way to get Week numbers from a date using the same numbering logic used by Outlook ?
EDIT:
I've already tried to use CalendarWeekRule.FirstFourDayWeek and CalendarWeekRule.FirstFullWeek
The example program I mentioned:
public sealed class Program
{
private static Calendar italianCalendar = CultureInfo.GetCultureInfo("it-IT").Calendar;
private static Calendar plainGregorianCalendar = new GregorianCalendar();
public static void Main()
{
var _31stOfDecember2000 = new DateTime(2000, 12, 31);
var _29thOfMarch2021 = new DateTime(2021, 3, 29);
var _31stOfDecember2012 = new DateTime(2012, 12, 31);
var _21stDecember1992 = new DateTime(1992, 12, 21);
var _30thDecember2019 = new DateTime(2019, 12, 30);
var _1stOfJanuary2013 = new DateTime(2013, 1, 1);
PrintWeek(_31stOfDecember2000, 53);
PrintWeek(_29thOfMarch2021, 14);
PrintWeek(_31stOfDecember2012, 1);
PrintWeek(_21stDecember1992, 52);
PrintWeek(_30thDecember2019, 1);
PrintWeek(_1stOfJanuary2013, 1);
Console.ReadKey();
}
private static void PrintWeek(DateTime date, int expectedWeek)
{
var isoWeek = ISOWeek.GetWeekOfYear(date);
var italianCalendarWeek = italianCalendar.GetWeekOfYear(date, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
var gregorianCalendarWeek = plainGregorianCalendar.GetWeekOfYear(date, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
var manuallyCalculatedWeek = CalculateWeek(date);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Date: {0:yyyy MMMM dd} - Expected week {1}", date, expectedWeek);
Console.ForegroundColor = isoWeek == expectedWeek ? ConsoleColor.Green : ConsoleColor.Red;
Console.WriteLine("ISO Week: {0}", isoWeek);
Console.ForegroundColor = italianCalendarWeek == expectedWeek ? ConsoleColor.Green : ConsoleColor.Red;
Console.WriteLine("Italian calendar Week: {0}", italianCalendarWeek);
Console.ForegroundColor = gregorianCalendarWeek == expectedWeek ? ConsoleColor.Green : ConsoleColor.Red;
Console.WriteLine("Gregorian calendar Week: {0}", gregorianCalendarWeek);
Console.ForegroundColor = manuallyCalculatedWeek == expectedWeek ? ConsoleColor.Green : ConsoleColor.Red;
Console.WriteLine("Manually calculated Week: {0}", manuallyCalculatedWeek);
Console.WriteLine();
}
public static int CalculateWeek(DateTime date)
{
var firstDayOfFirstWeekOfDateYear = StartOfWeekOfYear(date.Year, 1);
var firstDayOfFirstWeekOfNextYear = StartOfWeekOfYear(date.Year + 1, 1);
var lastDayOfLastWeekOfDateYear = firstDayOfFirstWeekOfNextYear.AddDays(-1);
var timePassedSinceFirstWeek = date - firstDayOfFirstWeekOfDateYear;
var daysPassedSinceFirstWeek = timePassedSinceFirstWeek.Days;
var timePassedBetweenFirstAndEndOfYear = lastDayOfLastWeekOfDateYear - firstDayOfFirstWeekOfDateYear;
var daysPassedBetweenFirstAndEndOfYear = timePassedBetweenFirstAndEndOfYear.Days;
var weeksOfDateYear = (daysPassedBetweenFirstAndEndOfYear / 7) + 1;
//If the week number surpasses the effective number of weeks of the year it is wrapped around the next year using modulo operator
//Modulo will wrap the week number around the Year weeks leaving the reminder of the calculation as the Week number of the next year.
var week = (daysPassedSinceFirstWeek / 7 % weeksOfDateYear) + 1;
return week;
}
private static DateTime StartOfWeekOfYear(int year, int week)
{
var firstOfJanuary = new DateTime(year, 1, 1);
var dayOfWeekFirstOfJanuary = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(firstOfJanuary);
//Sunday is 0 instead of 7, screwing calculations over
var dayOfWeekOffset = dayOfWeekFirstOfJanuary == DayOfWeek.Sunday ? -6 : DayOfWeek.Monday - dayOfWeekFirstOfJanuary;
var daysOffset = (7 * (week - 1)) + dayOfWeekOffset;
return firstOfJanuary.AddDays(daysOffset);
}
I adapted the answer from the one linked above to suit your requirements. The results match with all the manual calculations you've provided above.
using System;
using System.Globalization;
public class Program
{
public static void Main()
{
var today = new DateTime(2000, 12, 31);
Console.WriteLine(string.Format("{0} {1} {2} {3}", today, today.GetWeekOfYear("it-it"), today.DayOfWeek, today.DayOfYear));
today = new DateTime(2021, 3, 29);
Console.WriteLine(string.Format("{0} {1} {2} {3}", today, today.GetWeekOfYear("it-it"), today.DayOfWeek, today.DayOfYear));
today = new DateTime(2012, 12, 31);
Console.WriteLine(string.Format("{0} {1} {2} {3}", today, today.GetWeekOfYear("it-it"), today.DayOfWeek, today.DayOfYear));
today = new DateTime(1992, 12, 21);
Console.WriteLine(string.Format("{0} {1} {2} {3}", today, today.GetWeekOfYear("it-it"), today.DayOfWeek, today.DayOfYear));
today = new DateTime(2009, 12, 30);
Console.WriteLine(string.Format("{0} {1} {2} {3}", today, today.GetWeekOfYear("it-it"), today.DayOfWeek, today.DayOfYear));
today = new DateTime(2013, 1, 1);
Console.WriteLine(string.Format("{0} {1} {2} {3}", today, today.GetWeekOfYear("it-it"), today.DayOfWeek, today.DayOfYear));
}
}
static class DateTimeExtensions
{
public static int GetWeekOfYear(this DateTime time, string languageTag)
{
// Seriously cheat. If its Monday, Tuesday or Wednesday, then it'll
// be the same week# as whatever Thursday, Friday or Saturday are,
// and we always get those right
var culture = CultureInfo.GetCultureInfoByIetfLanguageTag(languageTag);
var day = culture.Calendar.GetDayOfWeek(time);
if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
{
time = time.AddDays(3);
}
// Return the week of our adjusted day
return culture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
}
}
I have a trouble with NodaTime lib. My goal: compute Year/Month/Date between two dates. So, here is my test example:
private static void Main()
{
var list = new List<Tuple<DateTime, DateTime>>
{
new Tuple<DateTime, DateTime>(new DateTime(1980, 1, 1), new DateTime(1983, 12, 31)),
new Tuple<DateTime, DateTime>(new DateTime(2009, 1, 1), new DateTime(2015, 01, 23))
};
var totalPeriod = Period.Zero;
foreach (var tuple in list)
{
var dateFrom = tuple.Item1;
var dateTo = tuple.Item2;
var ld1 = new LocalDate(dateFrom.Year, dateFrom.Month, dateFrom.Day);
var ld2 = new LocalDate(dateTo.Year, dateTo.Month, dateTo.Day);
var period = Period.Between(ld1, ld2, PeriodUnits.YearMonthDay);
totalPeriod += period;
}
Console.WriteLine("Years: {0}, Months: {1}, Days: {2}",
totalPeriod.Years,
totalPeriod.Months,
totalPeriod.Days);
Console.Read();
}
The output is:
Years: 9, Months: 11, Days: 52
It's wrong for me. I want to get, for example, the next output (Of course, the output depends on number of days in month, assuming that there are 31 days in our month):
Years: 10, Months: 0, Days: 21
So, I want that days was rounded to years and month. How I can get this?
The answer:
Using Matt's answer I created the next solution:
foreach (var tuple in list)
{
var dateFrom = tuple.Item1;
var dateTo = tuple.Item2;
var period = Period.Between(LocalDateTime.FromDateTime(dateFrom).Date, LocalDateTime.FromDateTime(dateTo).Date, PeriodUnits.YearMonthDay);
totalPeriod += period;
}
// trying clarify the period
while(totalPeriod.Days >= 30)
{
totalPeriod = totalPeriod - Period.FromDays(30);
totalPeriod = totalPeriod + Period.FromMonths(1);
while (totalPeriod.Months >= 12)
{
totalPeriod = totalPeriod - Period.FromMonths(12);
totalPeriod = totalPeriod + Period.FromYears(1);
}
}
Richard was right in his comment on the OP. The problem is that the months and years aren't distinct quantities unto themselves. One must have a frame of reference to "count" them. You have that reference when you do the Period.Between operation, but it's lost by the time you try to add the periods together.
If you check the periods that are being added, it makes sense:
First: Years: 3, Months: 11, Days: 30
Second: Years: 6, Months: 0, Days: 22
Total: Years: 9, Months: 11, Days: 52
In order to round as you would like, the 22 days being added to the 30 days would somehow have to know which month was being referenced. Even if you retained the original information - which one would you use? You could well have a 28-day month on one side, and a 31-day month on the other.
The best you could do would be to artificially round the results yourself afterwards, and choose a flat value (such as 30 days) to represent all months.
Oh, one minor thing (unrelated to your question) - To go from a DateTime to a LocalDate, try LocalDateTime.FromDateTime(dt).Date. :)
I need to find the week numbers of a given date rage in C#.
Ex: date between 01/01/2014 and 14/01/2014
Week numbers are 1st,2nd and 3rd weeks, likewise.
Thanks.
Not the smartest way, but works!
var d1 = new DateTime(2014, 1, 1);
var d2 = new DateTime(2014, 1, 14);
var currentCulture = CultureInfo.CurrentCulture;
var weeks = new List<int>();
for (var dt = d1; dt < d2; dt =dt.AddDays(1))
{
var weekNo = currentCulture.Calendar.GetWeekOfYear(
dt,
currentCulture.DateTimeFormat.CalendarWeekRule,
currentCulture.DateTimeFormat.FirstDayOfWeek);
if(!weeks.Contains(weekNo))
weeks.Add(weekNo);
}
This should work:
public List<int> Weeks(DateTime start, DateTime end)
{
List<int> weeks=new List<int>();
var Week=(int)Math.Floor((double)start.DayOfYear/7.0); //starting week number
for (DateTime t = start; t < end; t = t.AddDays(7))
{
weeks.Add(Week);
Week++;
}
return weeks;
}
All this does is get the week of the start date, then loops through one week at a time until you get to the end date, incrementing the week and adding it to the list of weeks.
OK, let's start with a simple, unoptimized example. We'll simply examine every date between those dates and check what week of the year it is.
We can do that with a simple loop:
var end = new DateTime(2014, 1, 14);
for (var date = new DateTime(2014, 1, 1); date <= end; date = date.AddDays(1))
{
}
This will simply loop over every day between two dates. Now we need to examine those days to determine their day of week. To do this you need to consider a few things: What is the first day of a week? Sunday? Monday? Are we assuming gregorian calendar?
For our example, let's assume the first day of the week is a Sunday and we are indeed using the Gregorian calendar. Then we will check each date, and keep a list of unique weeks of the year using a HashSet:
var weekNumber = new HashSet<int>();
var end = new DateTime(2014, 1, 14);
var calendar = new GregorianCalendar();
for (var date = new DateTime(2014, 1, 1); date <= end; date = date.AddDays(1))
{
weekNumber.Add(calendar.GetWeekOfYear(date, CalendarWeekRule.FirstDay, DayOfWeek.Sunday));
}
The weekNumber Hashset now contains the weeks of the year.
Is this optimized? No. It checks far more dates than it needs to, but the implementation is simple and fast enough. Optimizing can be done as a separate task.
Easy. Here's the code that determines the week of the year for a single date. This should get you going:
int WeekOfYear(DateTime date, DayOfWeek dayOfWeekStart) {
//Find the first day of the year that's the start of week day. If it's not 1/1,
//then we have a first partial week.
bool firstPartialWeek = false;
DateTime firstFullWeekStart = new DateTime(date.Year, 1, 1);
while(firstFullWeekStart.DayOfWeekDay != dayOfWeekStart) {
firstFullWeekStart = firstOfWeek.AddDays(1);
firstPartialWeek = true;
}
//Return the week by integer dividing the date difference by seven,
//and adding in the potential first partial week
return (firstPartialWeek ? 1 : 0) + ((date - firstFullWeekStart).TotalDays / 7);
}
Assume Financial Quarters always start on the 1st of a month and they are always 3 calendar months long.
Different organisations start their Financial Year (FY) in different months - some may be 1st April , some may be 1st July or could be just 1st Jan (which will match normal Calendar Quarters).
Given a date and a month that the FY starts on how can you determine the start of the quarter that the date falls in.
E.g.
DateTime getStartOfFinancialQtr(DateTime date, int monthFinancialYearStartsOn)
15th Jan when FY starts Jan would = 1st Jan
getStartOfFinancialQtr(new DateTime(2013,1,15), 1) == new DateTime(2013,1,1)
15th August when FY starts April would be 1st July
getStartOfFinancialQtr(new DateTime(2013,8,15), 4) == new DateTime(2013,7,1)
BUT 15th Jan 2013 when FY starts February would be 1st November 2012
getStartOfFinancialQtr(new DateTime(2013,1,15), 2) == new DateTime(2012,11,1)
The following solution is the most simple implementation I could think of and works without any - unnecessary - loops:
DateTime getStartOfFinancialQtr(DateTime date, int monthFinancialYearStartsOn)
{
var actualMonth = date.Month;
var financialYear = date.Year;
var difference = actualMonth - monthFinancialYearStartsOn;
if(difference < 0)
{
--financialYear;
difference += 12;
}
var quarter = difference / 3;
return new DateTime(financialYear, monthFinancialYearStartsOn, 1).AddMonths(quarter * 3);
}
Isn't it as simple as this? Am I missing something to this? A quarter is defined as a period of three months, so you just have to find where the given date is, and then compute where the quarter begins based off that given month of the date.
public DateTime GetStartOfFinancialQtr(DateTime dtGiven, int startMonth) {
DateTime dtQuarter = new DateTime(dtGiven.Year, startMonth, 1);
// Start Q is less than the given date
if(startMonth > dtGiven.Month) {
while(dtQuarter > dtGiven) {
dtQuarter = dtQuarter.AddMonths(-3);
}
}
// Start Q is larger than the given date
else {
while(dtQuarter.Month + 3 <= dtGiven.Month) {
dtQuarter = dtQuarter.AddMonths(3);
}
}
return dtQuarter;
}
Below is the testing I ran:
Console.WriteLine(GetStartOfFinancialQtr(new DateTime(2013, 1, 15), 1).ToString());
Console.WriteLine(GetStartOfFinancialQtr(new DateTime(2013, 8, 15), 4).ToString());
Console.WriteLine(GetStartOfFinancialQtr(new DateTime(2013, 1, 15), 2).ToString());
Console output:
01/01/2013 000000
07/01/2013 000000
11/01/2012 000000
You can use the Year class of the Time Period Library for .NET:
// ----------------------------------------------------------------------
public void FiscalYearRange()
{
// calendar
TimeCalendar fiscalYearCalendar = new TimeCalendar(
new TimeCalendarConfig
{
YearBaseMonth = YearMonth.April,
YearType = YearType.FiscalYear
} );
// time range
TimeRange timeRange = new TimeRange( new DateTime( 2007, 10, 1 ), new DateTime( 2012, 2, 25 ) );
Console.WriteLine( "Time range: " + timeRange );
Console.WriteLine();
// fiscal quarter
Console.WriteLine( "Start Quarter: " + new Quarter( timeRange.Start, fiscalYearCalendar ) );
Console.WriteLine( "End Quarter: " + new Quarter( timeRange.End, fiscalYearCalendar ) );
Console.WriteLine();
// fiscal year
Year year = new Year( timeRange.Start, fiscalYearCalendar );
while ( year.Start < timeRange.End )
{
Console.WriteLine( "Fiscal Year: " + year );
year = year.GetNextYear();
}
} // FiscalYearRange
As mentioned, you can easily obtain the answer from Nearest Completed quarter. Here's how you make the modification:
public static class DateTimeExtensions {
public static DateTime NearestQuarterEnd(
this DateTime date,
int firstMonthOfFiscalYear
) {
IEnumerable<DateTime> candidates =
QuartersInYear(date.Year, firstMonthOfFiscalYear)
.Concat(QuartersInYear(date.Year - 1, firstMonthOfFiscalYear));
return candidates.SkipWhile(d => d > date).First();
}
static Dictionary<Tuple<int, int>, List<DateTime>> dict =
new Dictionary<Tuple<int, int>, List<DateTime>>();
static IEnumerable<DateTime> QuartersInYear(
int year,
int firstMonthOfFiscalYear
) {
Contract.Requires(firstMonthOfFiscalYear >= 1
&& firstMonthOfFiscalYear <= 12);
var key = Tuple.Create(year, firstMonthOfFiscalYear);
if(dict.ContainsKey(key)) {
return dict[key];
}
else {
var value =
Enumerable
.Range(0, 4)
.Select(k => firstMonthOfFiscalYear + 3 * k)
.Select(m => m <= 12 ? m : m % 12)
.Select(m => new DateTime(year, m, 1))
.OrderByDescending(d => d)
.ToList();
dict.Add(key, value);
return value;
}
}
}
Usage:
Console.WriteLine(new DateTime(2013, 1, 15).NearestQuarterEnd(1));
Console.WriteLine(new DateTime(2013, 8, 15).NearestQuarterEnd(4));
Console.WriteLine(new DateTime(2013, 1, 15).NearestQuarterEnd(2));
Output:
1/1/2013 12:00:00 AM
7/1/2013 12:00:00 AM
11/1/2012 12:00:00 AM
This passes all three of your test cases.
When given a start date a need to do various calculations on it to produce 3 other dates.
Basically I need to work out what date the user has been billed up to for different frequencies based on the current date.
Bi-Annually (billed twice a year),
Quarterly (billed 4 times a year),
and Two Monthly (billed ever other month).
Take the date 26/04/2008
- BiAnnually: This date would have been last billed on 26/10/2010 and should give the date 26/04/2011.
- Quarterly: This date would have been last billed on 26/01/2011 and should give the date 26/04/2011.
- Two Month: This date would have been last billed on 26/12/2010 and should give the date 26/02/2011.
Assistance is much appreciated.
I think that you can just do like this:
public void FindNextDate(DateTime startDate, int interval);
DateTime today = DateTime.Today;
do {
startDate = startDate.AddMonths(interval);
} while (startDate <= today);
return startDate;
}
Usage:
DateTime startDate = new DateTime(2008, m4, 26);
DateTime bi = FindNextDate(startDate, 6);
DateTime quarterly = FindNextDate(startDate, 3);
DateTime two = FindNextDate(startDate, 2);
I think all you want is something like
DateTime x = YourDateBasis;
y = x.AddMonths(6);
y = x.AddMonths(3);
y = x.AddMonths(2);
Then to edit from comment,
Date Math per the period cycle of the person's account, you would simply need the start and end date and keep adding respective months until you've created all expected months. Almost like that of a loan payment that's due every month for 3 years
DateTime CurrentDate = DateTime.Now;
while( CurrentDate < YourFinalDateInFuture )
{
CurrentDate = CurrentDate.AddMonths( CycleFrequency );
Add Record into your table as needed
Perform other calcs as needed
}
enum BillPeriod
{
TwoMonth = 2,
Quarterly = 3,
SemiAnnually = 6,
BiAnnually = 24
}
public Pair<Datetime, Datetime> BillDates(Datetime currentBillDate, BillPeriod period)
{
Datetime LastBill = currentBillDate.AddMonths(-1 * (int)period);
Datetime NextBill = currentBillDate.AddMonths((int)period);
return new Pair<Datetime,Datetime>(LastBill, NextBill);
}
This is a terrible solution, but it works. Remember, red-light, green-light, refactor. Here, we're at green-light:
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
Console.WriteLine(GetLastBilled(new DateTime(2008, 4, 26), 6));
Console.WriteLine(GetNextBilled(new DateTime(2008, 4, 26), 6));
Console.WriteLine(GetLastBilled(new DateTime(2008, 4, 26), 4));
Console.WriteLine(GetNextBilled(new DateTime(2008, 4, 26), 4));
Console.WriteLine(GetLastBilled(new DateTime(2008, 4, 26), 2));
Console.WriteLine(GetNextBilled(new DateTime(2008, 4, 26), 2));
Console.WriteLine("Complete...");
Console.ReadKey(true);
}
static DateTime GetLastBilled(DateTime initialDate, int billingInterval) {
// strip time and handle staggered month-end and 2/29
var result = initialDate.Date.AddYears(DateTime.Now.Year - initialDate.Year);
while (result > DateTime.Now.Date) {
result = result.AddMonths(billingInterval * -1);
}
return result;
}
static DateTime GetNextBilled(DateTime initialDate, int billingInterval) {
// strip time and handle staggered month-end and 2/29
var result = initialDate.Date.AddYears(DateTime.Now.Year - initialDate.Year);
while (result > DateTime.Now.Date) {
result = result.AddMonths(billingInterval * -1);
}
result = result.AddMonths(billingInterval);
return result;
}
}
}
This is really tricky. For example, you need to take into account that the date you billed could have been 2/29 on a leap year, and not all months have the same number of days. That's why I did the initialDate.Date.AddYears(DateTime.Now.Year - initialDate.Year); call.