How do I represent it with date for "dd MMM yyyy", means "11 JUN 2019"? - objective-c

I have date string like this "11 June, 2019 16:35:21". When I try to convert the string to NSDate it returns nil.
NSString str =#"11 June, 2019 16:35:21";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: #"dd MMM yyyy"];
NSDate *date = [formatter dateFromString:str];
NSString *string = [[formatter stringFromDate:date] uppercaseString];
Is there any other way to convert it? Or where I'm doing wrong?

use date format as like
NSString str =#"11 June, 2019 16:35:21";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc]
initWithLocaleIdentifier:#"en_US_POSIX"];
[formatter setLocale:locale];
// input dateformat #"11 June, 2019 16:35:21";
[formatter setDateFormat: #"dd MMMM, yyyy HH:mm:ss"];
NSDate *date = [formatter dateFromString:str];
// output dateformat for e.g #"11 Jun 2019
[formatter setDateFormat: #"dd MMM yyyy"];
NSString *string = [[formatter stringFromDate:date] uppercaseString];

The NSDateFormatter uses Unicode Technical Standard.
this site has a great documentation:
Date_Format_Patterns
e.g. "11 June, 2019 16:35:21" can be parsed with:
[dateFormatter setDateFormat:#"dd MMMM, yyyy HH:mm:ss"]

Karthik is right, first, you have to convert date string with #"dd MMMM, yyyy HH:mm:ss" format to NSDate, then convert it to your desired format #"dd MMM yyyy".
Hope so following code'll work.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc]
initWithLocaleIdentifier:#"en_US_POSIX"];
[formatter setLocale:locale];
[formatter setDateFormat: #"dd MMMM, yyyy HH:mm:ss"];
NSString *str = #"11 June, 2019 16:35:21";
NSDate *date = [formatter dateFromString:str];
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: #"dd MMM yyyy"];
NSString *string = [[formatter stringFromDate:date] uppercaseString];

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.

converting date to correct format

I've a webservice which gives back my date in the following way.
Wed Oct 31 11:59:44 +0000 2012
But I want it to give it back in this way
31-10-2012 11:59
I know that it should be done with a NSDateFormatter. But I don't now how to implement it in the correct way.
I've something like this.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT+0:00"]];
NSDate *date = [dateFormatter dateFromString:[genkInfo objectForKey:DATE]];
Can anybody help me?
Kind regards.
Code at the moment
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:#"E MMM d hh:mm:ss Z y"];
NSDate *date = [f dateFromString:#"Wed Oct 31 11:59:44 +0000 2012"];
NSDateFormatter *f2 = [[NSDateFormatter alloc] init];
[f2 setDateFormat:#"dd-MM-y hh:mm"];
[f2 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString *date2 = [f2 stringFromDate:date];
Webservice layout
"text": "KRC Genk | Zaterdag is er opnieuw een open stadiontour http://t.co/tSbZ2fYG",
"created_at": "Fri Nov 02 12:49:34 +0000 2012"
Step 1: create an NSDateFormatter to convert your string from server to an NSDate object by setting the format to the format of the "server string"
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:#"E MMM d hh:mm:ss Z y"];
NSDate *date = [f dateFromString:#"Wed Oct 31 11:59:44 +0000 2012"];
Step 2: create another NSDateFormatter with the desired output string and convert your new NSDate object to a string object using the new NSDateFormatter
NSDateFormatter *f2 = [[NSDateFormatter alloc] init];
[f2 setDateFormat:#"dd-MM-y hh:mm"];
[f2 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString *s = [f2 stringFromDate:date];
desiredformat = s;
P.S. I'm not sure of f format, check this link
http://www.developers-life.com/nsdateformatter-and-uifont.html
There are a few issues with your format string for parsing the original date. And the locale isn't set properly. There is no need to set the timezone. That will be processed from the supplied date/time string.
NSDateFormatter *f = [[NSDateFormatter alloc] init];
NSLocale *posix = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[f setLocale:posix];
[f setDateFormat:#"EEE MMM dd hh:mm:ss Z yyyy"];
NSDate *date = [f dateFromString:#"Wed Oct 31 11:59:44 +0000 2012"];
You want to use the en_US_POSIX locale whenever you are parsing (or formatting) a fixed format date that is not from or for a user. Do not use the en_US_POSIX locale to display dates or times to a user.

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".

create a NSDate from a string

I have an NSString like this: #"3/15/2012 9:15 PM" and I would like to convert it to NSDate, I have done like this:
NSString *str =#"3/15/2012 9:15 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"mm/dd/yyyy HH:mm"];
NSDate *date = [formatter dateFromString:];
NSLog(#"%#", date); // date = null
Can you help me please, thanks.
Use the following solution
NSString *str = #"3/15/2012 9:15 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"MM/dd/yyyy HH:mm a";
NSDate *date = [formatter dateFromString:str];
NSLog(#"%#", date);
Edit:
Sorry, the format should be as follows:
formatter.dateFormat = #"MM/dd/yyyy hh:mm a";
And the time shown will be GMT time. So if you add/subtract the timezone, it would be 9:15 PM.
Edit: #2
Use as below. You would get exact time too.
NSString *str = #"3/15/2012 9:15 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"MM/dd/yyyy hh:mm a";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
formatter.timeZone = gmt;
NSDate *date = [formatter dateFromString:str];
NSLog(#"%#",date);
Your formatter is wrong. According to the NSDateFormatter documentation it should be "MM/dd/yyyy h:mm a". You also probably want to set the locale as stated in the Date Formatting Guideline:
If you're working with fixed-format dates, you should first set the
locale of the date formatter to something appropriate for your fixed
format. In most cases the best locale to choose is en_US_POSIX, a
locale that's specifically designed to yield US English results
regardless of both user and system preferences.
Try this:
NSString *str = #"3/15/2012 9:15 AM";
NSLocale *enUSPOSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setLocale:enUSPOSIXLocale];
[formatter setDateFormat:#"MM/dd/yyyy h:mm a"];
NSDate *date = [formatter dateFromString:str];
NSLog(#"%#",date);
Outputs:
2012-03-19 12:37:27.531 Untitled[6554:707] 2012-03-15 08:15:00 +0000
Hmmm - you've gone wrong for a couple of reasons
the NSDateFormatter is strict and you have not been.
you didn't supply str to dateFromString:
NSString* str = #"3/15/2012 9:15 PM";
NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"MM/dd/yyyy HH:mm a"];
NSDate* date = [formatter dateFromString:str];
NSLog(#"%#",date);
NSString *str =#"3/15/2012 9:15 PM";
NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
[dateformatter setDateFormat:#"MM/dd/yyyy"];
NSArray *firstSplit = [str componentsSeparatedByString:#" "];
NSDate *dateSelected = [dateformatter dateFromString:[firstSplit objectAtIndex:0]];
[dateformatter setDateFormat:#"mm:ss"];
NSDate *dateSelected2 = [dateformatter dateFromString:[firstSplit objectAtIndex:1]];
NSLog(#"%#",[dateformatter stringFromDate:dateSelected2]);
NSLog(#"%#",[dateformatter stringFromDate:dateSelected]);

Help with date formats

What's the correct format to parse this string:
"Mon, 14 Mar 2011 15:36:00 GMT"
into a NSDate using NSDateFormatter?
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss zzzz"];
NSDate *myDate = [dateFormatter dateFromString:strPubDate];
NSString *dateStr = [NSString stringWithString:#"Mon, 14 Mar 2011 15:36:00 GMT"];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:#"EEE, d MMM yyyy HH:mm:ss zzzz"];
NSDate *date = [dateFormat dateFromString:dateStr];