How to format a NSDate in dd/MM/YYYY
Solution 1
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.
Solution 2
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.
Solution 3
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.
Solution 4
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.
Solution 5
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]];
user2588945
Updated on May 02, 2020Comments
-
user2588945 about 4 years
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:00and then when I use:
[dateFormat setDateFormat:@"MM/dd/yyyy"];
I get anull
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
-
user2588945 over 10 yearsthanks, I tried that but I get a null value for the formatted date. my original str value is: str 2013-08-23T15:21:19+02:00
-
Rachit over 10 yearsI have edited my answer according to your input string. Please check.
-
Rob over 9 yearsAlso consider setting the
locale
as suggested in Apple Technical Q&A 1480.