HI All,
i am trying to use local notification
by using this
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay: [dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]]; // Notification will fire in one minute
[dateComps setMinute:0 ]; //[timeComponents minute]];
[dateComps setSecond:60]; //[timeComponents second]];
and i hv string something like current time : 2011-03-26 11:59:31 GMT
according to which i need to set sec,min, hours.
How can i set min, week,month, year etc, to pop up notification in above condition??
Guidenece needed.
Appreciate it
You can make a NSDateComponent from the above date as
NSDateComponents *components = [calendar components:(kCFCalendarUnitHour | kCFCalendarUnitMinute | kCFCalendarUnitSecond) fromDate:date];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
If you want to set sec,min,hours etc. with this string "current time : 2011-03-26 11:59:31 GMT"
then first make a NSDate with this string using NSDateFormatter and get your values from this date.
NSString *dateStr = #"2011-03-26 11:59:31";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
dateStr = [dateFormat stringFromDate:date];
Related
I'm writing an application based on UILocalNotifications. I having the user input a time on a date picker and that time will represent when the last UILocalNotification will fire. After the user hits a UIButton UILocalNotifications will fire based on a time interval until the time they selected occurs, then the app will stop.
I schedule the maximum number of UILocalNotifications before the user inputs a time. Is there a way to cancel the UILocalNotifications that are scheduled after the user's input time, based on time? Or is there a way to cancel all scheduled notifications after the inputted notification fires? The code is setting the last timer and the first timer based of the input time. Thanks for the help.
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *pickerDate = [self.datePicker date];
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:0];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
/**************Setting the final alarm*********************/
UILocalNotification *finalAlarm = [[UILocalNotification alloc] init];
if (finalAlarm) {
finalAlarm.fireDate = itemDate;
finalAlarm.timeZone = [NSTimeZone defaultTimeZone];
finalAlarm.repeatInterval = 0;
finalAlarm.soundName = UILocalNotificationDefaultSoundName;
finalAlarm.alertBody = #"Test message...";
[[UIApplication sharedApplication] scheduleLocalNotification:finalAlarm];
}
//Formatting original date in the date picker for readability
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH"];
NSString *formattedDateString = [dateFormatter stringFromDate:datePicker.date];
//isolate current date/time
NSDate *currentDate = [NSDate date];
//formatting the current date for readability
[dateFormatter setDateFormat:#"HH"];
NSString *currentForamttedDate = [dateFormatter stringFromDate:currentDate];
//current hour
int currentHour = [currentForamttedDate intValue];
/**************Setting the first alarm*********************/
//current hour plus 2
int firstAlarmHour = currentHour + 2;
//creating the first alarm
NSDateComponents *firstAlarm = [[NSDateComponents alloc]init];
[firstAlarm setDay:[dateComponents day]];
[firstAlarm setMonth:[dateComponents month]];
[firstAlarm setYear:[dateComponents year]];
[firstAlarm setHour:firstAlarmHour];
[firstAlarm setMinute:[dateComponents minute]];
[firstAlarm setSecond:0];
//creating a date from the components
NSDate *firstAlarmDate = [calendar dateFromComponents:firstAlarm];
//setting the first alarm
[self scheduleNotificationForDate: firstAlarmDate];
You can use the following code to get all the notifications and then cancel them based on your specifications.
if ([[UIApplication sharedApplication].scheduledLocalNotifications count] > 1){
for (int i = 0; i < [[UIApplication sharedApplication].scheduledLocalNotifications count]; i++){
UILocalNotification * notification = [[UIApplication sharedApplication].scheduledLocalNotifications objectAtIndex:i];
if (/* if after your fire date */){
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
}
I have two UIDatePickers in my app, one mode is for selecting date and other to time mode. I have fired a notification when the exact date and time is reached. I'm able to fire the reminder at correct time, but the problem is that the date is not checked. Is there any way to check the date and time at the same time??
Thanx in advance...
Edit
NSDate *date1 = [datePicker date];
NSDate *date2 = [timePicker date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned unitFlagsDate = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *dateComponents = [gregorian components:unitFlagsDate fromDate:date1];
unsigned unitFlagsTime = NSHourCalendarUnit | NSMinuteCalendarUnit ;
NSDateComponents *timeComponents = [gregorian components:unitFlagsTime fromDate:date2];
[dateComponents setHour:[timeComponents hour]];
[dateComponents setMinute:[timeComponents minute]];
NSDate *combDate = [gregorian dateFromComponents:dateComponents];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) return;
NSDate *fireTime = combDate;
localNotif.fireDate = fireTime;
localNotif.alertBody = #"Alert!";
localNotif.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:self.datePicker.date];
NSDateComponents *timeComponents = [calendar components:NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:self.timePicker.date];
NSDateComponents *newComponents = [[NSDateComponents alloc]init];
newComponents.timeZone = [NSTimeZone systemTimeZone];
[newComponents setDay:[dateComponents day]];
[newComponents setMonth:[dateComponents month]];
[newComponents setYear:[dateComponents year]];
[newComponents setHour:[timeComponents hour]];
[newComponents setMinute:[timeComponents minute]];
NSDate *combDate = [calendar dateFromComponents:newComponents];
NSLog(#" \ndate : %# \ntime : %#\ncomDate : %#",self.datePicker.date,self.timePicker.date,combDate);
Swift 5 version of the accepted answer if it might help.
/// Returns `Date` from date and time.
func combine(date: Date, time: Date) -> Date? {
let calendar = Calendar.current
let dateComponents = calendar.dateComponents([.day, .month, .year], from: date)
let timeComponents = calendar.dateComponents([.hour, .minute, .second], from: time)
var newComponents = DateComponents()
newComponents.timeZone = .current
newComponents.day = dateComponents.day
newComponents.month = dateComponents.month
newComponents.year = dateComponents.year
newComponents.hour = timeComponents.hour
newComponents.minute = timeComponents.minute
newComponents.second = timeComponents.second
return calendar.date(from: newComponents)
}
Try this:
Convert date into NSString and Time into NSString.
Apppend Time into date string.
Now convert that final string into NSDate
Example: (Add validations from ur side)
NSDate *date1 = [datePicker date];
NSDate *date2 = [timePicker date];
NSString *date = [NSString stringWithFormat:#"%#",date1];
NSString *time = [NSString stringWithFormat:#"%#",date2];
NSString *dateAndTime = [NSString stringWithFormat:#"%# %#",date,time];
NSDate *dateTime = [self dateFromString:dateAndTime];
- (NSDate*) dateFromString:(NSString*)aStr
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
//[dateFormatter setDateFormat:#"YYYY-MM-dd HH:mm:ss a"];
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSLog(#"%#", aStr);
NSDate *aDate = [dateFormatter dateFromString:aStr];
[dateFormatter release];
return aDate;
}
i want to get timezone but cant.
i use NSLog is show to me is show.time = 2011-06-29 04:20:00 +0000
here is my code.
-(IBAction)Save:(id)sender{
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
//[calendar setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
// Get the current date
NSDate *pickerDate = [self.datePicker date];
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
//[dateComps setSecond:[timeComponents second]];
timedata = [calendar dateFromComponents:dateComps];
[dateComps release];
DataClass *obj=[DataClass getInstance];
obj.showdate = timedata;
//NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//[defaults setObject:timedata forKey:#"selectdate"];
//NSLog(#"save time = %#",obj.showdate);
[self dismissModalViewControllerAnimated:YES];
}
You're setting the time zone for your NSDateComponents object to GMT:
[dateComps setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
Thus your date will be interpreted as being in GMT. If you want the time zone that the device is in, use systemTimeZone:
[dateComps setTimeZone:[NSTimeZone systemTimeZone]];
In general, as noted in the docs for timeZoneWithAbbreviation:, the abbreviations can be ambiguous. If you want to get a particular time zone in a way that will always work no matter the location of the device, you can use timeZoneForSecondsFromGMT:.
You can also set a time zone for your app, if you want, rather than always referring to a specific time zone when creating date-related objects:
[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:60*8]];
NSTimeZone * appTZ = [NSTimeZone defaultTimeZone];
I want to make an NSDate object which has the date set as the current date, but the time set as the time in the datepicker (because the datepicker's mode is the time one). How can i do this?
and if you really want to use a custom date use something like this.
- (IBAction)dpChange:(UIDatePicker *)sender {
// replace this with your custom date.
NSDate *myCustomDate = [NSDate dateWithTimeIntervalSinceNow:7*24*60*60];
NSInteger dateFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:dateFlags fromDate:myCustomDate];
NSDate *myCustomTime = [sender date];
NSInteger timeFlags = NSHourCalendarUnit | NSMinuteCalendarUnit;
NSDateComponents *timeComponents = [[NSCalendar currentCalendar] components:timeFlags fromDate:myCustomTime];
NSDateComponents *newComponents = [[[NSDateComponents alloc] init] autorelease];
[newComponents setYear:[dateComponents year]];
[newComponents setMonth:[dateComponents month]];
[newComponents setDay:[dateComponents day]];
[newComponents setHour:[timeComponents hour]];
[newComponents setMinute:[timeComponents minute]];
[newComponents setSecond:0];
NSDate *newDate = [[NSCalendar currentCalendar] dateFromComponents:newComponents];
NSLog(#" Date: %#", myCustomDate);
NSLog(#" Time: %#", myCustomTime);
NSLog(#"Combined: %#", newDate);
}
I'm trying to get a date that represents noon GMT for a recurring local notification.
I need this local notification to fire at the exact moment to anyone using the app.
So far, I'm trying this:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setHour:12];
[dateComps setMinute:0];
[dateComps setMonth:1];
[dateComps setDay:1];
[dateComps setYear:2010];
NSDate *localDate = [calendar dateFromComponents:dateComps];
NSTimeInterval timeZoneOffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
NSTimeInterval gmtTimeInterval = [localDate timeIntervalSinceReferenceDate] - timeZoneOffset;
NSDate *gmtDate = [NSDate dateWithTimeIntervalSinceReferenceDate:gmtTimeInterval];
localNotification.fireDate = gmtDate;
This isn't working, however. Any suggestions?
Edit::
Will this work if I don't specify a time zone on the notfication:
localNotification.fireDate = [NSDate dateWithTimeIntervalSince1970:1284483600];
Does setting the calendar's timezone to GMT work?
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setHour:12];
[dateComps setMinute:0];
[dateComps setMonth:1];
[dateComps setDay:1];
[dateComps setYear:2010];
[calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *gmtDate = [calendar dateFromComponents:dateComps];