Obj-c, whats the least lines of code to get the current date without hour, minutes, seconds and millseconds?
I want to produce a date without hours, minutes, seconds or mill开发者_StackOverflow社区iseconds.
I've tried [NSDate date]
but that adds them.
Hows can I do this ?
Something like...
NSLog(@"%@", [[NSDate date]
descriptionWithCalendarFormat:@"%Y-%m-%d" timeZone:nil
locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]]);
...should do the trick on Mac OS X.
Try:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSDate *date = [NSDate date];
NSLog(@"%@", [dateFormatter stringFromDate:date]);
[dateFormatter release];
Adjust the date style to suit.
Or, as suggested, use setDateFormat
; something like:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-mm-dd"];
NSDate *date = [NSDate date];
NSLog(@"%@", [dateFormatter stringFromDate:date]);
[dateFormatter release];
精彩评论