Possible to compare NSDate objects without using calendar components? - ios

I'm looking to compare NSDate objects based on the day only (ignoring time). Instead of converting the time to 0:00:00, or using NSDateComponent like most solutions (ex. Comparing two NSDates and ignoring the time component)
Does anyone see an issue with converting the date to an int representing the number of days since 1970 with the timeIntervalSince1970 method?
return (int)([date timeIntervalSince1970]/(SECONDS_PER_DAY));

Yes, absolutely. There are an endless number of pitfalls with date math. Use NSDateComponents; they’re not hard.
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *date1Components = [cal components:NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:date1];
NSDateComponents *date2Components = [cal components:NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:date2];
NSComparisonResult comparison = [[cal dateFromComponents:date1Components] compare:[cal dateFromComponents:date2Components];

Here's a way to convert an NSDate to an NSTimeInterval that represents midnight of the original date without using NSCalendar. Doing this with two NSDate objects would let you compare the two dates without regard to time.
NSDate *now = [NSDate date]; // Your original date with time
NSTimeInterval interval = [now timeIntervalSince1970]; // the full interval
NSDateFormatter *form = [[NSDateFormatter alloc] init];
[form setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; // Zulu time
[form setDateFormat:#"A"]; // milliseconds since midnight
NSString *secondsStr = [form stringFromDate:now];
NSTimeInterval seconds = [secondsStr integerValue] / 1000.0; // seconds since midnight
NSTimeInterval justDate = interval - seconds; // interval for date at midnight (Zulu time)
// For testing purposes
NSDate *nowDate = [NSDate dateWithTimeIntervalSince1970:justDate];
NSLog(#"now = %#, interval = %f", now, interval);
NSLog(#"seconds = %#", secondsStr);
NSLog(#"justDate = %f, nowDate = %#", justDate, nowDate);
This may or may not be better than using NSCalendar as shown in Noah's answer.
You must definitely not simply divide by SECONDS_PER_DAY. That will simply be wrong.

NSCalendar.currentCalendar().compareDate(date1, toDate: date2, toUnitGranularity: .Day)
Returns NSComparisonResult
Here is what apple talks about it

Related

How to get current seconds left to complete hour?

I want to get how many seconds are remaining to complete an hour. No matter which what time it is?
if its 05:01:00 then it should give 3540 seconds
and if its 11:58:40 then it gives 80 seconds and so on. I try to find it on google but could not able to find it.
Thanks in advance.
NSCalendar has got methods to do that kind of date math:
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
// find next date where the minutes are zero
NSDate *nextHour = [calendar nextDateAfterDate:now matchingUnit:NSCalendarUnitMinute value:0 options:NSCalendarMatchNextTime];
// get the number of seconds between now and next hour
NSDateComponents *componentsToNextHour = [calendar components:NSCalendarUnitSecond fromDate:now toDate:nextHour options:0];
NSLog(#"%ld", componentsToNextHour.second);
#Vadian's answer is very good. (voted)
It requires iOS 8 or later however.
There are other ways you could do this using NSCalendar and NSDateComponents that would work with older OS versions.
You could use componentsFromDate to get the month, day, year, and hour from the current date, then increment the hour value and use the NSCalendar method dateFromComponents: to convert your to adjusted components back to a date.
NSDate *date1 = [NSDate dateWithString:#"2010-01-01 00:00:00 +0000"];
NSDate *date2 = [NSDate dateWithString:#"2010-02-03 00:00:00 +0000"];
NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
Fill in with the correct times and dates to get the difference in seconds
Edit: An alternative method is to work out the currenthour and return as an integer. Then add one to the NSInteger returned as below (you will have to make sure to handle the case where it is after midnight though!)
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];
NSInteger currentHour = [components hour];

NSCalendarUnitSecond returning zero

I have this method returning a string. But the seconds value is always zero. What am I doing wrong?
-(NSString*)secondsBetweenDate:(NSDate*)startDate andDate:(NSDate*)endDate {
NSCalendar *calendar = [NSCalendar currentCalendar];
unsigned int unitFlags = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *difference = [calendar components:unitFlags fromDate:startDate toDate:endDate options:0];
long hour = [difference hour];
long min = [difference minute];
long sec = [difference second];
NSLog(#"Hour: %ld Min: %ld Sec: %ld", hour, min, sec);
return [NSString stringWithFormat:#"%02ld:%02ld:%02ld", hour, min, sec];
}
Why don't you use the default NSDate difference calculations?
Returns an NSTimeInterval which is in seconds:
typedef double NSTimeInterval; Description Used to specify a time
interval, in seconds.
Operation is:
[aDate timeIntervalSinceDate:anotherDate];
Your code is correct. If you use the dates that actually differ in their seconds value it yields the expected results.
[self secondsBetweenDate:[NSDate dateWithTimeIntervalSinceNow:-99999] andDate:[NSDate date]];
logs:
Hour: 27 Min: 46 Sec: 39
And 99999 Seconds is 27*60*60 +46*60 +39
In our discussion in the comments we discovered that you had more of a conceptional problem. If you need to show a countdown or stopwatch type string you have to use the current time as one of the date parameters.
So if you want to show the time that has passed since a specific date (like a stop watch) you use:
NSDate *now = [NSDate date];
string = [self secondsBetweenDate:yourStartDate andDate:now];
If you want to show the time until a specific date (like a countdown) you use:
NSDate *now = [NSDate date];
string = [self secondsBetweenDate:now andDate:yourEndDate];
Btw: If you are only targetting iOS 8 and later you can use NSDateComponentsFormatter to format your date
NSDateComponentsFormatter *df = [[NSDateComponentsFormatter alloc] init];
df.unitsStyle = NSDateComponentsFormatterUnitsStylePositional;
df.allowedUnits = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSString *dateString = [df stringFromDate:startDate toDate:[NSDate date]];

Get progress of day in Objective-C

I need to get the progress of the current day (For example from 6am and 11pm). I have tried many things and the only code I can get working so far is this code:
//Get the seconds between two days
//Get todays date
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
//Set todays date in a NSCalendar object
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *date1 = [gregorian dateFromComponents:components];
//Get tomorrows date
NSDateComponents *components1 = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
components1.day++;
//Set tomorrows date in a NSCalendar object
NSDate *date2 = [gregorian dateFromComponents:components1];
NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
NSLog(#"Test: %f", secondsBetween);
This code will get the amount of seconds between two days in total (which doesn't really matter as it's a constant). How do I get the progress between two different times? As the user will be allowed to change the times it counts between I need to be able to calculate this on the fly in a NSTimer.
Edit: I want to know how to use a ProgressView to display the percentage between two times. E.g. The ProgressView would be 50% if it was 9AM and the times were 6AM and 12PM.
The difference in seconds between two dates is
[date2 timeIntervalSinceDate:date1];
That seems to be all you need for your problem.
you only set day/month/year -- set hours/minutes/seconds too.
right now the dates always point to 00:00:00 and not to any TIME
No problem. I use the following method to determine the difference between NSDate values:
- (NSInteger)secondsBetweenDate:(NSDate*)fromDateTime andDate:(NSDate*)toDateTime {
NSDate *fromDate;
NSDate *toDate;
NSCalendar *calendar = [NSCalendar currentCalendar];
//Returns by reference the starting time and duration for the given dates (with second precision).
[calendar rangeOfUnit:NSCalendarUnitSecond startDate:&fromDate
interval:NULL forDate:fromDateTime];
[calendar rangeOfUnit:NSCalendarUnitSecond startDate:&toDate
interval:NULL forDate:toDateTime];
//Returns the difference between the two dates (with second precision)
NSDateComponents *difference = [calendar components:NSCalendarUnitSecond
fromDate:fromDate toDate:toDate options:0];
//returns the number of seconds
return [difference second];
}
To test the above code, I've used an easier way to create two points in time (rather than many lines you've used above). This is simply used to test and you'll obviously use real values in your actual code
NSDate * myDate1 = [NSDate date]; // date as of right now
NSDate * myDate2 = [[NSDate alloc] initWithTimeIntervalSinceNow:1 * 24 * 60 * 60]; // exactly 24 hours into the future
NSLog(#"Second Between: %ld",(long)[self secondsBetweenDate:myDate1 andDate:myDate2]);
//OUTPUT: Seconds: 86400
You can easily tweak the values above to test certain ranges. For reference, this is what each number stands for:
1 (days) * 24 (hours) * 60 (minutes) * 60 (seconds)
OK, with that sorted you just need to figure out how much time has progressed since your starting time. You can do that with two calls to the method created at the start of this post:
//Assuming dates are sourced from some other code
NSDate * startTime // Set elsewhere in the code
NSDate * currentTime // Set elsewhere in the code
NSDate * endTime // Set elsewhere in the code
long fullDuration = [self secondsBetweenDate:startTime andDate:endTime];
long timeElapsed = [self secondsBetweenDate:startTime andDate:currentTime];
float percentComplete = (float) timeElapsed / (float) fullDuration;
Now, since we're going to be using ProgressView I'm keep this percent value as a number between 0 and 1 and the variable as a float.
Finally, assuming you already have the following done:
Added a ProgressView object to your storyboard
Added an outlet to to the object called something like progressViewOutlet
All you need to do is send the update regularly using your timer using the following code:
self.progressViewOutlet.progress = percentComplete;
You can grab my source code below. Let me know if you have any questions
https://dl.dropboxusercontent.com/u/43038596/testDates.zip

Date Formatting is Weird

I have a date string that looks like this:
1391640679661
When I use this code:
NSString *seconds = #"1391640679661";
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[seconds doubleValue]];
I end up with this:
46069-05-03 07:27:41 +0000
So what's happening here? Is this a particular date format that I'm not accounting for? Or am I doing something else wrong?
Apple's own api will do all the hard work for you to format components as per your need.
If you want to get individual components as well you can apply below approach.
NSTimeInterval theTimeInterval = 1391640679661;
// Get the system calendar
NSCalendar *sysCalendar = [NSCalendar currentCalendar];
// Create the NSDates
NSDate *date1 = [[NSDate alloc] init];
NSDate *date2 = [[NSDate alloc] initWithTimeInterval:theTimeInterval sinceDate:date1];
// Get conversion to months, days, hours, minutes
unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;
NSDateComponents *conversionInfo = [sysCalendar components:unitFlags fromDate:date1 toDate:date2 options:0];
NSLog(#"Conversion: %dmin %dhours %ddays %dmoths",[conversionInfo minute], [conversionInfo hour], [conversionInfo day], [conversionInfo month]);
To convert a timestamp string into NSDate, you need to divid the timestamp double value to 1000, and then call dateWithTimeIntervalSince1970:
NSString *timestamp = #"1391640679661";
double seconds = [timestamp doubleValue]/1000.0;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:seconds];
The result is:
2014-02-05 22:51:19 +0000

How can I set only time in NSDate variable?

I have NSDate variable and would like to change only time (date shouldn't be changed). Is it possible ?
Eg: user pick date interval in DatePicker date (If it's start date I would like to set time as 00:00:00, if it's end date, I set time as 23:59:59)
Thanks all for your help.
Regards, Alex.
You'll want to use NSDateComponents.
NSDate *oldDate = datePicker.date; // Or however you get it.
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [calendar components:unitFlags fromDate:oldDate];
comps.hour = 23;
comps.minute = 59;
comps.second = 59;
NSDate *newDate = [calendar dateFromComponents:comps];
The NSDate object cannot be changed.
You can create a new NSDate object from it. Take a look at the - (id)dateByAddingTimeInterval:(NSTimeInterval)seconds method.
NSDate *newDate = [endDate dateByAddingTimeInterval:24.0f * 60.0f * 60.0f - 1.0f];
When I only need to track the time, I don't use NSDate or NSTimeInterval. Instead I use an NSinteger and store the "military" time, ie 0830 = 8:30 AM, 14:15 = 2:15 PM.

Resources