Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 215
user2588945 Avatar asked Sep 03 '13 10:09

user2588945


People also ask

What is NSDate?

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).

How do I change the current date format in Swift?

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.

What is a valid date format?

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.

Is NSDateFormatter thread safe?

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.


3 Answers

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.

like image 82
Pratik Avatar answered Oct 09 '22 23:10

Pratik


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.

like image 3
iEinstein Avatar answered Oct 10 '22 01:10

iEinstein


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.

like image 3
PeterParker Avatar answered Oct 10 '22 00:10

PeterParker