Best pattern for a monthly billing cycle - c#

I wrote some code for my new billing system. The purpose is to bill the customer on the same day each month. (not the 1st or last day of the month)
static bool NeedToBill(DateTime planLastBilled, DateTime cycleDate)
{
// is today the same date as the cycleDate AND is was the planLastBilled not the same day as today?
if (DateTime.UtcNow.Day.Equals(cycleDate.Day) && !DateTime.UtcNow.Day.Equals(planLastBilled))
return true;
else
return false;
}
The 2 pitfalls are:
If his cycleDate.Day is the 31 and the current month only has 29 days
cycleDate is Feb 29 2012 - he will only get billed on leap years
Is there a common best practice here?
so it seems like there's a bunch things to check
has this account already been billed this month?
does the cycle day exists in the current month
is the cycle day greater than or equal to the current date (this is ideal if
the transaction failed the day before)
Thanks!

Only allow the choice of a billing day between 1 - 28. In my experience this is how most credit card / loan companies deal with it when given a choice.

What does the same day each month mean?
If I am a customer, I want to be billed on the 16th each month. No problem. If I want to be billed on the 31st on each month the obvious issue is not all months have 31 days as you've pointed out in your question.
Why not check the current month for the number of days. If it has less than 31 days, make the last day of the month the bill date.
Is there more to the problem?

I'd say make him choose between 1-28, or any day but charge on the last day on the month if the current month has less days than the chosen day of month.

Ok, I believe I have been totally over-thinking this.
This is simple and covers everything:
bool NeedToBill = ((DateTime.UTCNow – LastBillDate) >= 30 Days)
It will not necessarily bill on the exact same day, however it's close enough.
This also adds flexibility if the transaction was denied for a day, or if the scheduled task was not ran for 1 day the next time it runs it will pick it up.

Related

How to calculate available vacation days?

I need to calculate number of vacation days of employee.
For example, he is working from 2020-05-13 to 2020-08-26.
If his first work day was from 1 to 15 - he gets 2 vacation days;
From 15 to 25 - 1 day;
From 26 to 31 - 0 days.
He get's 2 vacation days in the begining of each month.
And he could be on vacations on this period, so I need to decreace this value from general number of his vacation days.
There is a table with working (1) and vacation (3) days. I need to use it to calculate, how much days of vacation left.
This will a good place to start, https://learn.microsoft.com/en-us/dotnet/api/system.timespan?view=netcore-3.1

Get the week number from a date time [duplicate]

This question already has answers here:
Get the correct week number of a given date
(20 answers)
Closed 4 years ago.
I am trying to get the week number from a date time and in my case, the first day of the week is Monday and I want to follow the FirstFourDays convention.
To check the results, I am checking this webpage:
https://espanol.epochconverter.com/semanas/2020
To get the week number, I using the method:
System.Globalization.CultureInfo.InvariantCulture.Calendar.GetWeekOfYear();
So I am trying to get the week number of the date 2019-12-29, so I use this code:
System.Globalization.CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(new DateTime(2019, 12, 29), System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
The result is week 52. It is correct.
Now I am trying to get the week number of the 2019-12-30, the week number that I get is 53, it is wrong, because 2019 has only 52 weeks. In fact, 2019-12-30 belongs to the same week than 2020-01-01, that it is week 1, that is correct, so I don't understand why I can get two different results for the same date.
How I could get the correct result always? Or how would it be the correct way to get the week number of any date?
There's a blog article explaining this behavior and proposing a solution.
The issue:
Several people have noticed that Calendar.GetWeekOfYear() is almost like the ISO 8601 week when passed CalendarWeekRule.FirstFourDayWeek and DayOfWeek.Monday, however it is a little bit different. Specifically ISO 8601 always has 7 day weeks. If the first partial week of a year doesn't contain Thursday, then it is counted as the last week of the previous year. Likewise, if the last week of the previous year doesn't contain Thursday then its treated like the first week of the next year. GetWeekOfYear() has the first behavior, but not the second.
The proposed solution would be this:
A simple workaround to consistently get the ISO 8601 week is to realize that consecutive days Monday through Sunday in ISO 8601 weeks all have the same week #. So Monday has the same week # as Thursday. Since Thursday is the critical day for determining when the week starts each year my solution is to add 3 days if the day is Monday, Tuesday or Wednesday. The adjusted days are still in the same week, and use values that GetWeekOfYear and ISO 8601 agree on.
// This presumes that weeks start with Monday.
// Week 1 is the 1st week of the year with a Thursday in it.
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 = cal.GetDayOfWeek(time);
if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
{
time = time.AddDays(3);
}
// Return the week of our adjusted day
return cal.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}
All credits for this go to Shawn Steele.

How to get the remaining time from 24 hours in C#

This is currentTime(EST) "2015-09-20 04:25:49.090". I need the below calculation.
Remaining time of "2015-09-20 04:25:49.090" this day (Ex : remaining time of this day is : 19 hours 35 minutes)
againg i have subtract from 24 hours - remainingTime (With minutes)
Could you please suggest me the solution for this?
Regards,
Arun
Sure - you do exactly as you've described, using TimeSpan. You can get the time of day from a DateTime using the TimeOfDay property, and then just subtract that from 24 hours:
// You could use TimeSpan.FromDays(1) as well
var remaining = TimeSpan.FromHours(24) - dateTime.TimeOfDay;
Now, one thing to be wary of is that this doesn't necessarily give you the amount of time left in the day - because "local" days can be different lengths depending on time zone changes (e.g. for daylight saving time). If you need to take that into account (e.g. that on November 1st at 00:30 in New York, there's 24 1/2 hours left in the day...). That's a more complicated question - especially if you also need to take account of time zones where the day doesn't always start at 00:00.
As for the second part of getting "24 hours - remaining time" - that's just "the time of day", as you've got 24 hours - (24 hours - x) which is just x.

How to get list of monthly periods?

I have starting date and number of months. I need to create specific number of monthly periods for example:
var startingDate = new DateTime(2010,1,15);
var months = 3;
for (int i = 0; i < months; i++)
{
Console.WriteLine("{0} from {1} to {2}", i + 1, startingDate.AddMonths(i),
startingDate.AddMonths(i + 1).AddDays(-1));
}
OUTPUT:
1 from 2010-1-15 to 2010-2-14
2 from 2010-2-15 to 2010-3-14
3 from 2010-3-15 to 2010-4-14
In this case code is simple and it works.
However when startDate is DateTime(2010,1,31) result is:
OUTPUT:
1 from 2010-1-31 to 2010-2-27
2 from 2010-2-28 to 2010-3-30
3 from 2010-3-31 to 2010-4-29
Are these periods correct?
The periods do look funky but they are correct.
If your periods are starting on the last day of the month they will end on the second to last day of the next month.
You get to choose if they are right or wrong. Here is what I mean :
If you start your period on the 15th of January, are you starting 14 days after the first day of the month, or are you starting 16 days before the last?
It can get even more tricky. If the 15th is a Tuesday, is your period defined as starting the 3rd Tuesday of a given month?
There is a lot of literature about this in the financial community, since the Day Count Conventions, the Business Days, Rolling Conventions, etc. can make a lot of difference in the pricing of a financial product, and in the cash flows associated with it.

how do I get the dates of even and the odd weeks by using c#

how do I get the dates even and the odd weeks by using c#
must account for the
start of month given by user
and also accordingly the end of month with resect to the number of days possible in the month
You can use Calendar.GetWeekOfYear to get the week number of the week containing any date. You can get the week number of the first day in the month, and the last day. Then you just need to find all the even/odd numbers between those two week numbers. The test x % 2 == 0 will tell you if x is even.

Categories

Resources