MVC Getting number of days between calendar start date and end date - asp.net-mvc

I have 2 calendars, one for the user to select the start date and the other for the end date. I want to get the value of the difference between the two dates to display the number of days.
decimal period = Convert.ToDecimal((currentApplication.StartDate.Value - currentApplication.EndDate.Value).TotalDays);
currentApplication.NoOfDays = period;
It does work but number of days isn't accurate.
22/12 to 22/12 is displayed as 1.00
22/12 to 23/12 is displayed as -1.00
22/12 to 24/12 is displayed as -1.00
I thought using .TotalDays would be right but the values aren't accurate. Did I use it wrongly or is .TotalDays not meant to be used this way?

If number of days isn't accurate when using .TotalDays then you can use Timespan to get you number of days.
TimeSpan tSpan = (currentApplication.EndDate.Value).Subtract(currentApplication.StartDate.Value);
currentApplication.NoOfDays = tSpan.Days;
Note: Please make sure currentApplication.StartDate.Value and currentApplication.EndDate.Value are DateTime.

Try;
decimal period = (currentApplication.startDate.Date - currentApplication.endDate.Date).TotalDays

I'm assuming that startDate and endDate are of type Nullable<DateTime>.
You should check HasValue for existence of Value first. This code should work for you.
if(currentApplication.StartDate.HasValue && currentApplication.EndDate.HasValue)
{
currentApplication.NoOfDays = (currentApplication.EndDate.Value.Date - currentApplication.StartDate.Value.Date).Days;
}

Related

How can I subtract two dates and get a new date converted in minutes?

I need to subtract two dates, date1 and date2(current date), using Linq expressions. I need to verify if the subtraction between these two date isn't above 12 hours. I only want to get the data that has less than 12 hours. The code bellow is the code of my controller (GET), right now I'm subtracting the two dates and converting the result in minutes. I'm not sure if that's the right approach.
var date2 = DateTime.Now;
var model= _context.Model.Where(e => (e.Prop1== "hello world" && (e.date1 - date2).Minutes <= 12 * 60));
Right know it shows all the data that I have on my database.
When you subtract two DateTime the result is a TimeSpan
Use TimeSpan.TotalHours property, which simplifies to
... (e.date1 - date2).TotalHours <= 12) ...
based on the desired behavior

Random Time in between two times (a range) iOS SDK

I am trying to create a random time for a notification to occur that is between two times, a range...
I would like iOS to create a time between say 09:30AM and 11:30AM.
I was thinking of using the random number generator for the hours and another one for the minutes and then do some checks to make sure it is between 09:30AM and 11:30 AM but thought there might be an easier way to do it with out getting too involved. any help would be greatly appreciated.
Get an NSTimeInterval for the period between the two dates; get a single random number in that range; get a new date by offset from the first.
/*! Returns a random date in the range [start, end)
#param start The lower bound for random dates; returned dates will be equal to
or after the start date.
#param end The upper bound for random dates; dates will be before the end date.
#returns A random date.
*/
- (NSDate *)randomDateBetweenStart:(NSDate *)start end:(NSDate *)end
{
NSTimeInterval window = [end timeIntervalSinceDate:start];
NSTimeInterval randomOffset = drand48() * window;
return [start dateByAddingTimeInterval:randomOffset];
}
Addendum, a year-and-a-half later: an edit has pointed out that drand48 both has a well-defined sequence (including first value from program launch) and isn't a particularly advanced random number generator. I recommend srand48 or seed48 at program launch if the former is a problem, and something like ((double)arc4random() / UINT32_MAX) in place of drand48 if you want to eliminate both problems — although that reduces the number of output values you can hit from 2^48 to 2^32, it should still get you sub-second decisions within any reasonable time interval.

calculate timeIntervalSinceDate for over a month?

Maybe i am missing something very stupid so forgive me .
Comparing a date that is 1 month later than another date , starts to give me strange numbers.
I guess its because the result is float ? Here is how i do the comparison :
int daysToCheckUses=60;
long seconds=60*60*24*daysToCheckUses;
NSDate *today=[NSDate date];
if([today timeIntervalSinceDate:date]>seconds) //date can be more than 60 days old
Is there something wrong with this,when using big numbers? for example when the interval is 1 month i get 518400,but for 3 months i get 18662400000 i know that the comparisons returns float number that can't hold these numbers. I am afraid also to get a crash when a few months will pass.
This method to calculate a big distance is not that good, i found a great and better way for this using a great answer from here :
Number of days between two NSDates
Where the comparison is for days, than months will only give a few 100's integer .

joda overlap method for ambiguous hour

I'm trying to find out if there is any overlap between two joda intervals. value of the variables are given below. I'm not sure why the overlap is being returned as 'null'.
final Interval overlap = range.overlap(new Interval(beginDateTime, endDateTime));
beginDateTime = 2013-11-03T00:07:00.000Z
endDateTime = 2013-11-03T00:08:00.000Z
range = 2013-11-03T00:00:00.000-05:00/2013-11-03T23:59:59.999-06:00
Interval created by 'beginDateTime' and 'endDateTime' corresponds to ambiguous hour in America/Chicago time zone. Variable 'range' represents November 3rd in America/Chicago time zone.
I tried to debug into Interval.class, could not find out the reason.
thanks.
Never mind, found the issue.
beginDateTime and endDateTime be 2013-11-03T07:00:00.000Z and 2013-11-03T08:00:00.000Z respectively.
My test data was incorrect as listed in the question (that is not ambiguous hour, it is interval of just 1 minute, I swapped minutes with hour values) :)

Calculating how many 'Midnights' is one date past another in PHP?

I have a start/end times for a calculation I'm trying to do and am having a problem seeing if the end time is before 12AM the day after the start time. Also, I need to calculate how many days past the start time it is.
What I have: Start Date, End Date
What I need:
- How many 'Midnights' is the End Date past the Start Date?
Has anyone done anything like this?
This uses PHP 5.3, if you have an earlier version you may need to use unix timestamps to figure out the difference. The number of midnights should be the number of days difference assuming both start and end times have the same time. So setting both to be midnight of their current day setTime(0,0), should make the calculation correct.
Using the DateTime objects.
$start = new DateTime('2011-03-07 12:23:45');
$end = new DateTime('2011-03-08 1:23:45');
$start->setTime(0,0);
$end->setTime(0,0);
$midnights = $start->diff($end)->days;
Without using the setTime() calls, this would result in 0, because there is less than 24 hours between start and end. With the setTime() this results in 1 because now the difference is exactly 24 hours.
The diff() function was introduced in 5.3 along with the DateInterval class. In 5.2 you can still use the DateTime class but will have to work out the total days using the Unix timestamp.
$midnights = ($end->format('U') - $start->format('U')) / 86400
You can wrap that in an abs() function to the order of start/end does not matter.
Note: These functions may need to be tested for cases that involve DST.
A comment in the php date documentation uses round after dividing by 86400 (number of seconds in a day), to counter any issues that could be involved with DST.
An alternative approach with DateTimes would be to create them in the UTC.
$utcTimezone = new DateTimeZone('UTC');
$start = new DateTime('2011-03-07 12:23:45', $utcTimezone);
$end = new DateTime('2011-03-08 1:23:45', $utcTimezone);

Resources