There is a object called Place. user can add place to favorite and also user can remove place from the favorite. once user added to the favorite I save date on the database. when user remove from the favorite I retrieve date from the db. and compare particular object with array of objects. but it gives NSOrderedAscending when comparing same object.
NSDate *date1 = obj1.date; // same date
NSDate *date2 = obj2.date; // same date
// compare using date
NSComparisonResult result = [date2 compare:date1];
any help would be appreciate. Thanks.
Try do this:
-(NSDate *)clearSecondsFromDate:(NSDate *)date
{
NSTimeInterval timeInterval = [date timeIntervalSinceReferenceDate];
timeInterval -= fmod(timeInterval, 60);
NSDate *clearDate = [NSDate dateWithTimeIntervalSinceReferenceDate:: timeInterval];
return clearDate;
}
If you want to compare two dates are equivalent to the same second, then get the number of seconds since some epoch and round off the fractional part:
BOOL same = round([date1 timeIntervalSince1970]) ==
round([date2 timeIntervalSince1970]);
Or convert to an integer:
BOOL same = (unsigned long)[date1 timeIntervalSince1970] ==
(unsigned long)[date2 timeIntervalSince1970];
(you can also use [NSDate timeIntervalSinceReferenceDate] if you like, as well).
NSDate doesn't just contain day and seconds, it has a resolution better than microseconds. Your NSDates are probably some fraction of a second apart. Try to log [date1 timeIntervalSinceDate:date2].
So most likely your dates are just not the same. If you are sorting dates, and you want to have dates within the same second considered equal, take
double seconds1 = round ([date1 timeIntervalSinceReferenceDate]);
double seconds2 = round ([date2 timeIntervalSinceReferenceDate]);
which takes the time in a date and rounds it to the nearest second, and compare those values.
Related
Through my App users can order specific food item in restaurants..The thing is that if a user want to order a specific product he can give atleast 6 hrs time for the restaurant to deliver the product.For example you are the user,right now time is 12:00pm..In the app,user can select the timings of DELIVERY through UIDatepicker.Here if user selects the time before 06:00pm,one alert view will generate with message "please give 6hrs time for your order".Here i have to compare system time and UIDatepickers time.I found one method,,but it is not useful.
if ([self.datePicker.date compare:currentdate] == NSOrderedDescending){
NSLog(#"Descending");
}
Please help me...Thanks in advance
Either you can compare the selected time by
NSDate *date1 = [NSDate date];
NSDate *date2 = datePicker.date;
NSTimeInterval elapsed = [date1 timeIntervalSinceDate:date2];
Also you can set minimum time of date picker after 6 hours
NSDate *mydate = [NSDate date];
NSTimeInterval secondsInSixHours = 6 * 60 * 60;
NSDate *dateSixHoursAhead = [mydate dateByAddingTimeInterval:secondsInSixHours];
[datePicker setMinimumDate:dateSixHoursAhead];
use this
if ([[NSDate date] isEqualToDate: currentdate) {
NSLog(#"currentDate is equal to currentdate");
}
You are actually looking for time difference , here is the code :
NSLog(#"please give %#fhrs time for your order",[self.datePicker.date timeIntervalSinceDate:[NSDate date]]);
Use this method. The best way of doing comparison between two dates:
- (void)pickerDateIsSmallerThanCurrent:(NSDate *)checkDate
{
NSDate* enddate = checkDate;
NSDate* currentdate = [NSDate date];
NSTimeInterval distanceBetweenDates = [enddate timeIntervalSinceDate:currentdate];
double secondsInMinute = 60;
NSInteger secondsBetweenDates = distanceBetweenDates / secondsInMinute;
if (secondsBetweenDates == 0)
//both dates are equal
else if (secondsBetweenDates < 0)
//selected date is smaller than current date
else
//selected date is greater than current date
}
This question already has answers here:
how to split strings in objective c
(4 answers)
Closed 8 years ago.
NOTE: I have used the share knowledge feature to answer this question as a whole and not simply one question which is commonly asked - "Splitting strings"
2 questions I have come across but never together are:
How do I split Strings? (I acknowledge this question alone has been answered a few times)
How do I calculate the difference between 2 times as strings and display in time format (00:00:00)?
The purpose of this question is to help people who are storing times are strings in core data to be able to calculate the difference between the two. I used the "share knowledge" feature to answer this question and have not simply answered. Please see below.
Since you're coming from strings, you'll need to change them to NSDate objects. You'll use a formatter to do so.
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"HH:mm:ss"];
// I don't know what date format you're coming from, you should of course use yours. check out the doc for more info.
NSDate *date1 = [dateFormat dateFromString:string1];
NSDate *date2 = [dateFormat dateFromString:string2];
Now you have date objects that are coming from your strings.
If you want to know the time between time dates (in seconds or other)
(from this post)
NSTimeInterval distanceBetweenDates = [date1 timeIntervalSinceDate:date2];
double secondsInAnHour = 3600;
NSInteger hoursBetweenDates = distanceBetweenDates / secondsInAnHour;
NSTimeInterval gives you the difference in seconds, you can just calculate from there if you want hours, days, etc.
But sometimes, you might only need to know if a date is before or after another. Then you should use date compare, which returns a NSOrderingDesc or Asc or Same , like this:
//Then the comparison will tell which is earlier/later/same:
if ([date1 compare:date2] == NSOrderedDescending) {
NSLog(#"date1 is later than date2");
} else if ([date1 compare:date2] == NSOrderedAscending) {
NSLog(#"date1 is earlier than date2");
} else {
NSLog(#"dates are the same");
}
First we establish the format of the date like so. I am using HH:MM:SS format. Format yours to suit your saved string.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:#"HH:mm:ss"];
The second part is referencing your strings. I create a new reference just to keep it tidy when converting String Date to NSDate. In my example I have declared a reference to Core Data in my .h file so I reference as follows.
NSString * StringTime1 = self.coreDataReference.time1;
NSString * StringTime2 = self.coreDataReference.time2;
Convert the strings to date:
NSDate *time1 = [dateFormat dateFromString:StringTime1];
NSDate *time2 = [dateFormat dateFromString:StringTime2];
I next get the difference by getting the time interval between the times. I am comparing the latest saved time (time2) against the first save time (time1). This method essentially subtracts whatever time is allocated second from the first, which in this case tim2 subtracts time1. You will see why in a moment.
NSTimeInterval distanceBetweenDates = [time2 timeIntervalSinceDate:time1];
For displaying purposes, I convert the time which is now a double (NSTimeInterval is a double, check https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html for more information) and then convert them into hours, minutes and second values.
int timeInt = (int) distanceBetweenDates;
//I convert the values into the time value
int hours = timeInt / 3600;
int minutes = (timeInt / 60) % 60;
int seconds = timeInt % 60;
FInally we display the results in log and if you have a label display there as well.
NSLog(#"The total time is: %02u:%02u:%02u", hours, minutes, seconds);
NSString * time = [NSString stringWithFormat:#"%02u:%02u:%02u", hours, minutes, seconds];
self.totalLoginTimeLabel.text = time;
I hope this helps anyone investigating.
The whole code here:
-(void) calculateTimeIntervalFromStrings{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:#"HH:mm:ss"];
NSString * StringTime1 = self.coreDataReference.time1;
NSString * StringTime2 = self.coreDataReference.time2;
NSDate *time1 = [dateFormat dateFromString:StringTime1];
NSDate *time2 = [dateFormat dateFromString:StringTime2];
NSTimeInterval distanceBetweenDates = [time2 timeIntervalSinceDate:time1];
int timeInt = (int) distanceBetweenDates;
//I convert the values into the time value
int hours = timeInt / 3600;
int minutes = (timeInt / 60) % 60;
int seconds = timeInt % 60;
NSLog(#"The total time is: %02u:%02u:%02u", hours, minutes, seconds);
NSString * time = [NSString stringWithFormat:#"%02u:%02u:%02u", hours, minutes, seconds];
self.timeDifferenceLabel.text = time;
}
To call this method, place in your viewDidLoad method like so:
[self calculateTimeIntervalFromStrings];
I am trying to implement some pseudo code I have for the date picker. However I am unsure of how to add a minute value to adjust an NSDate object.
Here is the pseudo code:
//minTime is an NSDate object
minTime = currentTime + 30mins - (currentTime % 15)
(currentTime % 15) means that the user can only select in 15mins intervals, and must be 15mins from the current 15min interval. For example, if its 10:50, the user should only be able to select 11:15 from the UIDatePicker. If is 10:20, the user should only be able to select 10:45.
I know how to get the currentTime using [NSDate date] but I do not know how to add mins to it and adjust it.
It is not considered good practice to work with time intervals when working with dates and times. The best solution is to use NSDateComponents to add time periods.
NSDateComponents* dc = [NSDateComponents new];
dc.minutes = 15;
NSDate* newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dc toDate:oldDate options:0];
You can add some minutes to a NSDate using :
NSDate *nowDate = [NSDate date];
NSDate *nowDateAnd2moreMinutes = [nowDate dateByAddingTimeInterval:2*60]; //This add 2 minutes (2 * 60sec)
More information in apple documentation.
Edit I wrote a little function that add minutes :
+(NSDate) addMinutes:(int) minutes toDate:(NSDate) date{
return [date dateByAddingTimeInterval:minutes*60];
}
Bonus : Function that add minutes and seconds to a date
+(NSDate) addMinutes:(int) minutes andSeconds:(int) sec toDate:(NSDate) date{
return [date dateByAddingTimeInterval:(minutes*60)+sec];
}
I have a current NSString in the format of 2010-04-23 00:00:00 and then I'm trying to get the number of days passed from the current day. However, I'm not sure how to handle when the user changes their locale to Thailand for example.
Here is some of the code.
NSString *start = #"2010-04-23 00:00:00";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSDate *date = [formatter dateFromString:start];
//Region Format Thailand
NSDate *today = [[NSDate alloc] init];
NSTimeInterval difference = [today timeIntervalSinceDate:start];
int numberOfDays = difference / 86400;
What would be the correct way to handle this situation so the number of days difference is accurate?
Dates are complicated.
If you want a difference in days, hours, minutes, seconds, that's easy: Convert everything to NSDate, calculate the difference in seconds, convert to days, hours, minutes, seconds.
Anything else, you need to first define what results you actually want. Today at 1am and 11pm is the same day, but today 11pm and tomorrow 1am are different days - even though in the first case the difference is 22 hours, in the second case just two hours. So you have to define what you want. You have to define for which case you want a result of "0 days" and for which case you want a result of "1 days".
And if you change time zones, some dates will move to a different day, some won't.
It's up to you to decide what result you want. In any case, I'd convert all dates to the relevant time zone, extract the day, and calculate days differences from that.
You need to convert the date into epoch time.
- (NSTimeInterval)timeIntervalSince1970
After you do that you can use the below code, to find the time difference in seconds and compare them.
NSDate* date1 = [NSDate date];
NSDate* date2 = [NSDate date];
NSTimeInterval secs = [date1 timeIntervalSinceDate:date2];
if (secs < 0)
{
NSLog("less");
}
else if (secs > 0)
{
NSLog("greater");
}
else
{
NSLog(#"same");
return NSOrderedSame;
}
I have an entity with an attribute time, which is of type Date. I only care about the time component of it. When generating the class files, I chose to use scalar properties for primitive data types. So, NSTimeInterval is used instead of NSDate.
To store the time attribute , I has to parse strings with NSDateFormatter before assigning it to the entity:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"H:mm"];
NSTimeInterval time = [[dateFormatter dateFromString:#"19:20"] timeIntervalSince1970];
entity.time = time;
When using a predicate to fetch the records, I again used NSDateFormatter to "truncate" the date component:
NSDate *timeNow = [self.defaultDateFormatter dateFromString:
[self.defaultDateFormatter stringFromDate:[NSDate date]]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"time >= %#", timeNow];
// build the rest of the NSFetchRequest...
The problem is, all records came back: some records had time attributes before timeNow and some were after. Bue when I compared timeNow with the time attributes of the returned records using [NSDate compare:], it actually returned the right outcomes!
Where went wrong?
If I understand well, you need to keep track of the time H:mm. The day, month and year is of no interest. Right?
Why not using an int16_t if you need minute resolution (max 1440) or an int32_t if you need second resolution (max 86400), and some very basic methods to convert from the base unit to something that suits your needs best like NSDateComponents for NSCalendar calculations? NSDate and NSTimeInterval aren't well suited to the task, are they?