Line break in NSDateFormatter - nsdateformatter

I'm trying to create a line break between my date and time formats, but have not been successful. I've tried \n and \r with no success.
Thank you
Here is the code I have:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"EEEE, MMMM dd, yyyy 'at' hh:mm a";
NSDate *dateTime = [dateFormatter dateFromString:self.selectedTransaction.createdAt];

Related

Have troubles with NSDateFormatter while parsing

Here is the date sample: Wed, 13 May 2015 16:10:00 CEST
My formatter aint working
#"EEE, dd MMM yyyy HH:mm:ss vvvv"
What should i change?
UPD: full code
NSString* formattedDayWithString(NSString* date){
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [NSLocale currentLocale];
NSString *localeId = [locale displayNameForKey:NSLocaleIdentifier
value:[locale localeIdentifier]];
SLog(#"%# date:%#", localeId, date);
[inputFormatter setLocale: locale];
[inputFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss vvvv"];
NSDate *formattedDate = [inputFormatter dateFromString: date];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc]init];
[outputFormatter setDateFormat:#"d'.' MMMM yyyy"];
NSString* dateStr = [[NSString alloc] initWithFormat: #"%#",[outputFormatter stringFromDate:formattedDate]];
return dateStr;
}
"CEST" is a "short specific non-location format" and the corresponding
date field symbol is "z", not "v":
[inputFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z"];
In addition, you might have to set the date formatter locale explicitly to "en_GB"
as explained in Nsdateformatter + Timezone issue.

NSDateFormatter dateFromString conversion

I am having problems with conversion of NSString object to NSDate. Here is what I want to do:
I am downloading an RSS Message from the internet. I have the whole message parsed by NSXMLParser. After that, I have parts like , or saved to particular NSStrings. I want to convert element (that includes publication date of RSS Message) to NSDate so that I could perform some operations on it like on a date object (e.g. sorting, showing on a clock etc.). Here is the way my looks like:
"Wed, 25 Sep 2013 12:56:57 GMT"
I tried to convert it to NSDate in this way:
*//theString is NSString containing my date as a text
NSDate *dateNS = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy hh:mm:ss ZZZ"];
dateNS = [dateFormatter dateFromString:theString];*
However, after doing above code, dateNS always appear to be (null).
My question is simple: what is the right way to convert NSString with date formatted like this to NSDate object?
By the way, I have seen the website
http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns
It seems that there are many ways to format particular date, but I could not find what I am doing wrong.
Your problem is the your date formatter has not identical fort as your date string:
You should set date formatter the same format like your date string
My Example:
// Convert string to date
NSString *beginString = #"Sat, 30 Dec 2013 14:45:00 EEST";
//beginString = [beginString stringByReplacingOccurrencesOfString:#"EEST" withString:#""];
//beginString = [beginString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"]];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Helsinki"]];
[dateFormat setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z"];
dateFromString = [dateFormat dateFromString:beginString];
//NSLog(#"Begin string: %#", beginString);
//NSLog(#"not formated: %#", dateFromString);
// Convert Date to string
[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"ru_RU"]];
[dateFormat setDateFormat:#"dd MMMM yyyy"];
myStrDate = [dateFormat stringFromDate:dateFromString];
[currentTitle setPubDate:myStrDate];
NSDate * dateNS = [[NSDate alloc] init]; is useless, you don't need to allocate any date object.
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
NSLog(#"%#", [dateFormatter dateFromString:#"Wed, 25 Sep 2013 12:56:57 GMT"]);
Outputs the date correctly, are you sure theString isn't nil?

Format date with NSDateFormatter

I want to format 06-06-2013 1:51 PM as Jun 06,2013.
I have tried all possible different formatting styles using NSDateFormatter but failed.
Try this
First You need to convert this string back to NSDate then again convert the NSdate to string using formatter.
NSDateFormatter *dateFormatForDB = [[NSDateFormatter alloc] init];
[dateFormatForDB setDateFormat:#"dd-MM-yyyy HH:mm a"]; //Note capital H is 4 24-hour time format
NSDate *aDate = [[[NSDate alloc] initWithTimeInterval:0 sinceDate:[dateFormatForDB dateFromString:aDateString]] autorelease];
if(aDate){
[formatter setDateFormat:#"MMM dd,yyyy"];
NSString *date = [formatter stringFromDate:aDate];
[dateFormatForDB release];
}
Try to use this format
[df setDateFormat:#"MMM dd,yyyy];
By looking at your format, i think this is what you are looking for...
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd-MM-yyyy hh:mm a"];
NSDate *newDate = [df dateFromString:dateString];
NSDateFormatter *df2 = [[NSDateFormatter alloc] init];
[df2 setDateFormat:#"MMM dd,yyyy"];
NSString *formattedDate = [df2 stringFromDate:newDate];
This will give you the date as string in the required format. If you want time in 24hr format, replace 'hh' with 'HH'.

Change format of date

I got a NSString *string = #"2012-10-24 23:00:00 +0000";
And I want to convert that to a normal format 24 october 2012
I use the following code. But it keeps crashing. Can anyone help me?
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:#"yyyy MM dd HH:mm:SS zzz"];
NSDate *dateNS = [dateformat dateFromString:date];
[dateformat setDateFormat:#"dd MM yyyy"];
date = [dateformat stringFromDate:dateNS];
[dateformat release]
It keeps crashing because it can't create the NSDate from the input date because the dateformat is incorrect. it missing the - between the date and the seconds are ss not SS.
Also you need a date formate with MMMM to get the months full name:
NSString *string = #"2012-10-24 23:00:00 +0000";
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"];
NSDate *dateNS = [dateformat dateFromString:string];
[dateformat setDateFormat:#"dd MMMM yyyy"];
NSString *date = [dateformat stringFromDate:dateNS];
[dateformat release];

NSDate and NSDateFormatter return invalid CFStrinfRef

I've one problem with NSDate and NSDateFormatter.
From this NSString
NSString *startDate = #"Thu, 19 Apr 2012 13:09:56 +0000";
I would obtain a date localized with current locale like this "Thu, 19 Apr 2012 13:09".
I've elaborate this code but the 'endDate' string return nil (within the debugger obtain 'invalid CFStringRef').
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
NSDate *dateFromString = [dateFormatter dateFromString:startDate];
[dateFormatter setDateFormat:#"EEE dd MMM yyyy HH:mm"];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSString *endDate = [dateFormatter stringFromDate:dateFromString];
Where is the bug?
Alex.
It seems that the code executed in a non-US locale. I use non-US locales. I have the same nil output also.
It seems like we need to setLocale: before setDateFormat:.
Below code works ok:
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
NSLocale *usLocale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease];
[dateFormatter setLocale:usLocale];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
Ya its working correctly..Try removing autorelease from your dateFormatter and then try it again.
And i hope this link is same as yours
Invalid CFStringRef issue
Try this answer and change your format option with "ZZ" and not "ZZZ".