Swift – Get local date and time

update: Xcode 8.2.1 • Swift 3.0.2 You can also use the Date method description(with locale: Locale?) to get user’s localized time description: A string representation of the Date, using the given locale, or if the locale argument is nil, in the international format YYYY-MM-DD HH:MM:SS ±HHMM, where ±HHMM represents the time zone offset in hours … Read more

NSDate of yesterday

You should use NSCalendar for calculating dates. For example, in Swift 3 the date two days before today is: let calendar = Calendar.current let twoDaysAgo = calendar.date(byAdding: .day, value: -2, to: Date()) Or in Swift 2: let calendar = NSCalendar.currentCalendar() let twoDaysAgo = calendar.dateByAddingUnit(.Day, value: -2, toDate: NSDate(), options: []) Or to get the first … Read more

Sorting NSMutableArray By Object’s Property

NSSortDescriptorss make this really simple. With NSMutableArray you can sort the existing array using sortUsingDescriptors: and with immutable arrays you create a new array using sortedArrayUsingDescriptors: //This will sort by stringProperty ascending, then dateProperty ascending [mutable_array sortUsingDescriptors: @[ [NSSortDescriptor sortDescriptorWithKey:@”stringProperty” ascending:YES], [NSSortDescriptor sortDescriptorWithKey:@”dateProperty” ascending:YES] ]];

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

Current Week Start and End Date

rangeOfUnit:startDate:interval:forDate:. It gives you the start and the interval for a certain time unit. With it it is easy to find the start of the week in the used calendar and add the range-1 to get the latest second in that week. NSCalendar *cal = [NSCalendar currentCalendar]; NSDate *now = [NSDate date]; NSDate *startOfTheWeek; NSDate … Read more

How to format time intervals for user display (social network like) in swift?

If you are targeting newer OS versions (iOS 13.5+, OS X 10.15+), you can use RelativeDateTimeFormatter: let formatter = RelativeDateTimeFormatter() formatter.dateTimeStyle = .named for d in [-12600.0, -90000.0, -900.0, 13500.0] { let str = formatter.localizedString(fromTimeInterval: d) print(“\(d): \(str)”) } // Output -12600.0: 3 hours ago -90000.0: yesterday -900.0: 15 minutes ago 13500.0: in 3 hours … Read more

Difference between 2 dates in seconds ios

since you are not using ARC, when you write startTime = [NSDate date]; you do not retain startTime, so it is deallocated before -viewWillDisappear is called. Try startTime = [[NSDate date] retain]; Also, I recommend to use ARC. There should be much less errors with memory management with it, than without it