My question is :
i have a TableVC wherein i have CustomCells.
These customCells have Various Label and one of them is Time label.
The text of the timeLabel would be fetched by Parsing an xml.
But as of now assuming that i used static content for e.g.: 2:45pm.
Is it possible for me to give this text as fireDate of UILocalNotification and off-course it should fire on the said time.
It should be done on click of cell that is in "didSelectRowAtIndexPath".
Or this cannot be done ??
And yes if this is a repetition question,you did not like it, or if if there is some spelling mistake or anything related Please do not mark it with down votes.
I have seen in the past where people play such cheap gimmicks and use their so called reputations in an unhealthy and unprofessional way.
Your related answer's are warmly welcomed.
Thank You !!!
EDIT
i tried this thing
NSString *str = cell.timeLabel.text;
NSLog(#"str = %#",str);
formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[formatter setTimeZone:gmt];
date = [formatter dateFromString:str];
the above in cellForRowAtIndexPath
and
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [NSDate dateWithTimeInterval:5 sinceDate:date];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"You are notified";
localNotif.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotif];
}
NSDate *currentDate=[NSDate date];
NSString *dateStr=#"7:10 PM";
UILocalNotification *localNotification =[[UILocalNotification alloc]init];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a"];
[dateFormatter setTimeZone:gmt];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString *preFix=[dateFormatter stringFromDate:currentDate];
NSString *datetoFire=[NSString stringWithFormat:#"%# %#",preFix,dateStr];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm a"];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm a"];
NSDate *fireDate = [dateFormatter dateFromString:datetoFire];
fireDate=[fireDate dateByAddingTimeInterval:-(60*60*(5.5))];
NSLog(#"date is %#",fireDate);
// Cancel the previously scheduled notification, if any.
if (localNotification == nil) {
return;
}
localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = #"Good Morning Buddies";
localNotification.alertAction = #"View";
localNotification.fireDate = fireDate;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber++;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Related
I want to use date format like this 2015-08-14 but unfortunately I am getting this format 16/11/15
Below is the code I have used to get desired output.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
[dateFormatter setDateStyle:kCFDateFormatterShortStyle];
NSString *formattedDateString = [dateFormatter stringFromDate:selectedDate];
Can anyone please suggest how to fix this issue?
Just remove the line [dateFormatter setDateStyle:NSDateFormatterShortStyle];
-(NSString*)stringFromDateWithFormat:(NSString*)formatString{
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
dateFormatter.dateFormat = formatString;
return [dateFormatter stringFromDate:self];
}
NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"YYYY-MM-dd"];
NSString *dateString = [dateFormatter stringFromDate:currDate];
NSLog(#"date is %#",dateString);
NSDate *selectedDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSString *formattedDateString = [dateFormatter stringFromDate:selectedDate];
NSLog(#"formattedDateString = %#", formattedDateString); // Prints 2015-10-15
I scheduled the Local notification for current day +1 and time say 10AM.When I change the device date to current day+1 and time 10PM then notification didn't show up.
Code is given below:
UILocalNotification * localNot = [[UILocalNotification alloc] init];
localNot.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
#"ReminderId", #"REM01", nil];
localNot.alertBody = #"Alarm";
NSString *str =#"10/15/2014 9:15 AM";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.timeZone = [NSTimeZone defaultTimeZone];
[formatter setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSDate *date = [formatter dateFromString:str];
localNot.timeZone = [NSTimeZone defaultTimeZone];
localNot.fireDate = date;
localNot.repeatInterval = 0;
[[UIApplication sharedApplication] scheduleLocalNotification:localNot];
Need input.Thanks in advance.
I am using the following code to get the NSString from date
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = kCFDateFormatterLongStyle;
df.timeStyle = kCFDateFormatterNoStyle;
df.doesRelativeDateFormatting = YES;
[df setLocale:[NSLocale currentLocale]];
[df setTimeZone:[NSTimeZone systemTimeZone]];
NSString *dateString=[df stringFromDate:myDate];
But it displaying like 'Today','Yesterday' even after changing Locale.I want to display the localized language of that also ?
I used your code and created this:
NSDate *yesterday;
NSTimeInterval interval;
NSCalendar *cal = [NSCalendar currentCalendar];
[cal rangeOfUnit:NSDayCalendarUnit
startDate:&yesterday
interval:&interval
forDate:[NSDate date]];
yesterday = [yesterday dateByAddingTimeInterval:-interval];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = kCFDateFormatterLongStyle;
df.timeStyle = kCFDateFormatterNoStyle;
df.doesRelativeDateFormatting = YES;
[#[[NSLocale localeWithLocaleIdentifier:#"de_DE"], [NSLocale localeWithLocaleIdentifier:#"es_ES"],[NSLocale localeWithLocaleIdentifier:#"en_US"]] enumerateObjectsUsingBlock:^(NSLocale *locale, NSUInteger idx, BOOL *stop) {
[df setLocale:locale];
[df setTimeZone:[NSTimeZone systemTimeZone]];
NSString *dateString=[df stringFromDate:yesterday];
NSLog(#"%#", dateString);
}];
It outputs correctly
Gestern
ayer
Yesterday
Your problem must lay else-where.
Tested within a command-line program
Make sure that after you changed the language on device/simulator you kill the app completely before you open it again.
Hi I write this Function for LocalTime it Takes input of date as String and covert to Local Time String. May it help you
+(NSString*)LocalTimeWitheDateString:(NSString*)dateString{
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
[formatter setTimeZone:timeZone];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date =[formatter dateFromString:dateString];
NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
[formatter setTimeZone:localTimeZone];
NSString *LnewTimeZoneDateString = [formatter stringFromDate:date];
return LnewTimeZoneDateString;
}
I have an NSString object that contains the string 2013-02-28. By default, the UIDatepicker shows the current date, but I want to show whatever date I had in the NSString object.
I am converting the NSString object into NSDate and I don't know how to set that date on UIDatePicker in UIDatePickerModeDate. How could I do this? I have tried the setDate: method, but it's not working.
This should work for you. You likely need to turn your NSString into a NSDate before sending it to the NSDatePicker. Keep in mind that you may need to set the Timezone of the NSDateFormater to avoid edge cases.
//Given self.datepicker is your UIDatePicker
NSString *dateString = #"2013-03-28";
NSTimeZone *timezone = [NSTimeZone systemTimeZone];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormatterGMTAware dateFromString:dateString];
self.datePicker.date = date;
-(IBAction)btnDateClicked:(id)sender{
theDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, self.view.frame.size.width, 0.0)];
theDatePicker.datePickerMode = UIDatePickerModeDate;
theDatePicker.minimumDate=[NSDate date];
// ************To keep the selected date**********
NSString *dateString = [NSString stringWithFormat:#"%#",btnDate.titleLabel.text ];
NSTimeZone *timezone = [NSTimeZone systemTimeZone];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:timezone];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:dateString];
theDatePicker.date = date;
}
When I profile my iOS application, I found too much Memory leaks:
There is my code with NSDateFormatter and the code is in one loop:
for (NSDictionary * dataDict in deserializedData) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat : #"yyyy-MM-dd HH:mm:ss"];
NSString *currentDateString = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
}
Who can tell me what's wrong with my code.
Just try with auto release like this,
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat : #"yyyy-MM-dd HH:mm:ss"];
for (NSDictionary * dataDict in deserializedData) {
NSString *currentDateString = [dateFormatter stringFromDate:[NSDate date]];
}
There is nothing wrong in this code. But I think it is not called on main thread.
Just create an autorelease pool on the beginning of the function in which you have written this code. At the end of the function release the pool.
-(void) yourFun
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//other stuff...
for (NSDictionary * dataDict in deserializedData) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat : #"yyyy-MM-dd HH:mm:ss"];
NSString *currentDateString = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
}
[pool release];
}