problem with setDateFormat for my iPad app - objective-c

I use the following to get the date.
NSDate *today =[NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//manage day of week and date.
[formatter setDateFormat:#"EEE"];
dayOfWeek = [formatter stringFromDate:today];
[formatter setDateFormat:#"MMM"];
theDate = [formatter stringFromDate:today];
// manage the time and am/pm
[formatter setDateFormat:#"h:mm"];
theTime = [formatter stringFromDate:today];
[formatter setDateFormat:#"a"];
amPM = [[formatter stringFromDate:today] retain];
[formatter release];
My app crashes if I use [formatter setDateFormat:#"d MMM"]. Do I need to convert the integer to string ? Is so please correct my code on how to get the current date

This should do the trick:
[formatter setDateFormat:#"d MMM"];
NSString *theDate = [formatter stringFromDate:today];
setDateFormat doesn't return anything, stringFromDate returns what you are looking for.
And don't retain like you did in this line:
amPM = [[formatter stringFromDate:today] retain];
The formatter has already a retain count of 1 by using alloc.

Related

Date formatting using NSDateFormatter [duplicate]

This question already has answers here:
Date format for 2016-10-20T13:01:47.317
(4 answers)
Closed 5 years ago.
I'm trying to get date asNSString. I'm receiving date in the format 2017-05-05T04:42:44.954604Z
Below is the code in which I'm trying to apply NSDateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString *newdate = [utcDate stringByReplacingOccurrencesOfString:#"T" withString:#" "];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.Z"];
msgDate = [dateFormatter dateFromString:newdate];
But msgDate is showing nil
use 'SS' specifier (with number of S's equal to number of digits you want to get - 6 in your case)
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"];
instead of
NSString *newdate = [utcDate stringByReplacingOccurrencesOfString:#"T" withString:#" "];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.Z"];
UPDATE
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"];
NSDate *msgDate = [dateFormatter dateFromString:#"2017-05-05T04:42:44.954604Z"];
OUTPUT
#pragma mark- Change dateFormat method
+ (NSString*) changeDateFormat:(NSString*)date
{
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate* fromDate = [formatter dateFromString:date];
[formatter setDateFormat:#"YYYY.MM.dd"];
NSString* formatedDate = [formatter stringFromDate:fromDate];
if (formatedDate == nil)
{
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
fromDate = [formatter dateFromString:date];
[formatter setDateFormat:#"YYYY.MM.dd"];
formatedDate = [formatter stringFromDate:fromDate];
}
if (formatedDate == nil)
{
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
fromDate = [formatter dateFromString:date];
[formatter setDateFormat:#"YYYY.MM.dd"];
formatedDate = [formatter stringFromDate:fromDate];
}
return formatedDate;
}
Try this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ";// chang
NSDate *date = [dateFormatter dateFromString:#"2017-04-13T05:57:44.886Z"];
NSString * msgDate = [dateFormatter stringFromDate:date];
// if get nil **date** try to change this time format

How to format datetime from string

I have one string like this "2012 12 12 12:12", how to format as a Date ?
I try this but not work
-(NSDate *)stringToDate:(NSString *)date
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
[formatter setTimeZone:timeZone];
[formatter setDateFormat : #"yyyy MM dd HH:mm"];
NSString *stringTime = date;
NSDate *dateTime = [formatter dateFromString:stringTime];
[formatter release];
return dateTime;
}
Update: when I change the position of formatter, it also can not work by:
NSLog(#"%#", stringToDate(#"2012 12 12 12:12"));
It outputs "null", But it works with string format like "2012-12-12 12:12"
Change this,
[formatter setDateFormat : #"yyyy MM dd HH:mm"];
[formatter release];
NSString *stringTime = date;
NSDate *dateTime = [formatter dateFromString:stringTime];
to
[formatter setDateFormat : #"yyyy MM dd HH:mm"];
NSString *stringTime = date;
NSDate *dateTime = [formatter dateFromString:stringTime];
[formatter release];
Don't release formatter until you are done with the use of it in converting from string to date. In your code, you are releasing it before the conversion itself.
Update:
You need to use it as,
NSLog(#"%#", [self stringToDate:#"2012 12 12 12:12"]);
You can not use stringToDate(#"2012 12 12 12:12") in objective-c. Equivalent here is [self stringToDate:#"2012 12 12 12:12"].
You can use this:
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
instead of these two lines:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
...
[formatter release];
If you use ARC, then no trouble to you for memory management.
EDIT: But kindly read some memory management rules and guidelines, that is a must for everyone working in cococa.
You are releasing NSDateFormatter before it is used
Use this code :-
-(NSDate *)stringToDate:(NSString *)date
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
[formatter setTimeZone:timeZone];
[formatter setDateFormat : #"yyyy MM dd HH:mm"];
NSString *stringTime = date;
NSDate *dateTime = [formatter dateFromString:stringTime];
[formatter release];
return dateTime;
}

Getting correct NSDate value - is this the most efficient method?

I have this code to get the local date/time. It works but it seems a long way around the bush (10 statements) to get my current date/time value rather than the GMT time.
NSDate *currentDateGMT = [NSDate date];
NSDateFormatter *currentDateDateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *currentDateTimeZoneGMT = [NSTimeZone timeZoneWithName:#"GMT"];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[currentDateDateFormatter setTimeZone: currentDateTimeZoneGMT];
[currentDateDateFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
[currentDateDateFormatter setLocale:locale];
NSString *currentDateString = [currentDateDateFormatter stringFromDate:currentDateGMT];
NSDate *currentDateAdjusted = [currentDateDateFormatter dateFromString:currentDateString];
[currentDateDateFormatter release];
Can someone confirm that this is the best way to obtain the current machine value?
Thanks
NSDateFormatter will default to the users time zone so the simplest solution is
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//lower case h for hour will also default to the users
//12/24 hour clock preference
[formatter setDateFormat:#"yyyy-MM-dd hh:mm"];
NSString *currentDate = [formatter stringFromDate:[NSDate date]];
[formatter release];

Trouble printing NSDate returned from formatter

I want to see what is getting stored in an NSDate, so I am using NSLog, but it's showing (null), whereas if I print the string stf2, it's showing the proper value.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"YYYY-MM-dd"];
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM dd, yyyy"];
NSString *stf2 = [[pact.date componentsSeparatedByString:#" "] objectAtIndex:0];
NSLog(#"date %#",stf2);
NSDate *date_ = [formatter dateFromString:stf2];
pact.date = [formatter1 stringFromDate:date_];
NSLog(#"date %#",[NSDate date_]);
There are two specific problems in the code you've presented in the question.
Format Reset
First you do,
[formatter setDateFormat:#"YYYY-MM-dd"];
and then you initialize the second formatter followed by resetting the first formatter's format,
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM dd, yyyy"];
To emphasize
[formatter setDateFormat:#"MMM dd, yyyy"];
This should've been formatter1 but is formatter.
Date Format
If you look at the format you've use YYYY-MM-dd, it looks fine. But apparent YYYY have a different purpose and can be different from our usual calendar year. You should use the lowercase y instead.
[formatter setDateFormat:#"yyyy-MM-dd"];
And I don't think you meant this but
NSLog(#"date %#",[NSDate date_]);
should be
NSLog(#"date %#", date_);
you need to correct the dateformatter by setting proper date formatter. first do this
[formatter setDateFormat:#"yyyy-dd-MM"];
//it should be in the way as your string is. like if your string is 2011-Jun- 27 then fromatter should be
[formatter setDateFormat:#"yyyy-MM-dd"];
set the formatter as per your string's date format. then get the date back from this line
NSDate *date_ = [formatter dateFromString:stf2];
Assuming "stf2" is your string, then perhaps your object formatter is nil.
Below functions will be helpful to you.
"getDateTimeFromString" will take date and time as argument and it will return NSDate object.\
-(NSDate *)getDateTimeFromString :(NSString *)tempDate :(NSString *)tempTime{
NSString *dateValue = [tempDate stringByAppendingFormat:#" %#",tempTime];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateValue];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:date];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:date];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:date] autorelease];
return date;
}
"getDateStringFromDate" will take NSDate as argument and it will return NSString.
So, you can NSLog that value.
-(NSString *)getDateStringFromDate :(NSDate *)dateValue{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setTimeStyle:NSDateFormatterShortStyle];
[timeFormat setDateFormat:#"HH:mm a"];
NSString *theDate = [dateFormat stringFromDate:dateValue];
/*NSLog(#"\n"
"theDate: |%#| \n"
"theTime: |%#| \n"
, theDate, theTime);*/
return theDate;
}
Hope you will get the answer.

Why does my NSDateFormatter return nil when parsing?

my code is like this
NSString *tempDate = [NSString stringWithString:tempReviewData.pubDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateFormat:#"HH:mm a"];
NSDate *newDate = [dateFormatter dateFromString:tempReviewData.pubDate];
My newDate is getting nil at this point i dont know why
It seems to work for me but it depends on the format of tempReviewData.pubDate.
When I use invalid format, like #"6:30 M", I get null as well.
This is working:
NSString *tempDate = [NSString stringWithString:#"6:30 PM"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateFormat:#"HH:mm a"];
NSDate * newDate = [dateFormatter dateFromString:tempDate];
NSString * str = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"date: %#", newDate);
NSLog(#"str: %#", str);
Output:
2010-03-08 22:36:57.904 x[4340:903] date: 1970-01-01 12:30:00 +1000
2010-03-08 22:36:57.905 x[4340:903] str: 22:36 PM
NSDate *newDate = [dateFormatter dateFromString:tempReviewData.pubDate];
Does pubDate return an NSString, or an NSDate?
If it returns a string, then you should rename that property to clearly indicate that.
If it returns a date (NSDate), then trying to parse it as a string will not work, since it is not a string; moreover, you can cut out all this formatter code, since you already have the date object you're after.
It seems the NSDateFormatter has gotten very picky.
-(void)dateFormatterTests {
NSDateFormatter *formatter;
formatter = [[NSDateFormatter alloc] init];
#ifdef WORKS
[formatter setDateFormat:#"yyyy-MM-dd"];
#elif defined(ALSO_WORKS)
[formatter setDateFormat:#"yyyy MM dd"];
[formatter setLenient:YES];
#else // DOESN'T WORK
[formatter setDateFormat:#"yyyy MM dd"];
#endif
// Works per comments above
NSLog(#"dFS: %#", [formatter dateFromString:#"2010-01-13"]);
// Never works with any of the above formats
NSLog(#"dFS: %#", [formatter dateFromString:#"2010-01-13 22:00"]);
[formatter release]; formatter = nil;
}