简体   繁体   中英

How to get current date to particular format

I am new to iOS.

I got wrong format when getting NSLog for date.

Output is 2013-12-06 18:30:00 +0000 but I need to display in dd/mm/yyyy format.

Here is my code:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSLog(@"%@", [dateFormatter stringFromDate:[NSDate date]]);
[dateFormatter setDateFormat:@"dd/MM/yyyy"];
NSLog(@"%@", [dateFormatter stringFromDate:[NSDate date]]);

NSString *str=[dateFormatter stringFromDate:[NSDate date]];

[dateFormatter setDateFormat:@"dd/MM/yyyy"];
NSDate *date=[dateFormatter dateFromString:[dateFormatter stringFromDate:[NSDate date]]];

[dateFormatter release];

Don't make it complex,this should work.

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

May this help you.

NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"dd/MMM/yyyy"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];
NSLog(@"%@",dateString);

Enjoy coding

NSDate *date = [NSDate date];
NSLog(@"%@",date);

Above code will give you following output: 2014-11-27 10:44:43 +0000

But whenever you want to set date in proper format by removing time then follow the below code ,

NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:date];
NSLog(@"%@",dateString);

The Output will be : 27/11/2014

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM