How to format NSDate in dd / MM / YYYY format

I am trying to get NSDate in the format: 05/20/2012 10:30 PM

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

and then when I use:

[dateFormat setDateFormat:@"MM/dd/yyyy"]; I get a null return value 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 ]; 

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

thanks for any help

+6
source share
8 answers

you need to do this, but check your date format first

 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 date format is the same as on your page.

+35
source

First set the NSDateFormatter to the format you get to save it in the NSDate object, and then use the desired format again to change it to the desired format and show it. For more information, you can find the answer from Pratik .

Hope this helps.

+3
source

set the creation date to [dateFormat setDateFormat: @ "dd / MM / yyyy HH: mm"]; so u code is written in a 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 this will be helpful.

+3
source

I think that forced use of a locale and its appearance programmatically is a bad idea, unless it is specified as a requirement.

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

Now, to answer your question: If you add a short time format to the average date format, you will get the desired result, while maintaining both localizability and customizability.

+2
source

Just use the code below and you will get the result:

// convert timestamp

 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]]; 
+2
source

Set the date format for

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

// convert timestamp

 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 ]; 
+1
source

Take a look at DateFormatters to use the required formats: Apple Doc

0
source

** 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];** 
0
source

Source: https://habr.com/ru/post/953047/


All Articles