Home > DeveloperSection > Forums > How to print NSDate using NSLog?
marcel ethan
marcel ethan

Total Post:105

Points:735
Posted on    December-06-2015 11:29 PM

 iPhone iPhone  Ios  Objective C 
Ratings:


 1 Reply(s)
 277  View(s)
Rate this:

I want to print NSDate in NSLog, but its not printing date correctly.

NSDateFormatter *format = [NSDateFormatter new];
[format setDateFormat: @"YYYY-MM-DD"];
NSDate *date = [NSDate date];
NSLog(@"Date: %@",format);

Can anyone tell me, what's wrong in my code..!



Tarun Kumar

Total Post:213

Points:1635
Supported
Posted on    December-07-2015 10:53 PM

To print date in NSLog use stringFromDate method of NSDateFormatter class.
Here, we provide the example, change your code by this code:

NSDateFormatter *format = [NSDateFormatter new];
[format setDateFormat: @"YYYY-MM-DD"];
NSLog(@"Date: %@",[format stringFromDate:Your_Date_obj]);

Don't want to miss updates? Please click the below button!

Follow MindStick