0

I used AFFeedParser in my app for parsing the article from different HTML page.

By parsing, I got the written article DATE like

"Thu, 06 Dec 2012 17:44:22 +0000\n\t\t" as a string.

How can I convert this into Date like Thursday,December 06,2012?

irrelephant
  • 4,091
  • 2
  • 25
  • 41
user1673099
  • 3,293
  • 7
  • 26
  • 57
  • 1
    google or in SO, thousands of answer similar to this is there.. or check NSDate documentation https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html – Anoop Vaidya Dec 25 '12 at 11:46
  • you can check my answer no compatible with ios. – Anoop Vaidya Dec 25 '12 at 12:32

3 Answers3

2
NSString *dateStr=@"Thu, 06 Dec 2012 17:44:22 +0000\n\t\t";
NSString *getDate=[[dateStr componentsSeparatedByString:@"\\"]objectAtIndex:0];

Here, you should take care of local representation of date and time so you should use.

NSDateFormatter dateFormat = [NSDateFormatter dateFormatFromTemplate:@"yMMMMd" options:0 locale:[NSLocale systemLocale]];

NSDate *date=[dateFormat dateFromString:getDate];

NSLog(@"dateString = %@", [dateFormat stringFromDate:date]);
1

See the Log also, i got it...

    NSString *yourStringDate = @"Thu, 06 Dec 2012 17:44:22 +0000\n\t\t";
    NSString *str =[yourStringDate stringByReplacingOccurrencesOfString:@"\n" withString:@""];

    str =[str stringByReplacingOccurrencesOfString:@"\t" withString:@""];
    [str retain];

    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss z"];

    NSDate *date = [dateFormatter dateFromString: str];

    dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"EEEE,LLLL dd,yyyy"];

    NSString *convertedString = [dateFormatter stringFromDate:date];
    NSLog(@"Converted String : %@",convertedString);

OUTPUT IS : Converted String : Thursday,December 06,2012

Also see my Another answer for get NSDate from string but in that you just set your format like above , just change the format from that... convert-string-to-nsdate-with-custom-format

i hope this helpful to you....

Community
  • 1
  • 1
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
0

Find the answer:

NSString *inputDate=@"Thu, 06 Dec 2012 17:44:22 +0000\n\t\t";
NSString *trimDate=[[inputDate componentsSeparatedByString:@"+"]objectAtIndex:0];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init] ;
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss"];

NSDate *date = [dateFormatter dateFromString:trimDate];

[dateFormatter setDateFormat:@"EEEE, MMMM dd,yyyy"];
NSString *dateString = [dateFormatter stringFromDate:date];

NSLog(@"dateString = %@", dateString);

Output:

dateString = Thursday,December 06,2012
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • Explanation of line 2: He creates an `NSArray` separated by "\" which returns "Thu, 06 Dec 2012 17:44:22 +0000","n","t","t" and selects the first element. – Fabio Poloni Dec 25 '12 at 12:06
  • no.... @"\" cant be used, so @"\\" one extra \ is used as escape character. Then Thu, `06 Dec 2012 17:44:22 +0000` is stored in the first index (i.e. 0) in the array that is stored in trimDate...after that it is simple. – Anoop Vaidya Dec 25 '12 at 12:08
  • As you see I translated @"\\" to the human "\" which is easier to understand, because not everybody knows what stringescaping means. Second, I tried to create a pseudo-array which contains 4 elements: 1:"Thu, 06 Dec 2012 17:44:22 +0000" | 2: "n" | 3: "t" | 4: "t". (I'm sorry that the "+0000" got on the next line in my first comment.) – Fabio Poloni Dec 25 '12 at 12:11
  • @FabioPoloni : Exactly, this is the logic, but where is the OP, he should also take care of our labor.... we think code, post for no result :) – Anoop Vaidya Dec 25 '12 at 12:13
  • Comments like these is why I love SO. – Fabio Poloni Dec 25 '12 at 12:14
  • but dateWithNaturalLanguageString method is getting error. Is there any framework required?? – user1673099 Dec 25 '12 at 12:14
  • @FabioPoloni: due to ios and mac API, i need to change the code, now this code will work on ios... – Anoop Vaidya Dec 25 '12 at 12:31
  • @AnoopVaidya: You don't have to separate by "+". Timezone (+0000) is supported by iOS. – Fabio Poloni Dec 25 '12 at 12:58
  • no, actually dateWithNaturalLanguageString is not in ios. So i have to again use formatter. And as you said @"\\" will create confusion, so why not to break the array be +. NO confusion to anyone :) – Anoop Vaidya Dec 25 '12 at 12:59