Reputation:
I'm going to develop the piece of code to query all the records created yesterday. Well I know it's something very trial. Nevertheless it's one million question for me right now because I'm novice in programming. Below the SWIFT code that doesn't work properly though. For backend I use Parse.com.
var yesterday = NSDate() - 1.day.ago // 1.day.ago isn't valid but what I need.
var query = PFQuery(className:"score") // table name is score
query.whereKey("createdAt", equalTo:"yesterday")
Help please! As always sooner you help higher your chances to win the Tesla Model S Lottery! :)
Upvotes: 0
Views: 1944
Reputation: 236370
import UIKit
extension NSDate {
func xDays(x:Int) -> NSDate {
return NSCalendar.currentCalendar().dateByAddingUnit(.Day, value: x, toDate: self, options: [])!
}
}
let today = NSDate()
let yesterday = NSDate().xDays(-1)
let dayBeforeYesterday = NSDate().xDays(-2)
Upvotes: 1
Reputation: 511
Simply try below code. It works fine in iOS7 and iOS8
let today = NSDate()
let tomorrow = today.dateByAddingTimeInterval(24 * 60 * 60)
let yesterday = today.dateByAddingTimeInterval(-24 * 60 * 60)
Upvotes: 0
Reputation: 37300
You can use a pretty cool new attribute of NSCalendar
called dateByAddingUnit
to easily get yesterday's date:
let today = NSDate()
let yesterday = NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitDay,
value: -1,
toDate: today,
options: NSCalendarOptions(0))
Where you set .CalendarUnitDay
as the calendar unit you wish to add/subtract by and the value of -1 to indicate the change you want for that calendar unit.
But the problem with your query is that it specifically requests that a PFObject
's "createdAt" date equals a specific time, not that the PFObject
was created at anytime during that day. So I suggest you create two constraints in your query -- a greater than and a less than between midnight (the start of the day) yesterday and midnight (the start of the day) today so that the query will return all the results in between.
Update:
To get the start of yesterday and today, i.e. midnight, (converted to GMT to match the dates in the parse database, though that line can be removed if that's not your intent) here's what you can do:
var today = NSDate()
let calendar = NSCalendar.autoupdatingCurrentCalendar()
calendar.timeZone = NSTimeZone(forSecondsFromGMT: 0)
let preservedComponents = NSCalendarUnit.YearCalendarUnit | NSCalendarUnit.MonthCalendarUnit | NSCalendarUnit.DayCalendarUnit
today = calendar.dateFromComponents(calendar.components(preservedComponents, fromDate: today))!
var yesterday = NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitDay, value: -1, toDate: today, options: NSCalendarOptions(0))
Then to perform your query:
var query = PFQuery(className:"score") // table name is score
query.whereKey("createdAt", greaterThanOrEqualTo: yesterday)
query.whereKey("createdAt", lessThan: today)
Upvotes: 3
Reputation: 916
Try this:
var yesterday = NSDate.date() - 1.days
var query = PFQuery(className:"score") // table name is score
query.whereKey("createdAt", equalTo:yesterday)
Pay attention at equalTo:yesterday
without quotes
Upvotes: 0