Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let today = Date()
- let datePredicate = NSPredicate(format: "date == %@", today)
- // Get the current calendar with local time zone
- var calendar = Calendar.current
- calendar.timeZone = NSTimeZone.local
- // Get today's beginning & end
- let dateFrom = calendar.startOfDay(for: Date()) // eg. 2016-10-10 00:00:00
- let components = calendar.dateComponents([.year, .month, .day, .hour, .minute],from: dateFrom)
- components.day! += 1
- let dateTo = calendar.date(from: components)! // eg. 2016-10-11 00:00:00
- // Note: Times are printed in UTC. Depending on where you live it won't print 00:00:00 but it will work with UTC times which can be converted to local time
- // Set predicate as date being today's date
- let datePredicate = NSPredicate(format: "(%@ <= date) AND (date < %@)", argumentArray: [dateFrom, dateTo])
- fetchRequest.predicate = datePredicate
- let dateFrom = calendar.startOfDay(for: Date()) // eg. 2016-10-10 00:00:00
- let dateTo = calendar.date(byAdding: .day, value: 1, to: dateFrom)
Add Comment
Please, Sign In to add comment