Get time from pressed button (hold pressed) - ios

When trying to get the time, I am not getting the expected value.
When I ran this, the time was 19:59. And in the timeDifferenceString, the Value was 71976.894206.
I think that this is the current time.
I calculate 71976 / 3600 = 19,99 h
0,99 * 60 = 59,4 m
0,4 * 60 = 24 s
So I get the time 19:59:24 o'clock. But I want get the difference between the first time and the second time and not the current time in seconds.
I want only get the time when I (hold) pressed the button
and the time that the button is not pressed (by starting the first time if I press on the button and stopped if I press on an other button).
- (IBAction)pressTheButton:(id)sender {
NSDate * now = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"hh:mm:ss:SSS"];
NSString *currentTime = [formatter stringFromDate:now];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components; //= [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[components setHour:10];
NSDate* firstDate = [NSDate date];
NSString *getTime = [formatter stringFromDate:now];
getTime = [formatter stringFromDate:firstDate];
if (iX==0)
{
components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
firstDate = [calendar dateFromComponents:components];
getTime = [formatter stringFromDate:firstDate];
//firstDate = [NSDate date];//[dateFormatter dateFromString:#"00:01:00:090"];
}
components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
//NSDate* secondDate = [dateFormatter dateFromString:#"10:01:02:007"];
NSDate * secondDate = [calendar dateFromComponents:components];
getTime = [formatter stringFromDate:firstDate];
NSTimeInterval timeDifference = [firstDate timeIntervalSinceDate:secondDate];
NSString *timeDifferenceString = [NSString stringWithFormat:#"%f", timeDifference];
timeDiff[iX] = [timeDifferenceString intValue];
[timeLabel setText:timeDifferenceString];
iX++;
}
If you have a better solution for my problem pleas help me.

Look at UIControlEventTouchDown, UIControlEventTouchUpInside here: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIControl_Class/Reference/Reference.html
Then I suppose you want to store an NSDate of when you pressed down in some sort of ivar or property (UIControlEventTouchDown) and another one when you release the button (UIControlEventTouchUpInside or UIControlEventTouchUpOutside) and then call -timeIntervalSinceDate: on that date. So it would look roughly like this:
- (IBAction)pressedDown:(id)sender {
self.pressedDownDate = [NSDate date];
}
- (IBAction)touchedUp:(id)sender {
NSDate *now = [NSDate date];
NSLog(#"Time passed: %d", [now timeIntervalSinceDate:self.pressedDownDate]);
}

Related

how to calculate difference between two times in different dates in iOS?

starttime= 23:00 pm
end time= 5:00 am
I need the summing time of the start time to end time
i am trying this code but it is not helpful to me
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:#"HH"];
NSDate *currentTime = [NSDate date];
NSDate *openTime = [formatter dateFromString:start];
NSDate *closeTime = [formatter dateFromString:end ];
NSCalendar *calender = [NSCalendar currentCalendar];
NSDateComponents *currentTimeComponents = [calender components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:currentTime];
NSDateComponents *openTimeComponents = [calender components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:openTime];
NSDateComponents *closeTimeComponents = [calender components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:closeTime];
NSUInteger currentTimeSeconds = currentTimeComponents.hour;
NSUInteger openTimeSeconds = openTimeComponents.hour;
NSUInteger closeTimeSeconds = closeTimeComponents.hour;
if (currentTimeSeconds>openTimeSeconds && currentTimeSeconds<closeTimeSeconds) {
NSLog(#"Time Between Open and Close Time %lu",(unsigned long)currentTimeSeconds);
}
and it shows 0 hors.
please help me some one?
thanks in advance
If possible, I'd look at using libs/frameworks to accomplish that through their APIs. For example, I've done date operations using DateTools

Getting future date using NSDate and NSTimeInterval

I'm creating a simple timer but am failing to get the resulting date that I'm after.
My problem is that I'm getting -34481.274523 from my NSTimeInterval (nslog at the end) whereas I'm wanting the number of seconds since the current date (currentDate). stopDate is always a date before currentDate.
The code snippet below is called from an nstimer every second. Rather than simply increment an int I'm using a date as it's easier to perform other date related calculations on and format for display.
Here is my code:
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:stopDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"mm:ss"];
NSString *timeString=[dateFormatter stringFromDate:timerDate];
NSLog(#"nstimeinterval: %f", timeInterval);
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components: ( NSHourCalendarUnit | NSYearCalendarUnit) fromDate:timerDate];
NSLog(#"year: %li", (long)components.year);
if (components.year == 2000) {
timeString = #"00:00";
return;
}
NSLog(#"timer: %#", timeString);
timerTicksForCounter = timeString;
NSLog(#"hour: %li", (long)components.hour);
//Used to update uilabel
[self updateTimerLabel];
if (components.year < 2000) {
return;
}
if (components.hour == 0) {
..... I'm doing a bunch of stuff here

Trying to set current date and date parameters to past date

I am trying to create a function that pulls data that only has a time stamp of a specific date. It is for a project and the most recent data in the set is from February. I still want to use the standard yesterday, last week, last month parameters.
Is there any way to set the "current date" to a specific date so I can make the time frames based on that instead of the actual current date?
This is what I've tried
NSString *datestr = #"2014-02-16T04:59:50.021Z";
NSDateFormatter *dformat = [[NSDateFormatter alloc]init];
[dformat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssz"];
NSDate *date = [dformat dateFromString:datestr];
NSTimeInterval secondsPerDay = 24 * 60 * 60;
NSDate *yesterday = [[NSDate date] dateByAddingTimeInterval: date -secondsPerDay];
NSDate *lastWeek = [[NSDate alloc]initWithTimeIntervalSinceNow: - secondsPerDay * 7];
NSDate *lastMonth = [[NSDate alloc]initWithTimeIntervalSinceNow:-secondsPerDay * 30];
Trying to subtract secondsPerDay from date yields an arithmetic error. Any workarounds?
You can't subtract an NSTimeInterval (which is a double) from an NSDate (which is an object). You want to send the dateByAddingTimeInterval: message to the date from which the time will be subtracted:
NSString *datestr = #"2014-02-16T04:59:50.021Z";
NSDateFormatter *dformat = [[NSDateFormatter alloc]init];
[dformat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssz"];
NSDate *date = [dformat dateFromString:datestr];
NSTimeInterval secondsPerDay = 24 * 60 * 60;
NSDate *yesterday = [date dateByAddingTimeInterval:-secondsPerDay];
NSDate *lastWeek = [date dateByAddingTimeInterval:-(secondsPerDay * 7)];
NSDate *lastMonth = [date dateByAddingTimeInterval:-(secondsPerDay * 30)];
Note, however, that lastMonth will not be exactly correct, since not every month has 30 days. Instead, you may also want to look at NSDateComponents:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *todayComponents = [NSDateComponents new];
todayComponents.year = 2014;
todayComponents.month = 2;
todayComponents.day = 16;
todayComponents.hour = 4;
todayComponents.minute = 59;
todayComponents.second = 50;
NSDate *today = [calendar dateFromComponents:todayComponents];
NSDateComponents *offset = [NSDateComponents new];
offset.day = -1;
NSDate *yesterday = [calendar dateByAddingComponents:offset toDate:today options:0];
offset = [NSDateComponents new];
offset.day = -7;
NSDate *lastWeek = [calendar dateByAddingComponents:offset toDate:today options:0];
offset = [NSDateComponents new];
offset.month = -1;
NSDate *lastMonth = [calendar dateByAddingComponents:offset toDate:today options:0];

How to calculate NSDate between two NSDate in IOS

I want show whether particular office is opened or closed depends on the weekday
i am getting office timings from my server
NSString *open = #"10:00 AM";
NSString *close = #"6:00 PM";
NSDateFormatter *df11 = [[NSDateFormatter alloc]init];
[df11 setDateFormat:#"dd-MM-yyyy"];
NSString *date11=[df11 stringFromDate:[NSDate date]];
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"dd-MM-yyyy hh:mm a"];
NSDate *date1 = [NSDate date];
NSString *dte1 = [df stringFromDate:date1];
date1 = [df dateFromString:dte1];
NSString *dte2 = [NSString stringWithFormat:#"%# %#",date11, open];
NSDate *date2 = [df dateFromString:dte2];
NSString *dte3 = [NSString stringWithFormat:#"%# %#",date11,close];
NSDate *date3 = [df dateFromString:dte3];
if([date1 compare:date2]==NSOrderedDescending && [date1 compare:date3]==NSOrderedAscending)
open=YES;
else
open=NO;
In this case i didn't get any problem, but for some other office i got like this
NSString *open = #"11:30 AM";
NSString *close = #"12:30 AM";/*means here day is changing but still i am using current date
for this case the above code is not working, bcoz of day changes, i am not getting any idea how to follow, i was struggling from last day for solution
please help me
thank you
get days between two dates
NSDate *date1 = [NSDate dateWithString:#"2013-08-08"];
NSDate *date2 = [NSDate dateWithString:#"2013-09-09"];
NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
int numberOfDays = secondsBetween / 86400;
NSLog(#"There are %d days in between the two dates.", numberOfDays);
You can also get different between two dates GO
Extract the week day and hour from the date and do an explicit test on them:
const NSInteger openHour = 10;
const NSInteger closeHour = 18;
NSDate *dateToTest = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSWeekdayCalendarUnit|NSHourCalendarUnit
fromDate:dateToTest];
BOOL open =
[components weekDay] >= [calendar firstWeekDay] &&
[components weekDay] < [calendar firstWeekDay] + 5 &&
[components hour] >= openHour &&
[components hour] < closeHour;

Showing "past" and "upcoming" sections UITableView with FetchedResultsController

I'm having a bit of difficulty grouping my table view by past and upcoming. This would be determined by a date field in my Core Data model. So all items with a date > today would be upcoming, and all with a date < today would be considered past. Any insight into this would be greatly appreciated.
You can use this function in your entities class:
-(NSString*)dateToStringForSectionTitels{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString *formattedDateString;
[dateFormatter locale];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:[NSDate date]];
NSDate *today = [cal dateFromComponents:components];
components = [cal components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:self.startDate];
NSDate *otherDate = [cal dateFromComponents:components];
if([today isEqualToDate:otherDate]) {
formattedDateString = #"Today";
}else{
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
formattedDateString = [dateFormatter stringFromDate:self.startDate];
}
NSTimeInterval distanceBetweenDates = [self.startDate timeIntervalSinceDate:[NSDate date]];
double secondsInAnMinute = 60;
NSInteger minutsBetweenDates = distanceBetweenDates / secondsInAnMinute;
if (minutsBetweenDates<30) {
formattedDateString = #"Starting Soon";
}else if(minutsBetweenDates<0){
formattedDateString = #"In the past";
}
return formattedDateString;
}
It will return
The date in a string if the event is in the future
"Today" if the event is today.
"Starting soon" if the event is starting in 30 minutes - (You can range it to what ever you wish.
"In the past" if the event is in the past.
Then just pass the "dateToStringForSectionTitels" to your fetch result controller.
Good luck.

Resources