How to get the remaining time from 24 hours in C# - 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.

Related

Find all available combinations of bookings that can be made from a list of time periods

I'm trying to find all available bookings that could be made given a list of time periods and a booking duration.
For example if I have a list of 10 minute timespans and a booking duration of 20 minutes, I need to find all combinations of the 10 minute timespans that would produce a 20 minute slot.
Example list of durations:
10:00 - 10:10
10:10 - 10:20
10:20 - 10:30
10:50 - 11:00
11:30 - 11:40
11:40 - 11:50
would produce the list:
10:00 - 10:20
10:10 - 10:30
11:30 - 11:50
For the solution the input doesn't need to be timespans, it could be datetimes or even start datetime and a duration, I just made it timespans for ease of reading.
I've solved this in the past with a while loop that keeps checking the next and next slots whilst keeping track of the running duration until it reaches one that is not consecutive or the duration has been hit/exceeded and it would do this for every element in the list. I'm not sure if that is the optimal way of doing it so I thought I'd ask here
Cheers

Partly negative period between 2 dates with NodaTime

I use NodaTime library to get period between two days, but sometimes I get partly negative period between two dates:
var start = DateTime.Now;
var end = start.AddDays(7).AddMinutes(-1);
//6 days, 24 hours and -1 minutes
var period = Period.Between(LocalDateTime.FromDateTime(start), LocalDateTime.FromDateTime(end));
How can I prevent such behavior?
At this moment I already have found one solution:
//6 days, 23 hours and 59 minutes, but no weeks, months, years
var duration = period.ToDuration();
But duration object is not contrain weeks, months, years and so on.
TL;DR: Update to Noda Time 2.0.2.
This was a bug in 2.0.0 and 2.0.1 of Noda Time. Brief testing suggests it wasn't an issue in earlier versions.
The problem was due to an optimization when trying to obtain the time parts of a period between two LocalDateTime values. Consider finding the difference between these values using all units:
start: 2017-05-22T12:00:00
end: 2017-05-25T11:30:00
We'd start by finding the days portion: 2 days (not 3, because that would overshoot). That would leave us finding the time units between:
updated-start: 2017-05-24T12:00:00
end: 2017-05-25T11:30:00
When trying to find the number of hours, it would first find the number of days between the two dates, then multiply by 24, and then the number of hours between the results - so we'd end up with 24 hours, which then overshot by 30 minutes.
The fix for this was basically an overhaul of Period - see PR 825 and PR 826. PR 825 was then cherry-picked into the 2.0.x branch, and 2.0.2 was released on May 22nd 2017.
With the help of Jon Skeet, this bug will be fixed in 2.0.2 version of NodaTime.

Is there a way to convert a DateTimeOffset into minutes since midnight?

Is there a way to convert a DateTimeOffset into minutes since midnight?
I want the offset part to be reflected in the answer it gives.
dateTimeOffset.UtcDateTime.TimeOfDay.TotalMinutes
returns the number of minutes since midnight for the original date/time from which the offset was subtracted
so if date/time = Jan 2 2013 and offset =8 hours, this will return 16 hours (since Jan 1 midnight)

Calculate time difference using TimeSpan

I am trying to calculate the time difference of two given times slots but the answers does not seem to be correct what am I doing wrong?
My code:
For some reason the value given amFinish is changed from 16:30 to 16:18:00 I have no idea why!
And What if I have a text box and the user enters 16.30 how would I take that value and compute it as 16hrs and 30mins
The answer should be 05.30 but instead I get 05.18. Any sugestions?
30% of an hour is 18 minutes.
16.30 hours is 16 hours and 30/100 parts of an hour.
16 hours and a half would be 16.50.
You have decimal 16.3 hours, which is 16 hours and 18 minutes as Oded explains.
If you need to specify both hours, minutes, and seconds, use the overload of the TimeSpan constructor which takes three arguments:
TimeSpan amStart = new TimeSpan(0, 11, 0);
TimeSpan amFinish = new TimeSpan(16, 30, 0);
And if you need to convert a string into a TimeSpan, use something like:
TimeSpan amFinish = TimeSpan.ParseExact("16.30", #"hh\.mm", CultureInfo.InvariantCulture);

Best pattern for a monthly billing cycle

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.

Categories

Resources