How to convert date string into format "17 Nov 2010" - iphone

I have the date that is in format 2010-11-17.This is of the form NSString.
I want to convert this NSString date into format 17 Nov 2010 and display in a label
This date is just not the current date but may even be the older dates.
So I cant use the [NSDate date] instance to get the date.
I tried using NSDateFormatter with the method dateFromString.
But it gives me a null value.
Here is the code snippet
NSString *dates = #”2010-11-16”;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM yyyy"];
NSDate *dates1 = [dateFormatter dateFromString:dates];
NSString *dates2 = [dateFormatter stringForObjectValue:dates1];
NSLog(#"dates : %#",dates);
NSLog(#"Dates 2 : %#",dates2);
[dateLabel setText:dates2];
NSLog(#"Formatted Date is : %#",dateLabel.text);
What should be done?
Please Help and suggest.
Thanks.

Try this
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:#"yyyy-MM-dd"];
NSDate *date2 = [formater dateFromString:#"2010-11-17"];
[formater setDateFormat:#"d MMM yyyy"];
NSString *result = [formater stringFromDate:date2];
NSLog(#"RESULT %#",result);
I got the result.Correct me if this approach is wrong.

Here are some formats for who desire to know!!
12:16:45 PM on January 01, 2000 --> hh:mm:ss a 'on' MMMM dd, yyyy
Wednesday, Sep 12, 2018 --> EEEE, MMM d, yyyy--
09/12/2018 --> MM/dd/yyyy--
09-12-2018 14:11 --> MM-dd-yyyy HH:mm--
Sep 12, 2:11 PM --> MMM d, h:mm a--
September 2018 --> MMMM yyyy--
Sep 12, 2018 --> MMM d, yyyy
Wed, 12 Sep 2018 14:11:54 +0000 --> E, d MMM yyyy HH:mm:ss Z
2018-09-12T14:11:54+0000 --> yyyy-MM-dd'T'HH:mm:ssZ
12.09.18 --> dd.MM.yy
10:41:02.112 --> HH:mm:ss.SSS
12:16:45 PM --> hh:mm:ss a
AM or PM --> a

NSDateFormatter uses date format patterns from UNICODE.
So for your 17 Nov 2010 date you need something like this:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"d MMM yyyy"];
NSString *result = [formatter stringForObjectValue:date];
Where date variable contains the date you'd like to format.
UPDATE: Try changing your code to this:
NSString *dates = #”2010-11-16”;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *dates1 = [dateFormatter dateFromString:dates];
[dateFormatter setDateFormat:#"dd MMM yyyy"];
NSString *dates2 = [dateFormatter stringForObjectValue:dates1];
NSLog(#"dates : %#",dates);
NSLog(#"Dates 2 : %#",dates2);
[dateLabel setText:dates2];
NSLog(#"Formatted Date is : %#",dateLabel.text);

Related

EDT to GMT Conversion

Input: Fri, 26 Apr 2013 21:56:31 EDT
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
[df1 setDateFormat:#"EE, dd MMM YYYY HH:mm:ss zzz"];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSString *startDateStr2 = [df1 stringFromDate:[NSDate date]];
NSLog(#"currentDate: %#", startDateStr2);
but i am getting output : Sat, 05 Jan 2013 07:26:31 GMT+05:30
Use below method which is used to change the format of the NSDate..
-(NSString *)changeDateFormat:(NSString*)stringDate dateFormat:(NSString*)dateFormat getwithFormat:(NSString *)getwithFormat{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:dateFormat];
NSDate *date = [dateFormatter dateFromString:stringDate];
dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:getwithFormat];
NSString *convertedString = [dateFormatter stringFromDate:date];
NSLog(#"Converted String : %#",convertedString);
return convertedString;
}
Call above method with its 3 parameters..
1. stringDate : Pass your date which in string type.
2. dateFormat : set format which is used in your passed string date. For example, if your string date is "14-03-2013 11:22 am" then set the format to #"dd-MM-yyyy hh:mm a".
3. getwithFormat : Set format which you want, as if you want date 14/03/2013 then just set format #"dd/MM/yyyy".
How to call this see below example with method:
NSString *strSDate = [self changeDateFormat:#"14-03-2013 11:22 am" dateFormat:#"dd-MM-yyyy hh:mm a" getwithFormat:#"dd/MM/yyyy"];
NSLog(#"\n\n Now Date => %#",strSDate);
Output is : Now Date => 14/03/2013
hope its helpful to you...

iOS - Converting string to non-en-GB locale NSDate returns nil [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
NSString to NSDate
I have a date in format Mon Jan 14 14:00:00 CET 2013 I try to convert it to NSDate:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz y"];
NSString *dateString = #"Mon Jan 14 14:00:00 CET 2013"
NSDate *date = [df dateFromString:dateString];
but it doesn't work and my date is nil
Input data is in en-GB locale, my device's locale is nb-NO
Any suggestions?
You're missing day in your format:
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz y"];
If it was not a typo, then next thing is to set proper locale so formatter will recognise CET timezone, for example en-GB will fix that:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
// that will fix the problem with not recognized CET timezone
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en-GB"]];
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz y"];
NSString *dateString = #"Mon Jan 14 14:00:00 CET 2013"
NSDate *date = [df dateFromString:dateString];
Try to use this function
- (NSDate*) dateFromString:(NSString*)aStr
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
//[dateFormatter setDateFormat:#"YYYY-MM-dd HH:mm:ss a"];
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSLog(#"%#", aStr);
NSDate *aDate = [dateFormatter dateFromString:aStr];
[dateFormatter release];
return aDate;
}
I hope this will helps u.
I think your Time Zone is wrong. Just use this code , it will work Perfectly :
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz y"];
NSString *dateString = #"Mon Jan 14 14:00:00 EDT 2013";
NSDate *date = [df dateFromString:dateString];
NSLog(#"date :: %#",date);
It will log Output as :
date :: 2013-01-14 18:00:00 +0000
EDIT :
I found Something for you : NSDateFormatter doesn't parse some timezones
You can solve this by using en_GB Locale , as stated : "These abbreviations do still work with the en_GB locale" in Working with Date and Time in Cocoa .
CET is not recognised
Try this :-
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE MMM dd HH:mm:ss yyyy"];
NSString *dateString = #"Mon Jan 14 14:00:00 2013";
NSDate *date = [df dateFromString:dateString];
NSLog(#"%#",date);
Hope it helps you
Simply "CET" is not a recognized time zone by NSDateFormatter.
Also the date/tine is over specified, best to not try include the day or week (Mon).
Here is an example that demonstrates working code with a recognized timezone:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz yyyy"];
NSString *dateString = #"Mon Jan 14 14:00:00 EST 2013";
NSDate *date = [df dateFromString:dateString];
NSLog(#"date: %#", date);
NSLog output
date: 2013-01-14 19:00:00 +0000
NSLog(#"abbreviationDictionary: %#", [NSTimeZone abbreviationDictionary]);
does show
CET = "Europe/Paris";
so this looks like an Apple bug in NSDateFormatter.
Report the bug at: Apple Bug Reporter
You can use:
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
If you still want to custom your date format try this one:
yyyy-MM-dd HH:mm:ss ZZZ
Because can't invent your own formatted string syntax and expect it to work; you need to actually use a documented format as the documentation points it out : Formatters in OS X v10.8 and iOS 6.0 use version tr35-25.
-> https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
If you are curious: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

Date Format Problem

How to get this format of date from NSString;
Wed, 22 Jun 2011 12:36:00 +0100 to Wed, 22 Jun 2011.
Thanks
Try this code.
NSString *dateStr = #"Wed, 22 Jun 2011 12:36:00 +0100";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE, d MMM yyyy HH:mm:ss ZZZ"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:#"EEE, MMM d YYYY"];
dateStr = [dateFormat stringFromDate:date];
NSLog(#"Date -- %#",dateStr);
[dateFormat release];
The minimalistic version is E, d MMM y or to specify 2 digit days and 4 digit years E, dd MMM yyyy. The Date Formatter uses the Unicode Technical Standard #35.

date format for NSDateFormatter

I need a date format for string Dec 31, 2010 3:20am to convert it to date?
Try this: (I havent) ;)
NSString *test = #"Dec 31, 2010 3:20am"
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"MM dd, yyyy HH:mm a"];
NSDate *testOut = [dateFormatter dateFromString:test];

NSDateFormatter Does not convert correctly

I have a NSString which is passed from an xml feed........
NSString *strDate =#"Thu, 22 Apr 2010 10.30 am CEST";
I'm currently using this code to format the date........
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy hh:mm a vvvv"];
NSDate *myDate = [dateFormatter dateFromString:date];
[dateFormatter setDateFormat:#"HH"];
NSLog(#"%#", [dateFormatter stringFromDate:myDate]);
I want to format my string only to display only hours and currently I'm getting value like this.
strDate =#"2010-04-10 14:00:00 +0530";
Can anyone please help me with this?......
I'm sorry.It's my mistake.It should be like this.
NSString *strDate =#"Thu, 22 Apr 2010 10:30 am CEST";
What my requirement is to get hour part only from above string using NSDateFormatter. How can I achieve that. Sorry for the earlier mistake.
Thanks.
If you want to get the 10 of 10:30 (if its ur requirement) then you can do it like:
strDate = #"Thu, 22 Apr 2010 10:30 am CEST";
NSArray *dateComponents = [strDate componentsSeparatedByString:#" "];
NSString *requiredString = [dateComponents objectAtIndex:4];
dateComponents = [requiredString componentsSeparatedByString:#":"];
requiredString = [dateComponents objectAtIndex:0];
and when you do:
NSLog(rquiredString);
Output : 10;
This is just a workaround, for better approach you should go through the NSDateComponents class.
Hope this helps.
Thanks,
Madhup
change to
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy hh.mm a vvvv"];
(. instead of : in hh:mm)
You want to do this:
NSString *strDate =#"Thu, 22 Apr 2010 10:30 am CEST";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy hh:mm a"];
NSDate *myDate = [dateFormatter dateFromString:strDate];
[dateFormatter setDateFormat:#"hh"];
strDate = [dateFormatter stringFromDate:myDate];
NSLog(#"%#", strDate);
(Firstly your original formatter was wrong, you had a: instead of a .) EDIT no longer the case
Secondly, you want to ignore the CEST bit as this will cause your timezone being changed