iOS: Compare two dates

According to Apple documentation of NSDate compare: Returns an NSComparisonResult value that indicates the temporal ordering of the receiver and another given date. – (NSComparisonResult)compare:(NSDate *)anotherDate Parameters anotherDate The date with which to compare the receiver. This value must not be nil. If the value is nil, the behavior is undefined and may change in … Read more

Swift – check if a timestamp is yesterday, today, tomorrow, or X days ago

Swift 3/4/5: Calendar.current.isDateInToday(yourDate) Calendar.current.isDateInYesterday(yourDate) Calendar.current.isDateInTomorrow(yourDate) Additionally: Calendar.current.isDateInWeekend(yourDate) Note that for some countries weekend may be different than Saturday-Sunday, it depends on the calendar. You can also use autoupdatingCurrent instead of current calendar, which will track user updates. You use it the same way: Calendar.autoupdatingCurrent.isDateInToday(yourDate) Calendar is a type alias for the NSCalendar.

How to get time (hour, minute, second) in Swift 3 using NSDate?

In Swift 3.0 Apple removed ‘NS’ prefix and made everything simple. Below is the way to get hour, minute and second from ‘Date’ class (NSDate alternate) let date = Date() let calendar = Calendar.current let hour = calendar.component(.hour, from: date) let minutes = calendar.component(.minute, from: date) let seconds = calendar.component(.second, from: date) print(“hours = \(hour):\(minutes):\(seconds)”) … Read more

Find difference in seconds between NSDates as integer using Swift

Your attempt to calculate elapsedTime is incorrect. In Swift 3, it would be: let elapsed = Date().timeIntervalSince(timeAtPress) Note the () after the Date reference. The Date() instantiates a new date object, and then timeIntervalSince returns the time difference between that and timeAtPress. That will return a floating point value (technically, a TimeInterval). If you want … Read more

How do I get the day of the week with Foundation?

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateFormat:@”EEEE”]; NSLog(@”%@”, [dateFormatter stringFromDate:[NSDate date]]); outputs current day of week as a string in locale dependent on current regional settings. To get just a week day number you must use NSCalendar class: NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:[NSDate date]]; int … Read more

Get day of week using NSDate

Swift 3 & 4 Retrieving the day of the week’s number is dramatically simplified in Swift 3 because DateComponents is no longer optional. Here it is as an extension: extension Date { func dayNumberOfWeek() -> Int? { return Calendar.current.dateComponents([.weekday], from: self).weekday } } // returns an integer from 1 – 7, with 1 being Sunday … Read more

How do I get hour and minutes from NSDate?

Use an NSDateFormatter to convert string1 into an NSDate, then get the required NSDateComponents: Obj-C: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@”<your date format goes here”]; NSDate *date = [dateFormatter dateFromString:string1]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date]; NSInteger hour = [components hour]; NSInteger minute = [components minute]; … Read more

How can I calculate the difference between two dates?

NSDate *date1 = [NSDate dateWithString:@”2010-01-01 00:00:00 +0000″]; NSDate *date2 = [NSDate dateWithString:@”2010-02-03 00:00:00 +0000″]; NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1]; int numberOfDays = secondsBetween / 86400; NSLog(@”There are %d days in between the two dates.”, numberOfDays); EDIT: Remember, NSDate objects represent exact moments of time, they do not have any associated time-zone information. When you convert … Read more

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