Memory leaks with NSDateFormatter - ios

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

Related

objective-c dateFromString returns nil on specific date

The date is always nil, but if I change the dateString to any other date like 03/01/1975 it works normally. Any idea?
NSString *dateString = #"05/01/1975";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"us"];
[dateFormatter setLocale:gbLocale];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
Try this
NSString *dateString = #"05/01/1975";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[formatter setDateFormat:#"MM.dd.yyyy"];
NSDate *anotherDate = [formatter dateFromString:dateString];
NSLog(#"%#",anotherDate);
It will give output like this
1975-05-01 00:00:00 +0000
try this code
- (NSDate *)getPickerDateForString:(NSString *)dateString
{
NSDate * dt;
if (!dateFormatter)
{
dateFormatter = [[NSDateFormatter alloc] init];
}
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
dt = [dateFormatter dateFromString:dateString];
return dt;
}

While converting NSString to NSDate?

I try to convert NSString to NSDate with this code:
NSString *dateString=[[NSString alloc] init];
dateString = [[messageArray objectAtIndex:i] tim_dte];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm,yyyy/MM/dd"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
msg.date = dateFromString;
But msg.Date is nil even if dateString shows the correct date string. What am I missing?
change this format
[dateFormatter setDateFormat:#"HH:mm,yyyy/MM/dd"];
into and try
[dateFormatter setDateFormat:#"yyyy/MM/dd HH:mm"];
Update
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy/MM/dd HH:mm"];
// use simple
msg.date = [dateFormatter dateFromString:#"2016/05/06 12:36"];
NSLog(#"final date == %#",dateFromString);
//NSDate *dateFromString = [dateFormatter dateFromString:#"2016/05/06 12:36"];
// NSLog(#"final date == %#",dateFromString);
Try this code to convert NSString to NSDate
NSString *dateString = #"2016-05-06";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:#"2013-02-01T06:25:47Z"];
NSTimeZone *pdt = [NSTimeZone timeZoneWithAbbreviation:#"PDT"];
[dateFormatter setTimeZone:pdt];
[dateFormatter setDateFormat:#"HH:mm:ss zzz"];
[dateFormatter setDateFormat:#"K:mm a, z"];
NSString * updated String = [dateFormatter stringFromDate:date];
Use this Code,
NSString *dateString=[[NSString alloc] init];
dateString =[[messageArray objectAtIndex:i] tim_dte];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy/MM/dd HH:mm"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
msg.date=dateFromString;
hope its helpful

Change date format using NSDateFormatter

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

How to reuse a NSString method?

I am writing a method that takes string and returns string. I am trying to use it in another method.
- (NSString*) formatDate:(NSString*) showStartDateTime
{
NSTimeZone *tz = [NSTimeZone timeZoneWithName:#"UTC"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:tz];
[dateFormatter setDateFormat:#"EEE MM/dd/yyyy"];
NSDate*dattt=[dateFormatter dateFromString:showStartDateTime ];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString*tempo2= [dateFormatter stringFromDate:dattt];
return tempo2;
}
This is how I am using it. [self formatDate:#"Fri 08/08/2014"] is it a correct way to use it?
NSString *hellostring=[self formatDate:#"Fri 08/08/2014"];
NSLog(#"%#",hellostring);
I know it does not work. Any suggestion is appreciated.
I wrote a command line app and wasnt able to get it running, as already dateFromString: resulted in nil, until I added dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
- (NSString*) formatDate:(NSString*) showStartDateTime
{
NSTimeZone *tz = [NSTimeZone timeZoneWithName:#"UTC"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:tz];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setDateFormat:#"EEE MM/dd/yyyy"];
NSDate*dattt=[dateFormatter dateFromString:showStartDateTime ];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString*tempo2= [dateFormatter stringFromDate:dattt];
return tempo2;
}
Now it returns 2014-08-08
see Technical Q&A QA1480: NSDateFormatter and Internet Dates

format a NSDate to DDMMYYYY?

I have to send a DDMMYYY date from a Date to communicate with an API.
NSDateComponents *dateComponents;
NSCalendar *gregorian;
NSDate *date;
gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
dateComponents = [[NSDateComponents alloc] init];
self.date = [gregorian dateByAddingComponents:dateComponents toDate:[NSDate date] options:0];
Do I have to use the [dateComponents day] and so on? is there some method that can help me to performing that task like in PHP?
Try this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"ddMMyyyy"];
NSString *textDate = [NSString stringWithFormat:#"%#",[dateFormatter stringFromDate:[NSDate date]]];
NSLog(#"Date %#",textDate);
[dateFormatter release];
Just:
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"ddMMyyyy"];
NSString *textDate = [NSString stringWithFormat:#"%#",[outputFormatter stringFromDate:[NSDate date]]];
[outputFormatter release];
Try this code:
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
[dateFormatter setDateFormat:#"ddMMyyyy"];
NSDate *current = [NSDate date];
NSString *currentstring = [dateFormatter stringFromDate:current]
check the iphonetips,stackoverflow,apple reference
NSDate *date=[NSDate date];
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:#"ddMMyyyy"];
NSString *dateOfGame =[formatter stringFromDate:date];
[formatter release];
Try this:
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"ddMMyyyy"];
NSLog(#"%#", [dateFormatter stringFromDate:[NSDate date]]);
Agree With Ruben Esposito
Just change your code like this.
NSDateFormatter *dtFormatter = [[NSDateFormatter alloc]init];
[dtFormatter setDateFormat:#"ddMMyyyy"];
NSString *strDate = [NSString stringWithFormat:#"%#",[dtFormatter stringFromDate:[NSDate date]]];
[dtFormatter release];
NSLog(#"%#",strDate);

Resources