0
votes

i want to separate out day month year time how will i do

NSString *datefromweb = [[tableArray objectAtIndex:indexPath.row ] objectForKey:@"ContentDateTime"];

NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"dd MMM yyyy HH:mm"];

NSDate *date1 = [[NSDate alloc] init];
date1 = [dateformatter dateFromString:datefromweb];
NSLog(@"date1=%@",date1);
[dateformatter setDateFormat:@"MMM yyyy"];

NSString *month1 = [dateformatter stringFromDate:date1];
[dateformatter setDateFormat:@"hh:mm a"];

NSString *time= [dateformatter stringFromDate:date1];
[dateformatter setDateFormat:@"dd"];

NSString *day1= [dateformatter stringFromDate:date1];

    cell.day.text=day1;
    cell.monthYear.text=month1;
    cell.ldatetime.text=time;

we get string in datefromweb(NSString) variable but not get in datefromstring(NSDate) variable and i am separate out date,time,month,year.

my response is {ContentDateTime = "02 Jul 2016  5:12PM";}
2
It's probably best if you used a standard timestamp format - you're not including the timezone?nikkumang
dd MMM yyyy HH:mm doesn't match 02 Jul 2016 5:12PM. Well, for the Day, Month and Year, it seems to, but not for the hours/minutes. So date1 should be nil. And so nothing after should work. You wrote yourself afterwards hh:mm a, so you know how to get AM/PM, no? So the good format was something like dd MMM yyyy hh:mm a.Larme

2 Answers

0
votes

Use NSDateComponents. Try this code

NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:@"dd MMM yyyy  hh:mma"];
NSDate *date = [formatter dateFromString:@"02 Jul 2016  5:12PM"];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comonents =
[cal components:(NSCalendarUnitDay | NSCalendarUnitMonth |  NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond
                       ) fromDate:date];
NSInteger day = [comonents day];
NSInteger month = [comonents month];
NSInteger year = [comonents year];
NSInteger hour = [comonents hour];
NSInteger minutes = [comonents minute];
NSInteger seconds = [comonents second];
0
votes

I went through your code sorry for late answer. Just Change the date format.

 NSDateFormatter *formatter = [NSDateFormatter new];
 [formatter setDateFormat"dd MMM yyyy  hh:mma"];