How to keep same date even if time zone is changed - nscalendar

How to keep same date even if time zone is changed. for example: I am in India, IST timezone. I have setup reminder at 10am IST. I move to USA (which has 5 timezones), I m in PDT. It should remind me at 10am PDT and not 8:30pm PDT.Please help!

You can Use this Code . might be it helpful for you.
NSDate sourceDate = Yourdate;
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:#"Australia/Melbourne"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone]; // your device time . you can also change any time zone like above.
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];

Related

Can't fire UILocalNotification on custom time ios objective c

I want to fire NSLocalNotification on my custom time. Ex. 1hour before
appointment time but unable to set it on my custom time.
NSDate *dt1 = [NSDate date]; //2018-07-18 10:41:22 +0000
NSString *datestr = [NSString stringWithFormat:#"%#", [NSDate date]];//2018-07-18 10:42:09 +0000
NSRange range = NSMakeRange(11,5);
NSString *cutstring = 05:00 PM;
By this I add substring to current date and change date in my custom
date.
NSString *newstr = [cutstring substringWithRange:NSMakeRange(0, 5)];
NSString *changed = [datestr stringByReplacingCharactersInRange:range withString:newstr]; //2018-07-18 05:00:09 +0000
By this code I got my custom time '2018-07-18 05:00:09 +0000' on which I want to fire UILocalNotification, But issue is that when I set my custom time with NSLocalNotification it set's on such a random time like 18 July 2018 at 4:19:29 PM
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *fireddate = [dateFormatter dateFromString:changed];
localNotification.fireDate = fireddate;
NSLog(#"%#",fireddate);
localNotification.alertBody = [NSString stringWithFormat:#"You have appointment in 1 hour"];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
localNotification.category = #"ACTIONABLE";
//Received Local Notification:<UIConcreteLocalNotification:
0x6000003887b0>{fire date = Wednesday, 18 July 2018 at 4:19:29 PM
India Standard Time, time zone = (null), repeat interval = 0, repeat
count = UILocalNotificationInfiniteRepeatCount, next fire date =
(null), user info = { }}
I would just create a fire date with NSDateComponents where you can set year, month, hour, minute etc.
Something like this:
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.calendar = [NSCalendar currentCalendar];
[dateComponents setYear:year];
[dateComponents setMonth:month];
[dateComponents setDay:day];
[dateComponents setHour:hour];
NSDate *fireDate = [dateComponents date];
You can read more about it in this great article
NSDateā€‹Components - NSHipster

Get monday with locale in Swift [duplicate]

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;
}

Swift - NSDate after breaking into components always give the next day's date [duplicate]

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;
}

The setHour Function changing the timezone for a function

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

Creating NSDate from only date components [duplicate]

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;
}

Resources