Convert NSDate to NSString Convert NSDate to NSString swift swift

Convert NSDate to NSString


How about...

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)}


I don't know how we all missed this: localizedStringFromDate:dateStyle:timeStyle:

NSString *dateString = [NSDateFormatter localizedStringFromDate:[NSDate date]                                                       dateStyle:NSDateFormatterShortStyle                                                       timeStyle:NSDateFormatterFullStyle];NSLog(@"%@",dateString);

outputs '13/06/12 00:22:39 GMT+03:00'


Hope to add more value by providing the normal formatter including the year, month and day with the time.You can use this formatter for more than just a year

[dateFormat setDateFormat: @"yyyy-MM-dd HH:mm:ss zzz"];