如何转换,NSDate以NSString使得只有一年 @“YYYY” 格式输出到字符串?
NSDate
NSString
怎么样…
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy"]; //Optionally for time zone conversions [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]]; NSString *stringFromDate = [formatter stringFromDate:myNSDateInstance]; //unless ARC is active [formatter release];
Swift 4.2:
func stringFromDate(_ date: Date) -> String { let formatter = DateFormatter() formatter.dateFormat = "dd MMM yyyy HH:mm" //yyyy return formatter.string(from: date) }