Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set nsdate format for iphone

I want date in below format.

04/22/2011 1:05:21 PM

format is mm/dd/yyyy time(12 hour) pm or AM

what is the code for this?

like image 214
GameLoading Avatar asked Apr 22 '11 07:04

GameLoading


People also ask

Is DateFormatter thread safe iOS?

Thread SafetyOn iOS 7 and later NSDateFormatter is thread safe. In macOS 10.9 and later NSDateFormatter is thread safe so long as you are using the modern behavior in a 64-bit app.

What is en_US_POSIX in iOS?

"en_US_POSIX" is also invariant in time (if the US, at some point in the future, changes the way it formats dates, "en_US" will change to reflect the new behaviour, but "en_US_POSIX" will not), and between machines ("en_US_POSIX" works the same on iOS as it does on OS X, and as it it does on other platforms).

What is NSDate?

The NSDate class provides methods for comparing dates, calculating the time interval between two dates, and creating a new date from a time interval relative to another date.

How do you format a date?

Press CTRL+1. In the Format Cells box, click the Number tab. In the Category list, click Date, and then choose a date format you want in Type.


3 Answers

    NSDate *date=[NSDate date];
    NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
    NSString *dateOfGame =[formatter stringFromDate:date];
    NSLog(@"dateOfGame%@",dateOfGame);
    [formatter release];
like image 86
Gypsa Avatar answered Oct 21 '22 08:10

Gypsa


Check NSDateFormatter (and Date Formatting)

MM/dd/yyyy hh:mm:ss a

like image 28
ttarik Avatar answered Oct 21 '22 10:10

ttarik


some other type

NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"MM/dd/yyyy"];
 NSLog(@"date=%@",[formatter stringFromDate:date]);//date=04/22/2011

[formatter setTimeStyle:NSDateFormatterBehavior10_4];
NSLog(@"date=%@",[formatter stringFromDate:date]);// date=1:05:11 PM

[formatter setTimeStyle:NSDateFormatterBehaviorDefault];
NSLog(@"date=%@",[formatter stringFromDate:date]); date=

[formatter setTimeStyle:NSDateFormatterFullStyle];
NSLog(@"date=%@",[formatter stringFromDate:date]); date=1:05:11 PM India Standard Time

[formatter setTimeStyle:NSDateFormatterLongStyle];
NSLog(@"date=%@",[formatter stringFromDate:date]); date=1:05:11 PM GMT+05:30

[formatter setTimeStyle:NSDateFormatterMediumStyle];
NSLog(@"date=%@",[formatter stringFromDate:date]); date=1:05:11 PM

[formatter setTimeStyle:NSDateFormatterNoStyle];
NSLog(@"date=%@",[formatter stringFromDate:date]);date=

[formatter setTimeStyle:NSDateFormatterShortStyle];
NSLog(@"date=%@",[formatter stringFromDate:date]); date=1:05 PM
like image 1
GameLoading Avatar answered Oct 21 '22 09:10

GameLoading