Store more than 24 hours in a DateTime - c#

I work in a bizarre and irrational industry where we need to be able to represent the time of day as 06:00:00 to 30:00:00 instead of 0:00:00 to 24:00:00. Is there any way to do this using the DateTime type? If I try to construct a date time with an hour value greater than 24 it throws an exception.

I think this should be a presentation issue only.
Allow your users to input data in this weird format, and immediately convert it to UTC. Do all calculations on the UTC times. Then create a ToString method to convert the results back into your weird format. You will probably also need some other utility methods and properties such as an implementation of WeirdDateTime.Day.
You could write a wrapper class around a DateTime and have all the conversion and utility methods you need on that class. I've had a go at starting it - by implementing parsing from a string in weird format. This isn't production code ready by any means, but perhaps it can give you a few ideas of how you could approach this:
class WeirdDateTime
{
public DateTime DateTime { get; set; }
public WeirdDateTime(int year, int month, int day, int hour, int minute, int second, DateTimeKind kind)
{
if (hour < 6 || hour >= 30)
throw new ArgumentException("Not a valid WeirdDateTime", "hour");
bool addDay;
if (hour >= 24)
{
addDay = true;
hour -= 24;
}
else
{
addDay = false;
}
DateTime dateTime = new DateTime(year, month, day, hour, minute, second, kind);
if (addDay)
dateTime = dateTime.AddDays(1);
DateTime = dateTime;
}
public static WeirdDateTime Parse(string s)
{
Regex regex = new Regex(#"(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})");
Match match = regex.Match(s);
if (!match.Success)
throw new FormatException("Not a valid WeirdDateTime");
int[] parts = match.Groups.Cast<Group>()
.Skip(1)
.Select(x => int.Parse(x.Value))
.ToArray();
int year = parts[0];
int month = parts[1];
int day = parts[2];
int hour = parts[3];
int minute = parts[4];
int second = parts[5];
return new WeirdDateTime(year, month, day, hour, minute, second, DateTimeKind.Unspecified);
}
public override string ToString()
{
throw new NotImplementedException("Write this!");
}
}
class Program
{
public static void Main()
{
WeirdDateTime weirdDateTime = WeirdDateTime.Parse("2010-01-19 27:00:00");
DateTime dateTimeUtc = weirdDateTime.DateTime.ToUniversalTime();
Console.WriteLine(dateTimeUtc);
}
}

How about use a TimeSpan instead ?
DateTime departure = new DateTime(2010, 6, 12, 18, 32, 0);
DateTime arrival = new DateTime(2010, 6, 13, 22, 47, 0);
TimeSpan travelTime = arrival - departure;
Console.WriteLine("{0} - {1} = {2}", arrival, departure, travelTime);
Then use the TotalHours property of the TimeSpan obj

I doubt you can do exactly what you're looking for, but I expect that you could make your own DateTime class that simply adds +6 hrs to the value. i.e. stores 00 - 24 internally, but the get/set methods make it seem like 06 - 30.

You should be using TimeSpan, not DateTime.
The format options for TimeSpan is
a: [days].[hours]:[minutes]:[seconds].[fractional seconds]
b: [days].[hours]:[minutes]:[seconds]
c: [days].[hours]:[minutes]
d: [days].[hours]
e: [days]
f: [hours]:[minutes]:[seconds].[fractional seconds]
g: [hours]:[minutes]:[seconds]
h: [hours]:[minutes]

Just have your business logic store/return DateTime.Hours.Add(6). You'll have to be aware of this in your display logic.

how 'bout using a normal DateTime to store the actual time, and writing a new class which stores (or derives from ) a DateTime, and has a ToString() which adjusts the output.

I for calculate Employ work hours use this function:
public string SumHours(string TimeIn, string TimeOut)
{
var parts = TimeIn.Split(':');
var hours = Int32.Parse(parts[0]);
var minutes = Int32.Parse(parts[1]);
var result = new TimeSpan(hours, minutes, 0);
TimeIn = result.ToString();
TimeSpan Hour1 = TimeSpan.Parse(TimeIn);
TimeSpan Hour2 = TimeSpan.Parse(TimeOut);
Hour1 = Hour1.Add(Hour2);
string HourtoStr = string.Format("{0:D2}:{1:D2}:{2:D2}", (Hour1.Days * 24 + Hour1.Hours), Hour1.Minutes, Hour1.Seconds);
return HourtoStr;
}

Related

C# datetime scope

Assuming I can not change service that returns data, I am left with
var date = "20140231";
var scope = DateTime.ParseExact(date, "yyyyMMdd", CultureInfo.CurrentCulture);
Clearly "20140231" is lazy way of saying end of February. What is the cleanest way to get last date of February with input of "20140231"?
There is 1 constraint - this should work with .net 2.0.
string date = "20140231";
DateTime result;
int year = Convert.ToInt32(date.Substring(0, 4));
int month = Convert.ToInt32(date.Substring(4, 2));
int day = Convert.ToInt32(date.Substring(6, 2));
result = new DateTime(year, month, Math.Min(DateTime.DaysInMonth(year, month), day));
February can have only 28 or 29 days depends on current year is a leap year or not.
It can't have 30 or 31 days in any year. That's why you can't parse your 20140231 string successfully.
You can clearly get the last day of February like;
DateTime lastDayOfFebruary = (new DateTime(2014, 2, 1)).AddMonths(1).AddDays(-1);
If your service always get year as a first 4 character, you can use .Substring() to get year and pass DateTime constructor as a year.
var date = "20140231";
string year = date.Substring(0, 4);
DateTime lastDayOfFebruary = (new DateTime(int.Parse(year), 2, 1)).AddMonths(1).AddDays(-1);
You could create a while, cut the date in pieces, and keep subtracting one from the day part until it is a valid date. This should really be fixed on the entry side though.
Try this:
var date = "20140231";
DateTime scope;
bool dateValid = DateTime.TryParseExact(date, "yyyyMMdd", CultureInfo.CurrentCulture, DateTimeStyles.None, out scope);
while (!dateValid)
{
string yearMonth = date.Substring(0, 4);
int day = Convert.ToInt32(date.Substring(6, 2));
if (day > 1)
{
day--;
}
else
{
break;
}
date = yearMonth + day.ToString().PadLeft(2, '0');
dateValid = DateTime.TryParseExact(date, "yyyyMMdd", CultureInfo.CurrentCulture, DateTimeStyles.None, out scope);
}

Absolute difference of a TimeSpan object with a timing interval in scale of month and year

I am in some trouble within a simple timing manipulation in C#.
The user defines two DateTime objects, as the start and the end of a time interval:
DateTime From = new DateTime(Y, Mo, D, H, Mi, S, Ms);
DateTime To = new DateTime(YY, MMo, DD, HH, MMi, SS, MMs);
Then a delay parameter, is which a TimeSpan object, would be taken into account:
TimeSpan delay = new TimeSpan(day, month, hour, second);
Now the program should return the deviation of the time interval, corresponding to the delay parameter.
Now, there are two problems:
1- Time span has no Year and Month parameters, whereas the difference between From and To might be more than Day... How can I feed Year and Month into the TimeSpan object?!... (I know that there is no defined constructor for this aim)
2- The final difference, which I try to catch by below code snippet just produces garbage:
var diff = (To - From).duration() - delay;
How should I resolve this case?!
I am appreciated if anyone can handle above cases...
This is the sort of thing that my Noda Time project is designed to handle. It has a Period type which does know about months and years, not just a fixed number of ticks. For example:
LocalDateTime start = new LocalDateTime(2014, 1, 1, 8, 30);
LocalDateTime end = new LocalDateTime(2014, 9, 16, 12, 0);
Period delay = new PeriodBuilder {
Months = 8,
Days = 10,
Hours = 2,
Minutes = 20
}
.Build();
Period difference = (Period.Between(start, end) - delay).Normalize();
Here difference would be a period of 5 days, 1 hour, 10 minutes. (The Normalize() call is to normalize all values up to days... otherwise you can have "1 hour - 10 minutes" for example.) The Period API is going to change a bit for Noda Time 2.0, but it will still have the same basic ideas.)
If you you choose to round down and add extension methods :
public static class Extensions
{
private const double DaysInYear = 365.242;
private const double DaysInMonth = 30.4368;
public static int GetDays(this TimeSpan ts)
{
return (int)((ts.TotalDays % DaysInYear % DaysInMonth));
}
public static int GetMonths(this TimeSpan ts)
{
return (int)((ts.TotalDays % DaysInYear) / DaysInMonth);
}
public static int GetYears(this TimeSpan ts)
{
return (int)(ts.TotalDays / DaysInYear);
}
}
It would be easy as:
var oldDate = new DateTime(2002, 7, 15);
var newDate = new DateTime(2014, 9, 16, 12, 3, 0);
// Difference
var ts = newDate - oldDate;
var dm = ts.Minutes; //3
var dh = ts.Hours; //12
var dd = ts.GetDays(); //2
var dM = ts.GetMonths(); //2
var dY = ts.GetYears(); //12
Note that this is an approximation and would apply only if you can make assumptions that
DaysInYear = 365.242
DaysInMonth = 30.4368
are correct.

Range of values - how to determine without if loop?

I need to find the sun-sign of a given person based on his age.
For eg,
Capricorn December 22 – January 20
Aquarius January 21 – February 18
Pisces February 19 – March 19
Aries March 20 – April 19
Taurus April 20 – May 20
Gemini May 21 – June 20
Cancer June 21 – July 22
Leo July 23 – August 22
Virgo August 23 – September 22
Libra September 23 – October 22
Scorpio October 23 – November 21
Sagittarius November 22 – December 21
I wrote this code,
public enum Months
{
January = 1, February, March, April, May, June, July, August, September, October, November, December,
}
var person = new Person(name:"mady", age:20, dateTime: new DateTime(2011,09,16));
if (person.DOB.Month == (int)Months.December)
{
if (person.DOB.Day >= 22)
return "Capricorn";
else
return "Sagittarius";
} ...
....
....
....
The IF statements grow consistently and might become a nightmare if tomorrow the list grows.
Is there an elegant way of finding out the Sunsign ? Enumerable or Range in .NET doesn't seem to fit this case or is this the only way of writing the code ?
Create a class StarSign:
class StarSign
{
public readonly string Name;
public readonly DateTime StartDate;
public readonly DateTime EndDate;
public bool Contains(DateTime date);
}
Add all the star signs to a collection StarSigns. Then for any given DateTime date (of the person) do
foreach (var sign in StarSigns)
{
if (sign.Contains(date))
{
Console.WriteLine("I am a: " + sign.Name);
break;
}
}
Edit, responding to your comment:
The Contains function can easily compare dates, just make sure you ignore the year:
public bool Contains(DateTime date)
{
DateTime startNoYear = new DateTime(1904, StartDate.Month, StartDate.Day);
DateTime endNoYear = new DateTime(1904, EndDate.Month, EndDate.Day);
DateTime dateNoYear = new DateTime(1904, date.Month, date.Day);
return dateNoYear >= startNoYear && dateNoYear <= endNoYear;
}
So yes, if you have many many StarSigns, this will affect performance. Normaly you will only have 12, and since you know you are dealing with a closed set, you can afford to do it this way.
When it comes to optimization, you will also want to store startNoYear and endNoYear and not calculate them each time you run Contains. Calculate them in the constructor; I'm only doing it in the method so it's easier to understand. Even faster would be to work on DateTime properties directly and avoid creating new DateTime objects altogether. As far as this example goes, I opt for simplicity over optimization.
Note that you can compare dates:
if (new DateTime(2012, 1, 1) < new DateTime(2012, 2, 1)) ...
Thus, I would suggest that
you normalize the DOB to a given leap year (e.g. 1904)
and then simply use date comparisons:
DateTime dob = new DateTime(1904, person.DOB.Month, person.DOB.Day);
if (dob >= new DateTime(1904, 12, 21))
return "Aquarius";
else if (dob >= new DateTime(1904, 11, 22))
return "Sagittarius";
else if (dob >= new DateTime(1904, 10, 23))
return "Scorpio";
...
else
return "Aquarius";
An obvious improvement would be to create a List<Tuple<DateTime, String>> and iterate through that. However, since the dates are very unlikely to change in the next hundred years, hardcoding them in the if conditions might suffice.
You can use switch statement
switch (person.DOB.Month)
{
.....
case 12:
if (day >= 22) return "Capricorn"; else return "Sagittarius";
break;
.......
}
May you could build a small Dictionary of sun-sign, which stors the Name of the sun sign as key and it's timespan as value. The timespan would be the first to the last date.
Then there a standard time function to tell if the persons DOB is in the timespan. Mayb you need to strip out the year of birth, but that should be easy.
As a final touch you could use linq:
var sunsigns as Dictionary<string, TimeSpan>();
// adding sun-signs here
var sunsign = (from s in sunsigns where (methodToTellIfItsinRange(s)) select s).first();
linq is good.. just use your list...
have a look at this one
public class sing
{
public string singName {
get { return _singName; }
set { _singName = value; }
}
private string _singName;
public DateTime singStart {
get { return _singStart; }
set { _singStart = value; }
}
private DateTime _singStart;
public DateTime singEnd {
get { return _singEnd; }
set { _singEnd = value; }
}
private DateTime _singEnd;
public void findSing(System.DateTime usersDate)
{
List<sing> ListOfSings = new List<sing>();
sing scorpio = new sing();
System.DateTime startD = new System.DateTime(1910, 10, 23);
System.DateTime endD = new System.DateTime(1910, 11, 21);
scorpio.singName = "scorpio";
scorpio.singStart = startD;
scorpio.singEnd = endD;
ListOfSings.Add(scorpio);
//' ....etc all the others
dynamic hismonth = usersDate.Month;
dynamic hisDay = usersDate.Day;
System.DateTime fixedDate = new System.DateTime(1910, hismonth, hisDay);
dynamic q = (from i in ListOfSings where i.singStart >= fixedDate && i.singEnd <= fixedDatei).ToList;
MessageBox.Show("your sing is: " + q.FirstOrDefault.singName);
}
}
The David Božjak's answer is a great choice.
I think the class could be abstract and implemented for every sign.
Also the StartDate, EndDate and the date pass as parameter need to ignore the year.
I made in this way:
public abstract class StarSign
{
public readonly string Name;
public readonly DateTime StartDate;
public readonly DateTime EndDate;
protected StarSign(string name, DateTime startDate, DateTime endDate)
{
Name = name;
StartDate = startDate;
EndDate = endDate;
}
public virtual bool Contains(DateTime date)
{
date = new DateTime(1, date.Month, date.Year);
return date >= StartDate && date <= EndDate;
}
}
public class AquariusStarSign : StarSign
{
public AquariusStarSign()
: base("Aquarius", new DateTime(1, 1, 21), new DateTime(1, 2, 18))
{
}
}
public class CapricornStarSign : StarSign
{
public CapricornStarSign()
: base("Capricorn", new DateTime(1, 12, 21), new DateTime(1, 1, 20))
{
}
public override bool Contains(DateTime date)
{
if (date.Month == StartDate.Month)
return date.Day >= StartDate.Day;
if (date.Month == EndDate.Month)
return date.Day <= EndDate.Day;
return false;
}
}

Getting year and week from date

I need to return year and week of a given date. Sounds simple. But to be right 2012-01-01 have to return 2011-52, because week 1 in 2012 starts January 2th.
To find the week, I use:
GregorianCalendar calw = new GregorianCalendar(GregorianCalendarTypes.Localized);
return calw.GetWeekOfYear(DateTime.Parse("2012-01-01"), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday).ToString();
this return 52. (correct)
But how do I get the Year?
edit:
With the help from here: http://codebetter.com/petervanooijen/2005/09/26/iso-weeknumbers-of-a-date-a-c-implementation/
This seems to work:
private int weekYear(DateTime fromDate)
{
GregorianCalendar cal = new GregorianCalendar(GregorianCalendarTypes.Localized);
int week = weekNumber(fromDate);
int month = cal.GetMonth(fromDate);
int year = cal.GetYear(fromDate);
//week starts after 31st december
if (week > 50 && month == 1)
year = year - 1;
//week starts before 1st January
if (week < 5 && month == 12)
year = year + 1;
return year;
}
private int weekNumber(DateTime fromDate)
{
// Get jan 1st of the year
DateTime startOfYear = fromDate.AddDays(-fromDate.Day + 1).AddMonths(-fromDate.Month + 1);
// Get dec 31st of the year
DateTime endOfYear = startOfYear.AddYears(1).AddDays(-1);
// ISO 8601 weeks start with Monday
// The first week of a year includes the first Thursday
// DayOfWeek returns 0 for sunday up to 6 for saterday
int[] iso8601Correction = { 6, 7, 8, 9, 10, 4, 5 };
int nds = fromDate.Subtract(startOfYear).Days + iso8601Correction[(int)startOfYear.DayOfWeek];
int wk = nds / 7;
switch (wk)
{
case 0:
// Return weeknumber of dec 31st of the previous year
return weekNumber(startOfYear.AddDays(-1));
case 53:
// If dec 31st falls before thursday it is week 01 of next year
if (endOfYear.DayOfWeek < DayOfWeek.Thursday)
return 1;
else
return wk;
default: return wk;
}
}
Noda Time handles this for you very easily:
Noda Time v1.x
using System;
using NodaTime;
public class Test
{
static void Main()
{
LocalDate date = new LocalDate(2012, 1, 1);
Console.WriteLine($"WeekYear: {date.WeekYear}"); // 2011
Console.WriteLine($"WeekOfWeekYear: {date.WeekOfWeekYear}"); // 52
}
}
Noda Time v2.x
using System;
using NodaTime;
using NodaTime.Calendars;
public class Test
{
static void Main()
{
LocalDate date = new LocalDate(2012, 1, 1);
IWeekYearRule rule = WeekYearRules.Iso;
Console.WriteLine($"WeekYear: {rule.GetWeekYear(date)}"); // 2011
Console.WriteLine($"WeekOfWeekYear: {rule.GetWeekOfWeekYear(date)}"); // 52
}
}
That's using the ISO calendar system where the week year starts in the first week with at least 4 days in that year. (Like CalendarWeekRule.FirstFourDayWeek.) If you want a different calendar system, specify it in the LocalDate constructor. Week year rules are handled slightly differently between 1.x and 2.x.
EDIT: Note that this gives the right value for both this situation (where the week-year is less than the calendar year) and the situation at the other end of the year, where the week-year can be more than the calendar year. For example, December 31st 2012 is in week 1 of week-year 2013.
That's the beauty of having a library do this for you: its job is to understand this sort of thing. Your code shouldn't have to worry about it. You should just be able to ask for what you want.
You can get the weeknumber according to the CalendarWeekRule in this way:
var d = new DateTime(2012, 01, 01);
System.Globalization.CultureInfo cul = System.Globalization.CultureInfo.CurrentCulture;
var firstDayWeek = cul.Calendar.GetWeekOfYear(
d,
System.Globalization.CalendarWeekRule.FirstDay,
DayOfWeek.Monday);
int weekNum = cul.Calendar.GetWeekOfYear(
d,
System.Globalization.CalendarWeekRule.FirstFourDayWeek,
DayOfWeek.Monday);
int year = weekNum >= 52 && d.Month == 1 ? d.Year - 1 : d.Year;
You probably want to compare CalendarWeekRule.FirstDay with CalendarWeekRule.FirstFourDayWeek. On this way you get the weeknumber and the year (DateTime.Year-1 if they differ).
CultureInfo.Calendar Property
Calendar.GetWeekOfYear Method
CalendarWeekRule Enumeration
That is just an edge case which you will have to add special code for. Get the year from the date string and then if the week = 52 and the month = 1 then subtract one from the year.
I have solving similar problem where the result should be in "YYYYWW" format. I wanted avoid hardcoded dates and using 3rd party libraries.
My test case was date 1.1.2017 which should return week 201652 (Iso YearWeek)
To get week number I have used thread: Get the correct week number of a given date which returns week number without the year.
Finally the correct year I got from Monday(first day of iso week) of required date:
// returns only week number
// from [Get the correct week number of a given date] thread
public static int GetIso8601WeekOfYear(DateTime time)
{
// 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
DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
{
time = time.AddDays(3);
}
// Return the week of our adjusted day
var week = CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
return week;
}
// returns int YearWeek in format "YYYYWW"
public static int GetIso8601YearWeekOfYear(DateTime time)
{
var delta = (-((time.DayOfWeek - CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek + 7) % 7));
var firstDayofWeek = time.AddDays(delta); // takeMonday
var week = GetIso8601WeekOfYear(time);
var yearWeek = (firstDayofWeek.Year * 100) + week;
return yearWeek;
}
In my approach I'm taking advantage of the fact, that GetWeekOfYear() displays a correct ISO-8601 week number for days with the same year as Thursday of the same week. So I look for Thursday that belongs to the same week as a given date, and then call GetWeekOfYear() on it.
I can't do that trick to get a correct year, as there's no iso8601-compliant method for this, so I make a year adjustment if Thursday belongs to a different year than a given date.
The solution is basically a three-liner:
using System.Globalization;
namespace TESTS
{
class Program
{
static void Main(string[] args)
{
//sample dates with correct week numbers in comments:
string[] dats = new string[] {
"2011-12-31","2012-01-01" //1152
,"2012-12-31","2013-01-01" //1301
,"2013-12-31","2014-01-01" //1401
,"2014-12-31","2015-01-01" //1501
,"2015-12-31", "2016-01-01" //1553
};
foreach (string str in dats)
{
Console.WriteLine("{0} {1}", str, GetCalendarWeek(DateTime.Parse(str)));
}
Console.ReadKey();
}
public static int GetCalendarWeek(DateTime dat)
{
CultureInfo cult = System.Globalization.CultureInfo.CurrentCulture;
// thursday of the same week as dat.
// value__ for Sunday is 0, so I need (true, not division remainder %) mod function to have values 0..6 for monday..sunday
// If you don't like casting Days to int, use some other method of getting that thursday
DateTime thursday = dat.AddDays(mod((int)DayOfWeek.Thursday-1,7) - mod((int)dat.DayOfWeek-1,7));
//week number for thursday:
int wk = cult.Calendar.GetWeekOfYear(thursday, cult.DateTimeFormat.CalendarWeekRule, cult.DateTimeFormat.FirstDayOfWeek);
// year adjustment - if thursday is in different year than dat, there'll be -1 or +1:
int yr = dat.AddYears(thursday.Year-dat.Year).Year;
// return in yyww format:
return 100 * (yr%100) + wk;
}
// true mod - helper function (-1%7=-1, I need -1 mod 7 = 6):
public static int mod(int x, int m)
{
return (x % m + m) % m;
}
}

Date calculations in C#

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.

Categories

Resources