Measure elapsed time in Swift

Update With Swift 5.7, I think everything below becomes obsolete. Swift 5.7 introduces the concept of a Clock which appears to have a function designed to do exactly what is required here. I’ll update with an example as soon as I’ve got Swift 5.7 and have the time to rework it. Here’s a Swift function … Read more

What’s the optimum way of storing an NSDate in NSUserDefaults?

You are needlessly complicating things. Why are you converting the date to a time interval (then the time interval to a different primitive)? Just [sharedDefaults setObject:theDate forKey:@”theDateKey”] and be done with it. NSDate is one of the “main types” supported by the PLIST format (dates, numbers, strings, data, dictionaries, and arrays), so you can just … Read more

How can I convert string date to NSDate?

try this: let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = /* find out and place date format from * http://userguide.icu-project.org/formatparse/datetime */ let date = dateFormatter.dateFromString(/* your_date_string */) For further query, check NSDateFormatter and DateFormatter classes of Foundation framework for Objective-C and Swift, respectively. Swift 3 and later (Swift 4 included) let dateFormatter = DateFormatter() dateFormatter.dateFormat = /* … Read more

Convert string to date in Swift

Convert the ISO8601 string to date let isoDate = “2016-04-14T10:44:00+0000” let dateFormatter = DateFormatter() dateFormatter.locale = Locale(identifier: “en_US_POSIX”) // set locale to reliable US_POSIX dateFormatter.dateFormat = “yyyy-MM-dd’T’HH:mm:ssZ” let date = dateFormatter.date(from:isoDate)! Get the date components for year, month, day and hour from the date let calendar = Calendar.current let components = calendar.dateComponents([.year, .month, .day, .hour], … Read more

Converting NSString to NSDate (and back again)

Swift 4 and later Updated: 2018 String to Date var dateString = “02-03-2017” var dateFormatter = DateFormatter() // This is important – we set our input date format to match our input string // if the format doesn’t match you’ll get nil from your string, so be careful dateFormatter.dateFormat = “dd-MM-yyyy” //`date(from:)` returns an optional … Read more

How to compare two NSDates: Which is more recent?

Let’s assume two dates: NSDate *date1; NSDate *date2; Then the following comparison will tell which is earlier/later/same: if ([date1 compare:date2] == NSOrderedDescending) { NSLog(@”date1 is later than date2″); } else if ([date1 compare:date2] == NSOrderedAscending) { NSLog(@”date1 is earlier than date2″); } else { NSLog(@”dates are the same”); } Please refer to the NSDate class … Read more

Getting the difference between two Dates (months/days/hours/minutes/seconds) in Swift

Xcode 8.3 • Swift 3.1 or later You can use Calendar to help you create an extension to do your date calculations as follow: extension Date { /// Returns the amount of years from another date func years(from date: Date) -> Int { return Calendar.current.dateComponents([.year], from: date, to: self).year ?? 0 } /// Returns the … Read more

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

Date Format in Swift

This may be useful for who want to use dateformater.dateformat; if you want 12.09.18 you use dateformater.dateformat = “dd.MM.yy” Wednesday, Sep 12, 2018 –> EEEE, MMM d, yyyy 09/12/2018 –> MM/dd/yyyy 09-12-2018 14:11 –> MM-dd-yyyy HH:mm Sep 12, 2:11 PM –> MMM d, h:mm a September 2018 –> MMMM yyyy Sep 12, 2018 –> MMM … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)