0

How do I query a date range in Swift code data IOS? If I store attributes: date, breakfast meals, lunch meals. How can I query what meals I ate this week?

Sandy
  • 149
  • 1
  • 7

2 Answers2

0

I was able to get the current week using the following code:

var today = Date()

//finds date for Sunday start of week

var todayweekdaycomp = Calendar.component(.weekday, from: Date())
let startOfWeekSunday = Calendar.current.date(byAdding: .day, value: -(todayweekdaycomp - 1), to: today)!

//finds date for Saturday end of week

let endOfWeekSatvalue = 7 - todayweekdaycomp
let endofWeekSat = Calendar.current.date(byAdding: .day, value: endOfWeekSatvalue, to: today)!


//array of dates this week

var arr: [Date] = []

for n in 1...7 {
    arr.append(Calendar.current.date(byAdding: .day, value: n, to: startOfWeekSunday)!)
}
undefined
  • 1,019
  • 12
  • 24
Sandy
  • 149
  • 1
  • 7
-1

Defining the begin date :

First you have to define a begin date. This will help us retrieve all the meals from that date .

If you want to get it from 7 days ago define it like that :

let calendar = Calendar.current

let beginDate = calendar.date(byAdding: .day, value: -7, to: Date())!

If you want to get it since the beginning of the week you first need to add this extension :

 extension Date {
    func startOfWeek(using calendar: Calendar = Calendar(identifier: .gregorian)) -> Date {
        calendar.dateComponents([.calendar, .yearForWeekOfYear, .weekOfYear], from: self).date!
    }
}

Then you can define your begin date like so :

let beginDate = Date().startOfWeek()

Creating the request :

// Creating the predicate. 
let predicate = NSPredicate(format: "%K >= %@", #keyPath(YourData.date) ,beginDate as NSDate)
let request: NSFetchRequest<YourData> = YourData.fetchRequest()
// Adding a predicate to your request.   
request.predicate = predicate

Note that you need to replace "YourData" by the name of the class of your meal data object. Once you have your request ready you can fetch it as usual.

Titouan
  • 531
  • 4
  • 13
  • This code looks familiar to me. :) https://stackoverflow.com/a/65414641/2303865 Btw `Date() - TimeInterval(3600 * 24 * 7)` is wrong. You should use calendar methods to subtract a week from your date. Not all dates have 24 hours. And If OP is trying to get the current week it doesn't make any sense subtracting a week from now. And you are simply discarding the date you subtracted the "week" from. – Leo Dabus Dec 22 '20 at 20:09
  • 1
    @LeoDabus I guess you meant https://stackoverflow.com/a/35687720/9223839 – Joakim Danielson Dec 22 '20 at 20:48
  • @JoakimDanielson yes pasted the wrong link – Leo Dabus Dec 22 '20 at 20:50
  • Yeah Leo I got it from this answer. – Titouan Dec 22 '20 at 20:59
  • And I was not sure of what the OP Is looking for so I just put the two different methods. And why is there a dislkie on my answer ? I mean it answer the question doesn't it ? – Titouan Dec 22 '20 at 21:00
  • Not my downvote but as I said `Date() - TimeInterval(3600 * 24 * 7)` is wrong – Leo Dabus Dec 22 '20 at 21:28
  • I don't really understand what you mean by "not all dates have 24 hours". Can you develop on an example please. – Titouan Dec 22 '20 at 22:35
  • If the week is the same as a daylight savings transition a day might have 23, 24 or 25 hours. https://en.wikipedia.org/wiki/Daylight_saving_time – Leo Dabus Dec 23 '20 at 00:18