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

How do I get the current Date in short format in Swift

Xcode 11 or later • Swift 5.1 or later extension TimeZone { static let gmt = TimeZone(secondsFromGMT: 0)! } extension Locale { static let ptBR = Locale(identifier: “pt_BR”) } extension Formatter { static let date = DateFormatter() } extension Date { func localizedDescription(date dateStyle: DateFormatter.Style = .medium, time timeStyle: DateFormatter.Style = .medium, in timeZone: TimeZone … Read more

Difference between ‘YYYY’ and ‘yyyy’ in NSDateFormatter

Also when using a date format string using the correct format is important. @”YYYY” is week-based calendar year. @”yyyy” is ordinary calendar year. You can go through the whole blog, its a good to give it a look https://web.archive.org/web/20150423093107/http://realmacsoftware.com/blog/working-with-date-and-time http://realmacsoftware.com/blog/working-with-date-and-time (dead link)

Converting NSString to NSDate gives nil [duplicate]

i know this question false in duplicate category & i flagged it as well. but i am answering here with explanation, if in case OP does not get the references . NSString *dateString = @”2014-04-30T00:00:00″; //input string NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@”yyyy-MM-dd’T’HH:mm:ss”];// fomatter in your case NSDate *date = [formatter dateFromString:dateString]; NSLog(@”date is%@”,date); … Read more

Calculate next date in week

Use [NSCalendar currentCalendar] to get NSDateComponents from the current date, especially NSWeekdayCalendarUnit and NSWeekOfYearCalendarUnit. the components will have a member weekday, that ranges from 1 — sunday to 7 — saturday. if it is 1 or 2, create a new componets with weekday 2, if it is 7, add one to weekOfYear and set weekday … Read more