So I am trying to get the current date and time and then trying to override the Hours and the minutes so as to set up a fire time. But whenever I use the [comp sethour] functionality it reverts it to GMT timings for some reason.
In the header file I have :
static int re_hour = 19;
static int re_minute = 52;
and in the .m File I have this function
- (void) ScheduleTimer {
NSCalendar* myCalendar = [NSCalendar currentCalendar];
//Get The GMT Time
NSDate* now = [NSDate date];
//Get The Current Timezone and then convert the Time
NSTimeZone* currentTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSTimeZone* nowTimeZone = [NSTimeZone systemTimeZone];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:now];
NSInteger nowGMTOffset = [nowTimeZone secondsFromGMTForDate:now];
NSTimeInterval interval = nowGMTOffset - currentGMTOffset;
NSDate* nowDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:now];
//Setting the restart Time
NSDateComponents *comp = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:nowDate];
[comp setHour:re_hour];
[comp setMinute:re_minute];
NSDate *restartTime = [myCalendar dateFromComponents:comp];
NSLog(#"The time of restart :%#",restartTime);
}
and it always seems to return:
2014-08-26 19:52:34.032 SASMobile[2605:60b] The time of restart :2014-08-27 02:52:34 +0000
Related
I am using
NSDate *date = [NSDate date];
for getting the date, but the date I get is off by 2 hours.
NSDate objects don't have time zones. They represent an absolute moment in time. However, when you ask one for its description (by printing it with NSLog(), e.g.), it has to pick a time zone. The most reasonable "default" choice is GMT. If you're not in GMT yourself, the date will seem to be incorrect, by the amount of your own offset.
You should always use an NSDateFormatter to create a string for display. The formatter's timezone should be set to yours, which is the default.
You can get your date corrected like this:
NSDate * dateGMT = [NSDate date];
NSTimeInterval secondsFromGMT = [[NSTimeZone localTimeZone] secondsFromGMT];
NSDate * correctDate = [dateGMT dateByAddingTimeInterval:secondsFromGMT];
-(NSDate *)getDateInCurrentSystemTimeZone
{
NSDate* sourceDate = [NSDate date];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
return destinationDate;
}
Need help to show the time intervals for every 30 mins, suppose the current time is 11:45 am then
Time intervals should be : 12:00 pm,12:30 pm,01:00 pm,01:30 pm,02:00 pm,02:30 pm......10:30 pm.
NSString *time = #"10.30 pm";
NSDate *date1;
NSDate *date2;
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"hh.mm a"];
date1 = [formatter dateFromString:time];
date2 = [formatter dateFromString:[formatter stringFromDate:[NSDate date]]];
}
NSTimeInterval interval = [date1 timeIntervalSinceDate: date2];//[date1 timeIntervalSince1970] - [date2 timeIntervalSince1970];
int hour = interval / 3600;
int minute = (int)interval % 3600 / 60;
NSLog(#"%# %dh %dm", interval<0?#"-":#"+", ABS(hour), ABS(minute));
This code returns me difference of current time and the given time how can I proceed further.
You can do something like,
NSString *startTime = #"02:00 AM";
NSString *endTime = #"11:00 AM";
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"hh:mm a"];
NSDate* fromTime = [timeFormat dateFromString:startTime];
NSDate* toTime = [timeFormat dateFromString:endTime];
NSDate *dateByAddingThirtyMinute ;
NSTimeInterval timeinterval = [toTime timeIntervalSinceDate:fromTime];
NSLog(#"time Int %f",timeinterval/3600);
float numberOfIntervals = timeinterval/3600;
NSLog(#"Start time %f",numberOfIntervals);
for(int iCount = 0;iCount < numberOfIntervals*2 ;iCount ++)
{
dateByAddingThirtyMinute = [fromTime dateByAddingTimeInterval:1800];
fromTime = dateByAddingThirtyMinute;
NSString *formattedDateString;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a"];
formattedDateString = [dateFormatter stringFromDate:dateByAddingThirtyMinute];
NSLog(#"Time after 30 min %#",formattedDateString);
}
In for loop I have taken numberOfIntervals*2 because time interval is 30 min, so 60/30 = 2 and your datebyAddingThirtyMinute is 1800 because 30 min = 1800 seconds. If you want time after every 10 minutes then it should 60/10 = 6, so it should numberOfIntervals*6. And your datebyAddingThirtyMinute should be [fromTime dateByAddingTimeInterval:600];
Hope this will help :)
The most reliable way (also to consider daylight saving time changes) is to use the date math capabilities of NSCalendar.
Simply adding seconds with dateByAddingTimeInterval is not recommended at all.
NSDate *now = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
// get minute and hour from now
NSDateComponents *nowComponents = [cal components:NSCalendarUnitHour | NSCalendarUnitMinute fromDate: now];
NSDate *currentDate;
// if current minutes is not exactly 0 or 30 get back to the past half hour
if (nowComponents.minute % 30 != 0) {
NSInteger pastHalfHourIndex = nowComponents.minute / 30;
nowComponents.minute = pastHalfHourIndex * 30;
currentDate = [cal nextDateAfterDate:now matchingHour: nowComponents.hour minute: nowComponents.minute second: 0 options: NSCalendarMatchNextTime | NSCalendarSearchBackwards];
} else {
currentDate = now;
}
NSMutableArray<NSDate *>* dates = [NSMutableArray array];
// loop and add 30 minutes until the end time (10:30 pm) is reached
while (nowComponents.minute != 30 || nowComponents.hour != 22) {
currentDate = [cal dateByAddingUnit:NSCalendarUnitMinute value: 30 toDate: currentDate options: NSCalendarMatchNextTime];
[dates addObject:currentDate];
nowComponents = [cal components:NSCalendarUnitHour | NSCalendarUnitMinute fromDate: currentDate];
}
NSLog(#"%#", dates);
Try this code it works for you
NSDateFormatter *datefrmt = [[NSDateFormatter alloc] init];
[datefrmt setDateFormat:#"hh:mm a"];
NSDate* startingTime = [datefrmt dateFromString:startTime];
NSDate* endingTime = [datefrmt dateFromString:endTime];
NSLog(#"Starting time is %#", startingTime);
NSLog(#"Stop time is %#", endingTime);
NSDate * addingTimeInterval;
addingTimeInterval = [startingTime dateByAddingTimeInterval:1800];
NSLog(#"Required output is %#", addingTimeInterval);
I would first check if the given start time is AM or PM, after doing that, I would find the nearest (rounded up) 30m interval of time and simply add 30m to it and check whether or not I was at the given stop stop time, and if not increment a counter.
You could also add each 30m interval to a list if you wanted to keep track of and return that list to be printed out.
If you get the Start date is current date and time, then the end date is automatically set 30 mins use dateByAddingTimeInterval:, see my example,
NSDate *startDate = [NSDate date];
then set ending date is below code,
currentdate = startDate; //today
endingDate = [startDate dateByAddingTimeInterval:(60*60)/2]; // 60*60 is 1 hour.
this above method automatically calculate current time to end time add 30 minutes.
hope its helpful
I am using
NSDate *date = [NSDate date];
for getting the date, but the date I get is off by 2 hours.
NSDate objects don't have time zones. They represent an absolute moment in time. However, when you ask one for its description (by printing it with NSLog(), e.g.), it has to pick a time zone. The most reasonable "default" choice is GMT. If you're not in GMT yourself, the date will seem to be incorrect, by the amount of your own offset.
You should always use an NSDateFormatter to create a string for display. The formatter's timezone should be set to yours, which is the default.
You can get your date corrected like this:
NSDate * dateGMT = [NSDate date];
NSTimeInterval secondsFromGMT = [[NSTimeZone localTimeZone] secondsFromGMT];
NSDate * correctDate = [dateGMT dateByAddingTimeInterval:secondsFromGMT];
-(NSDate *)getDateInCurrentSystemTimeZone
{
NSDate* sourceDate = [NSDate date];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
return destinationDate;
}
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];
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]);
}