user2588945
user2588945

Reputation: 1681

How to format a NSDate in dd/MM/YYYY

I'm trying to get an NSDate into the format: 20/05/2014 22:30

here's the code I currently have. [dateFormat setDateFormat:@"YYYY-MM-dd\'T\'HH:mm:ssZZZZZ"]; this works but the format is not like 20/05/2014 22:30 it displays the date in format like: 12-03-2013 12:00:00 +00:00:00

and then when I use:

[dateFormat setDateFormat:@"MM/dd/yyyy"]; I get a null value returned instead of a formatted date.

// timestamp conversion
    NSString *str = [timeStamp objectAtIndex:indexPath.row];
    // convert to date
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
  //  [dateFormat setDateFormat:@"YYYY-MM-dd\'T\'HH:mm:ssZZZZZ"];

 [dateFormat setDateFormat:@"MM/dd/yyyy"];

    NSDate *dte = [dateFormat dateFromString:str];
    cell.timeStampLabel.text = [NSString stringWithFormat:@"%@",dte ];

the str original string output before formatting is str 2013-08-23T15:21:19+02:00

thanks for any help

Upvotes: 7

Views: 44500

Answers (8)

Kalpesh Satasiya
Kalpesh Satasiya

Reputation: 1

**NSString *strNotificationTimeAndDate="Your date";

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss.z"];

or

[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss+z"];

or

[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ssz"];

or

[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];
NSDate *commentdate = [dateFormat 
dateFromString:strNotificationTimeAndDate];**

Upvotes: 0

Gobi Manickam
Gobi Manickam

Reputation: 3267

Have a look at DateFormatters to use required formats: Apple Doc

Upvotes: 0

Rachit
Rachit

Reputation: 1189

Just use below code and you will get the result:

// timestamp conversion

NSString *str = [timeStamp objectAtIndex:indexPath.row];

// convert to date

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd T HH:mm:ss ZZZZ"];

NSDate *dte = [dateFormat dateFromString:str];

[dateFormat setDateFormat:@"dd/MM/yyyy HH:mm"];

cell.timeStampLabel.text = [NSString stringWithFormat:@"%@",[dateFormat stringFromDate:dte]];

Upvotes: 2

Pétur Ingi Egilsson
Pétur Ingi Egilsson

Reputation: 4452

I think that enforcing the locale` and its appearance programatically is a bad idea unless it is has been specified as a requirement.

An alternative would be to use setDateStyle on the NSDateFormatter, to one of the formats specified under System Preferences -> Language & Text -> Region -> Dates. There is a Short , Medium and a Long format to choose from for both Time and Date. Available styles are listed in the documentation under NSDateFormatterStyle

Now to answer your question: If you would append the short time format to the medium date format then you would get your desired outcome, while maintaining both localizability and customisability.

Upvotes: 3

PeterParker
PeterParker

Reputation: 308

set your date formate in [dateFormat setDateFormat:@"dd/MM/yyyy HH:mm"]; so that u code write in way

// timestamp conversion
NSString *str = [timeStamp objectAtIndex:indexPath.row];
// convert to date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
//  [dateFormat setDateFormat:@"YYYY-MM-dd\'T\'HH:mm:ssZZZZZ"];

[dateFormat setDateFormat:@"dd/MM/yyyy HH:mm"];

NSDate *dte = [dateFormat dateFromString:str];
cell.timeStampLabel.text = [NSString stringWithFormat:@"%@",dte ];

I hope it is helpful.

Upvotes: 3

Pratik
Pratik

Reputation: 2399

you have to do like this but please first of check your str's date format

NSString *str = [timeStamp objectAtIndex:indexPath.row];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-dd\'T\'HH:mm:ssZZZZZ"];


NSDate *dte = [dateFormat dateFromString:str];

[dateFormat setDateFormat:@"dd/MM/yyyy HH:mm"];
cell.timeStampLabel.text = [NSString stringWithFormat:@"%@",[dateFormat stringFromDate:dte]];

first time set date format same as in your str.

Upvotes: 37

iEinstein
iEinstein

Reputation: 2100

First set the NSDateFormatter to the format you are receiving data to retain it into a NSDate Object and then again use your desired format to change it into the format you want and show it. For more you can see Pratik answer.

Hope this helps.

Upvotes: 3

Toseef Khilji
Toseef Khilji

Reputation: 17409

Set Your date formatter to

 [dateFormat setDateFormat:@"dd/MM/yyyy HH:mm"];

// timestamp conversion

NSString *str = [timeStamp objectAtIndex:indexPath.row];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy HH:mm"];

NSDate *dte = [dateFormat dateFromString:str];
cell.timeStampLabel.text = [NSString stringWithFormat:@"%@",dte ];

Upvotes: 1

Related Questions