4

Below is my code

 NSString *dateandtime =[NSString stringWithFormat:@"%@",self.eventtime];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];
    NSDate *dateFromString = [[NSDate alloc] init];
    dateFromString = [dateFormatter dateFromString:dateandtime];

if i print my self.event name i am getting the output as Mon, 7 Jan 2013 15:30:00 CST but when i pass this string in NSDateFormatter i am getting null value i don't know why i tried many combinations of date formats, but i cant solve it, can anyone help me

Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
Pradeep Kumar
  • 401
  • 5
  • 18
  • in your self.event, you are getting CST, it should be time zone for example +0530 – P.J Jan 08 '13 at 08:30
  • I don't know if this is the cause but if it is showing 7 Jan then that should be "d MMM" not "dd MMM" – Fogmeister Jan 08 '13 at 08:30
  • i am getting that from xml, how can i change it in to time zone – Pradeep Kumar Jan 08 '13 at 08:31
  • i tried Fogmeister, but it doesn't word – Pradeep Kumar Jan 08 '13 at 08:32
  • @Fogmeister : 7 Jan should be dd MMM not as d MMM. You cant make two formatters for 1-9 and 10-31. – Anoop Vaidya Jan 08 '13 at 08:39
  • @AnoopVaidya : dd is the zero padded day (i.e. 01, 02, 03, ..., 29, 30, 31). d is the non-zero padded day (i.e. 1, 2, 3, ..., 29, 30, 31). – Fogmeister Jan 08 '13 at 08:48
  • 1
    I think what you require is : Why does NSDateFormatter return nil?[1] [1]: http://stackoverflow.com/questions/2254003/why-does-nsdateformatter-return-nil?rq=1 – Bhavin Jan 08 '13 at 08:50
  • @AnoopVaidya and @Fogmeister : it will work for both `d` and `dd` in these situation – Paresh Navadiya Jan 08 '13 at 08:51
  • Also check http://stackoverflow.com/questions/5838497/nsdateformatter-not-working-properly-with-utc – Sulthan Jan 08 '13 at 09:47
  • @PradeepKumar you got the solution?? otherwise see my bellow answer i use with your string like NSString *dateandtime = @"Mon, 7 Jan 2013 15:30:00 CST"; NSDate *dateFromString = [[NSDate alloc] init]; dateFromString = [self convertStringToDate:dateandtime]; NSLog(@"\n\n Date is %@",dateFromString); and it will give me th output Date is 2013-01-07 10:00:00 +0000 dude.. just use formater with [formatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss 'CST'"]; this – Paras Joshi Jan 08 '13 at 12:04
  • Why are you allocating an NSDate object and then throwing it away?? – Hot Licks Jan 08 '13 at 13:28
  • Why do you have the first line if self.eventtime is already a NSString? – Hot Licks Jan 08 '13 at 13:30

4 Answers4

1

i solved my problem as Per Prateek comments, now its working for me...Thanks Prateek.

in your self.event, you are getting CST, it should be time zone for example +0530
Pradeep Kumar
  • 401
  • 5
  • 18
0

Use :

[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss z"];
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
0

Use the below method for getting the NSDate from NSString date

NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [self convertStringToDate:dateandtime];

Paste this below method in your .m file...

-(NSDate *)convertStringToDate:(NSString *) date {
        NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; 
        NSDate *nowDate = [[[NSDate alloc] init] autorelease];
        [formatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss 'CST'"];// set format here  format in which the date should be

        date = [date stringByReplacingOccurrencesOfString:@"+0000" withString:@""];
        nowDate = [formatter dateFromString:date];
        // NSLog(@"date============================>>>>>>>>>>>>>>> : %@", nowDate);
        return nowDate;
}

For More Information About NSDate component see this post of My Blog NSDate-formate-info-for-iphone-sdk

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • @PradeepKumar you got it?? now i just update the answer , its return Date is 2013-01-07 10:00:00 +0000 output.. – Paras Joshi Jan 08 '13 at 09:35
  • @PradeepKumar see my another answer from this link http://stackoverflow.com/questions/13251633/convert-string-to-nsdate-with-custom-format/13251656#13251656 like your question – Paras Joshi Jan 08 '13 at 09:53
0

Use this code...

- (NSString *)formattedDate:(NSString*)dateSte {

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [NSTimeZone resetSystemTimeZone];
    NSTimeZone *gmtZone = [NSTimeZone systemTimeZone];
    [dateFormatter setTimeZone:gmtZone];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];

    NSDate *date = [dateFormatter dateFromString:dateSte];

    [dateFormatter setDateFormat:@"EEE,MMM d,''yyyy"];
    NSString *strDate = [dateFormatter stringFromDate:date];

    return strDate;
}
BADRI
  • 643
  • 8
  • 26