Get Beginning of Today in SwiftUI

My question is how to take this code:

Code Block
var now = Date()


And get the beginning of that date. Let's say that Date() return March 17, 2021, 9 pm: 47 : 32 . How would you take that and get March 17, 2021, 12 am : 00 : 00?

Also, my aim is to get code that works in SwiftUI because every other online forum I searched had out dated results for UIkit or Swift 3.

Thank you,
Rorocoder
this maybe of use:

Code Block
let now = Date()
let startOfDay = Calendar.current.startOfDay(for: now)
print("startOfDay: \(startOfDay)")

You can format the date, like:

Code Block
let formatter = DateFormatter()
formatter.dateStyle = .full
formatter.timeStyle = .medium
print(" now: \(formatter.string(from: now)) \n startOfDay: \(formatter.string(from: startOfDay))")

Thank you, but when I try and put startOfDay into a Core Data Fetch Request as a Predicate format I get an error saying something about can't use initializers. Do you know how to solve this. Thank you so much for your help already.
glad it helped. As for the Core Data Fetch Request question, show us the code you are using.
This shouldn't be tagged with "SwiftUI". The Date type is in Foundation and the answer will be the same regardless of whether the app is using SwiftUI, AppKit, UIKit, or the command line.
Get Beginning of Today in SwiftUI
 
 
Q