How to compare current and manual NSDate - iphone

there is a function where option NSDate is specified putting up manually.
How can I prevent execution of the function with the parameter of time, less than the current time?
I have tried this
if ([currentDate timeIntervalSinceDate: myTime] < 0)
but unfortunately sometimes it doesn't work
Thanks in advance.

Another option is to convert both the date to timeIntervals using timeIntervalSince1970
int interval1 = [date1 timeIntervalSince1970];
int interval2 = [date2 timeIntervalSince1970];
if(interval1 > interval2) //etc...

It sounds like you want to compare the current date with your custom date. More specifically, if the current date takes place after myTime, then the condition should be true.
if ([[NSDate date] compare: myTime] == NSOrderedDescending) {
NSLog(#"Current date is later than myTime");
}
To the opposite, you would check the comparison against NSOrderedAscending.

Related

Comparing NSDate to [NSDate date] Fastest Way?

I have a core data table view and I am comparing dates. The method which I currently use is: if ([todayDate compare: [NSDate date]]==NSOrderedAscending) . This works perfectly but slow. I do not need to know the difference in time though. Any help is much appreciated!
I really think, that NSDates method isEqualToDate: is what you are searching for. Seems to me to be the Apple-way to answer your question:
NSDate *date1 = ...;
NSDate *date2 = ...;
BOOL datesAreEqual = [date1 isEqualToDate:date2];
For more information visit https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsdate_Class/Reference/Reference.html
One option could be to not actually create a new NSDate object but use the time interval for comparison. Don't know about the performance, but it might be worth a try.
if ([todayDate timeIntervalSinceReferenceDate] > [NSDate timeIntervalSinceReferenceDate]) {
...
}
You should remember the current date or current timestamp in a local variable:
NSTimeInterval current = [NSDate timeIntervalSinceReferenceDate];
And use this value later for all your comparisons:
myTimestamp = [myDate timeIntervalSinceReferenceDate]
if (myTimestamp == current) {
return NSOrderedSame;
} else if (myTimestamp > current) {
return NSOrderedDescending;
} else {
return NSOrderedAscending;
}
Or a faster way, using C functions:
// Get the current calendar time as a time_t object.
time_t time ( time_t * timer );
// Return difference between two times
double difftime ( time_t time2, time_t time1 );
I've not measured it, but you may want to try:
NSTimeInterval i = [todayDate timeIntervalSinceNow];
where the result (i) may be positive or negative.
You might also try CFDateCompare.
Or you might want to consider another way to represent a point in time in your database -- such as a CFTimeInterval (a double representing the number of seconds from a common reference time).

How can validate NSDATE

How to find one text field value is within past 60 day excluding current date.
For example if I enter value in text field is 20-July-2012 using Date Picker.Then I click submit,it'll check that specific is date is within 60 days or not. If the values are entered which is before 60 days an alert message is displayed. The values are retrieved from api.
NSDate *today = [NSDate date];
NSTimeInterval dateTime;
if ([pickerDate isEqualToDate:today]) //pickerDate is a NSDate
{
NSLog (#"Dates are equal");
}
dateTime = ([pickerDate timeIntervalSinceDate:today] / 86400);
if(dateTime < 0) //Check if visit date is a past date, dateTime returns - val
{
NSLog (#"Past Date");
}
else
{
NSLog (#"Future Date");
}
Change the value of 86400 to suit your query.In this case, it is the number of seconds we want to compare.
First, convert the text into an NSDate. Then use
timeIntervalSinceDate:[NSDate dateWithTimeIntervalSinceNow:0]
There are a couple of ways to convert text into an NSDate. You can format the text correctly and then use dateWithString or you can convert everything into numbers, multiply them out, and one of the dateWithTimeInterval methods.
If you want the user to be able to enter "July" (plain text month) then you might want to write a method that converts months into their numerical equivalents with string matching.
NSDate *lastDate; //your date I hope you have created it
NSDate *todaysDate = [NSDate date];
NSTimeInterval lastDiff = [lastDate timeIntervalSinceNow];
NSTimeInterval todaysDiff = [todaysDate timeIntervalSinceNow];
NSTimeInterval dateDiff = lastDiff - todaysDiff; // number of seconds
int days = dateDiff/(60*60*24); // 5.8 would become 5 as I'm taking int
How do you define 60 days?
You may want to use NSCalendar -dateByAddingComponents:toDate:options: to ensure your 60 days really are 60 days.
NSCalendar also provides -components:fromDate: and -dateFromComponents: which are very nice when dealing with date components.
If 60 days do not need to be true calendar days (daylight saving time switches, astronomical time corrections, stuff like that), you can just have fun with NSDate and the time interval methods alone.

Time as a value to compare against my value

How can I display time as a number value for an if statement?
I'm trying to write an if statement where a set time is compared against my computed time for an event to occur. I just don't know how to write that time as a value. how can I write a time like 10am as a set time value for example.
Any help is much appreciated.
You can use the NSDate class and the timeIntervalSinceDate: method to compare the two times. You need to set up the two NSDate objects with the different times (and the same date) and then call that method to compare the two.
The easiest way to do this is probably to convert the time to a UNIX timestamp (unsigned integer representing the number of seconds which have elapsed since january 1st 1970)
convert both times to this format and compare.
time_t unixTime = (time_t) [[NSDate date] timeIntervalSince1970];
To compare two dates use:
- (NSComparisonResult)compare:(NSDate *)anotherDate
Here is the definition of NSComparisonResult:
enum {
NSOrderedAscending = -1,
NSOrderedSame,
NSOrderedDescending
};
typedef NSInteger NSComparisonResult;

how to store time in NSDate without date?

I have a timer in my app. When I click on exit buton then timer gets stop and stores value into the string in format of 01:15:55 . I have an array to store this string object.
What I want is , now I want to display these values by comparing to each other. So I think first I have to convert the string into the NSDate but I am having only time format and do not want to store date.
How can I accomplish this task ? any suggestion ?
EDITED : code
NSInteger secondsSinceStart = (NSInteger)[[NSDate date] timeIntervalSinceDate:sDate]; // sDate = when app get started
myAppDelegate.seconds = secondsSinceStart % 60;
myAppDelegate.minutes = (secondsSinceStart / 60) % 60;
myAppDelegate.hours = secondsSinceStart / (60 * 60);
NSString *result = nil;
if (myAppDelegate.hours > 0)
{
result = [NSString stringWithFormat:#"%02d:%02d:%02d", myAppDelegate.hours, myAppDelegate.minutes, myAppDelegate.seconds];
}
else
{
result = [NSString stringWithFormat:#"%02d:%02d", myAppDelegate.minutes, myAppDelegate.seconds];
}
NSString *tempDateString = [NSString stringWithFormat:#"%d:%d:%d",[myAppDelegate hours],[myAppDelegate minutes],[mogsApp seconds]];
Now I want to convert tempDateString into the NSDate so I can compare with similar objects. Is it possible ?
Thanks...
Sounds like an NSTimeInterval might be more appropriate. This is just a floating-point value indicating a number of seconds (including fractional seconds). You can manually format a value like this into whatever string format you want with some simple division and remainder math. (NSDate will give you time intervals since a reference date or other dates if you want to use those to get the values.) You can store NSTimeIntervals as strings if necessary.
NSDateComponents is always a good choice when storing only parts of a date/time (or a timespan).
It also gives you easy access to time management methods via NSCalendar. Then (unlike using NSTimeInterval), you don't have to set up any of the math yourself, and it will all automagically localize.

finding NSDate's in an NSArray that have a time of day after a given time (e.g. 8pm)?

Is there a quick way in Objective-C of identifying NSDate's in an NSArray that have a time of day after a given time (e.g. 8pm)?
I can't quite see anyway other than manually walking through each NSDate in the array and then using NSDateComponents to break out the hour/minute/second...Not even sure if there is a simple way to get the time from an NSDate in a fashion that represents a fraction of 24hours, as this might help a little. (e.g. 6pm would be 18/24 = 0.75 in this case)
There is no need to break in NSDateComponents.
NSTimeInterval interval = [date1 timeIntervalSinceDate:date2];
if (interval > 0) {
// date2 is earlier
} else {
// date1 is earlier
}
Now you can represent your target time(8 P.M., for example) with date2 and compare all dates of array with that.
Haven't tried this myself, but I guess
- (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate
is what you're looking for.