-1

I am getting date and time as string from web service those looks like date is "2013-10-29" and "11:30"(or "23:30") but I want show date like "29 OCT.2013" and Time is "11:30 AM"(or "11:30 PM"). I have gone through so many answers regarding this type of questions but when i tried its returning "null" without error or warings.Please help me out...

Srinivasa Reddy
  • 77
  • 1
  • 2
  • 10
  • 4
    Please share code of what you have tried and possible links you have looked at we could just be telling you what you have already seen. – Popeye Oct 28 '13 at 10:44
  • can you pls post the exact date string you want to convert in a NSDate object pls ? – Manu Oct 28 '13 at 10:46
  • I'd like to vote for closing this question as a duplicate. But there are so many duplicates that I can't decide which one to select. So please be more specific and provide details on what you've already tried and why it didn't work. – tilo Oct 28 '13 at 11:03
  • This is simply duplicate of `NSDateFormatter` there is no need for any complex and extra calculation @tilo – TheTiger Oct 28 '13 at 11:04
  • @TheTiger this was more of a "you said you've gone through so many answers .. go on and you'll find the one that fits your specific problem." – tilo Oct 28 '13 at 11:06
  • @tilo Actually I'm unable to find any issue here. If anybody knows use of `NSDateFormatter` its very simple. And I'm sure 2-3 minutes R&D on google can solve this easily. – TheTiger Oct 28 '13 at 11:09

3 Answers3

11

Apply this code changing the manually coded time and date with your properties and It should work.

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate * dateNotFormatted = [dateFormatter dateFromString:@"2013-10-29"];
[dateFormatter setDateFormat:@"d MMM.YYYY"];    
NSString * dateFormatted = [dateFormatter stringFromDate:dateNotFormatter];

[dateFormatter setDateFormat:@"HH:mm"];
NSDate * timeNotFormatted = [dateFormatter dateFromString:@"11:30"];
[dateFormatter setDateFormat:@"hh:mm a"];    
NSString * timeFormatted = [dateFormatter stringFromDate:timeNotFormatter];

The other option is put tighter time and date in one string and use this format:

    [dateFormatter setDateFormat:@"d MMM.YYYY at hh:mm a"];    
Alex
  • 1,061
  • 10
  • 14
0

Use NSDateFormatter. In first case your format will be: @"yyyy-MM-dd", @"HH:mm", and in the second one: @"dd MMM.yyyy", @"hh:mm a"

Sviatoslav Yakymiv
  • 7,887
  • 2
  • 23
  • 43
  • NSDateFormatter *formatter=[[NSDateFormatter alloc]init]; [formatter setDateFormat:@"dd MMM.YYYY "]; NSDate *date=[[NSDate alloc]init]; date=[formatter dateFromString:@"2013-10-29"]; and here date is returning "null" – Srinivasa Reddy Oct 28 '13 at 11:02
0

First of combine date & Time like "2013-10-29 11:30"

than use the below function like [self dateFormat:@"yyyy-NN-dd HH:ss" newFormat:@"dd MMM.yyyy hh:mm a"]

- (NSString*) dateFormat : (NSString*)date currentFormat:(NSString*) currentFormat newFormat : (NSString*) newFormat
{
    NSString *dateStr = date;
    NSDateFormatter *dtF = [[NSDateFormatter alloc] init];
    [dtF setDateFormat:currentFormat];
    NSDate *d = [dtF dateFromString:dateStr];

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:newFormat];
    NSString * strFinalDate = [dateFormat stringFromDate:d];
    NSLog(@"%@",strFinalDate);

    [dtF release];
    [dateFormat release];

    return strFinalDate;
}

if you want Date and Time differ than separate it using NSArray.

Hitarth
  • 1,950
  • 3
  • 27
  • 52