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
NSDate objects encapsulate a single point in time, independent of any particular calendrical system or time zone. Date objects are immutable, representing an invariant time interval relative to an absolute reference date (00:00:00 UTC on 1 January 2001).
We start by creating a Date object. To convert the date to a string, we need to create a date formatter, an instance of the DateFormatter class. To convert the Date object to a string, we invoke the date formatter's string(from:) instance method.
Valid dates range from 12/31/1969 to 01/18/2038. Valid dates may differ depending on the type of machine (PC or host) and the type of CPU chip. * This format defaults to a two-digit year, but can be overridden to have four digits.
Thread Safety On earlier versions of the operating system, or when using the legacy formatter behavior or running in 32-bit in macOS, NSDateFormatter is not thread safe, and you therefore must not mutate a date formatter simultaneously from multiple threads.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With