Guest User

Untitled

a guest
Mar 22nd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. let today = Date()
  2. let datePredicate = NSPredicate(format: "date == %@", today)
  3.  
  4. // Get the current calendar with local time zone
  5. var calendar = Calendar.current
  6. calendar.timeZone = NSTimeZone.local
  7.  
  8. // Get today's beginning & end
  9. let dateFrom = calendar.startOfDay(for: Date()) // eg. 2016-10-10 00:00:00
  10. let components = calendar.dateComponents([.year, .month, .day, .hour, .minute],from: dateFrom)
  11. components.day! += 1
  12. let dateTo = calendar.date(from: components)! // eg. 2016-10-11 00:00:00
  13. // 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
  14.  
  15. // Set predicate as date being today's date
  16. let datePredicate = NSPredicate(format: "(%@ <= date) AND (date < %@)", argumentArray: [dateFrom, dateTo])
  17. fetchRequest.predicate = datePredicate
  18.  
  19. let dateFrom = calendar.startOfDay(for: Date()) // eg. 2016-10-10 00:00:00
  20. let dateTo = calendar.date(byAdding: .day, value: 1, to: dateFrom)
Add Comment
Please, Sign In to add comment