# @Query using Predicate and DateComponents (multidatecomponents)

**URL:** <https://forums.swift.org/t/query-using-predicate-and-datecomponents-multidatecomponents/79378>\
**Category:** Using Swift\
**Tags:** swiftdata\
**Created:** [April 19, 2025, 10:16am UTC](https://forums.swift.org/t/query-using-predicate-and-datecomponents-multidatecomponents/79378 "2025-04-19T10:16:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![JELI](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@JELI](https://forums.swift.org/u/JELI)\
**Post date:** [April 19, 2025, 10:16am UTC](https://forums.swift.org/t/query-using-predicate-and-datecomponents-multidatecomponents/79378/1 "2025-04-19T10:16:34Z")

</div>

I am trying to build a CRUD app using a multidatepicker.  
The Apple documentation for multidatepicker uses  
`Set<DateComponents> = []`  
The problem is how to retrieve records for the current date.  
The debugger has the folllowing error;

> Query encountered an error: Unsupported Predicate: Captured/constant values of type DateComponents' are not supported

Here is my syntax for the @Query. I have a class called Planner and a property called dates (multidatepicker) and a 2nd property called period.

> @Query var planner: [Planner]
> 
> ```
> init() {
> let dateComp = DateComponents(calendar: Calendar.current, year: 2025, month: 4, day: 19)
>     
> _planner = Query(
> filter: #Predicate { $0.dates.contains(dateComp) },
> sort: \Planner.period
> )
> }
> 
> ```

The code compiles but there is a query error and I can't find any documentation or examples of the correct syntax.

Any help is greatly appreciated!

---

<div class="post-metadata">

**Author:** ![carlynorama](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carlynorama/32/27072_2.png) [@carlynorama](https://forums.swift.org/u/carlynorama)\
**Post date:** [April 21, 2025, 2:14am UTC](https://forums.swift.org/t/query-using-predicate-and-datecomponents-multidatecomponents/79378/2 "2025-04-21T02:14:42Z")

</div>

Hi JELI, I found your post just today while looking for a solution to a similar problem. There is likely an easier way than what I ended up doing, and I'd like to know it too! In the meantime I pulled dates from Calendar components and used that in the predicate instead of Calendar references:

### In my type:

```swift
    static var forwardSort = SortDescriptor(\SavedItem.timeStamp, order: .forward)
    
    //thingMemory.items.filter({ Calendar.current.isDate($0.timestamp, inSameDayAs:date) }).sorted(by: { $0.timestamp > $1.timestamp })
    static func makeDateFilter(thisDate:Date) -> FetchDescriptor<SavedItem> {
    
        let calendar = Calendar.current
        let dateFrom = calendar.startOfDay(for: thisDate)
        
        //TODO: Fix the !!!
        let dateTo = calendar.date(byAdding: .day, value: 1, to: dateFrom)!
        
        let datePredicate = #Predicate<SavedItem> { item in
            item.timeStamp > dateFrom && item.timeStamp < dateTo
        }
//
        var descriptor = FetchDescriptor<SavedItem>(sortBy: [forwardSort])
        descriptor.predicate = datePredicate
        
       return descriptor
    }

```

### In My View

```swift
    @State var selectedDate:Date
    
    @Query private var items: [SavedItem]
    init(date:Date) {
        self.selectedDate = date
        _items = Query(SavedItem.makeDateFilter(thisDate: selectedDate))
    }

```

The links I used:

- [https://stackoverflow.com/questions/76526517/swiftdata-query-with-dynamic-properties-in-a-view](https://stackoverflow.com/questions/76526517/swiftdata-query-with-dynamic-properties-in-a-view)
- [https://stackoverflow.com/questions/40312105/core-data-predicate-filter-by-todays-date?answertab=modifieddesc#tab-top](https://stackoverflow.com/questions/40312105/core-data-predicate-filter-by-todays-date?answertab=modifieddesc#tab-top)
- [Storing our data with SwiftData - a free Hacking with iOS: SwiftUI Edition tutorial](https://www.hackingwithswift.com/books/ios-swiftui/storing-our-data-with-swiftdata)
- [How to sort SwiftData queries using key paths or SortDescriptor - a free SwiftData by Example tutorial](https://www.hackingwithswift.com/quick-start/swiftdata/how-to-sort-swiftdata-queries-using-key-paths-or-sortdescriptor)
