Nsdate of Yesterday

get NSDate today, yesterday, this Week, last Week, this Month, last Month... variables

Might be a better way to write this but here what i came up on Ben's NSCalendar suggestion and working from there to NSDateComponents

NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ) fromDate:[[NSDate alloc] init]];

[components setHour:-[components hour]];
[components setMinute:-[components minute]];
[components setSecond:-[components second]];
NSDate *today = [cal dateByAddingComponents:components toDate:[[NSDate alloc] init] options:0]; //This variable should now be pointing at a date object that is the start of today (midnight);

[components setHour:-24];
[components setMinute:0];
[components setSecond:0];
NSDate *yesterday = [cal dateByAddingComponents:components toDate: today options:0];

components = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[[NSDate alloc] init]];

[components setDay:([components day] - ([components weekday] - 1))];
NSDate *thisWeek = [cal dateFromComponents:components];

[components setDay:([components day] - 7)];
NSDate *lastWeek = [cal dateFromComponents:components];

[components setDay:([components day] - ([components day] -1))];
NSDate *thisMonth = [cal dateFromComponents:components];

[components setMonth:([components month] - 1)];
NSDate *lastMonth = [cal dateFromComponents:components];

NSLog(@"today=%@",today);
NSLog(@"yesterday=%@",yesterday);
NSLog(@"thisWeek=%@",thisWeek);
NSLog(@"lastWeek=%@",lastWeek);
NSLog(@"thisMonth=%@",thisMonth);
NSLog(@"lastMonth=%@",lastMonth);

Obtain yesterday with NSDate in objective-c

NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow: -(60.0f*60.0f*24.0f)];

Edit: This solution isn't correct. See Aadhiras answer for the correct solution.

Swift - How to check if an NSDate is yesterday compare to current time?

As of iOS 8.0, you can use -[NSCalendar isDateInYesterday:], like this:

let calendar = NSCalendar.autoupdatingCurrentCalendar()

let someDate: NSDate = some date...
if calendar.isDateInYesterday(someDate) {
// It was yesterday...
}

If you'll be doing this a lot, you should create the calendar once and keep it in an instance variable, because creating the calendar object is not trivial.

Find out if an NSDate is today, yesterday, tomorrow

Check our Erica Sadun's great NSDate extension class: http://github.com/erica/NSDate-Extensions

There are lots of date comparisons, among them exactly what you need :)

Getting yesterday's NSDate start and end

Some extensions I drag around. Plop the following in a play and enjoy:

import UIKit

extension NSDate {
var yesterday:NSDate {
let calendar = NSCalendar.currentCalendar()
return calendar.startOfDayForDate(calendar.dateByAddingUnit(.Day, value: -1, toDate: self, options: NSCalendarOptions.WrapComponents)!)
}

var tomorrow:NSDate {
let calendar = NSCalendar.currentCalendar()
return calendar.startOfDayForDate(calendar.dateByAddingUnit(.Day, value: 1, toDate: self, options: NSCalendarOptions.WrapComponents)!)
}
}


let now = NSDate()
now.yesterday
now.tomorrow
now.yesterday.yesterday.yesterday.yesterday
now.tomorrow.yesterday

swift 3 how to get date for tomorrow and yesterday ( take care special case ) new month or new year

You should use Calendar method date(byAdding component:) to do your calendrical calculations using noon time. Doing so you don't need to worry about those special cases:

Swift 3 or Later

extension Date {
static var yesterday: Date { return Date().dayBefore }
static var tomorrow: Date { return Date().dayAfter }
var dayBefore: Date {
return Calendar.current.date(byAdding: .day, value: -1, to: noon)!
}
var dayAfter: Date {
return Calendar.current.date(byAdding: .day, value: 1, to: noon)!
}
var noon: Date {
return Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: self)!
}
var month: Int {
return Calendar.current.component(.month, from: self)
}
var isLastDayOfMonth: Bool {
return dayAfter.month != month
}
}

Date.yesterday    // "Oct 28, 2018 at 12:00 PM"
Date() // "Oct 29, 2018 at 11:01 AM"
Date.tomorrow // "Oct 30, 2018 at 12:00 PM"

Date.tomorrow.month // 10
Date().isLastDayOfMonth // false


Related Topics



Leave a reply



Submit