With Swift 3, you can choose one of the following four templates to solve your problem.
# 1. Using Calendar startOfDay(for:)
startOfDay(for:) has the following declaration:
func startOfDay(for date: Date) -> Date
Returns the first moment of the given Date , as Date .
The following is an example of a playground code:
import Foundation let date = Date() // Get new date let calendar = Calendar.current let newDate = calendar.startOfDay(for: date) // Format dates let dateFormatter = DateFormatter() dateFormatter.locale = Locale(identifier: "en_UK") dateFormatter.dateStyle = .short dateFormatter.timeStyle = .long let formattedDate = dateFormatter.string(from: date) let formattedNewDate = dateFormatter.string(from: newDate) // Print formatted dates print(formattedDate) // Prints: 30/03/2017, 15:14:41 CEST print(formattedNewDate) // Prints: 30/03/2017, 00:00:00 CEST
# 2. Using Calendar date(bySettingHour:minute:second:of:matchingPolicy:repeatedTimePolicy:direction:)
date(bySettingHour:minute:second:of:matchingPolicy:repeatedTimePolicy:direction:) has the following declaration:
func date(bySettingHour hour: Int, minute: Int, second: Int, of date: Date, matchingPolicy: Calendar.MatchingPolicy = default, repeatedTimePolicy: Calendar.RepeatedTimePolicy = default, direction: Calendar.SearchDirection = default) -> Date?
Returns a new Date representing the date calculated by setting the hour, minute, and second to the specified time with the specified Date .
The following is an example of a playground code:
import Foundation let date = Date() // Get new date let calendar = Calendar.current let newDate = calendar.date(bySettingHour: 0, minute: 0, second: 0, of: date) // Format dates let dateFormatter = DateFormatter() dateFormatter.locale = Locale(identifier: "en_UK") dateFormatter.dateStyle = .short dateFormatter.timeStyle = .long let formattedDate = dateFormatter.string(from: date) let formattedNewDate = dateFormatter.string(from: newDate!) // Print formatted dates print(formattedDate) // Prints: 30/03/2017, 15:14:41 CEST print(formattedNewDate) // Prints: 30/03/2017, 00:00:00 CEST
# 3. Using the Calendar methods dateComponents(_:from:) and date(from:)
dateComponents(_:from:) has the following declaration:
func dateComponents(_ components: Set<Calendar.Component>, from date: Date) -> DateComponents
Returns all components of a date date using the calendar time zone.
date(from:) has the following declaration:
func date(from components: DateComponents) -> Date?
Returns the date created from the specified components.
The following is an example code on a playground:
import Foundation let date = Date() // Get new date let calendar = Calendar.current let components = calendar.dateComponents([.day, .month, .year], from: date) let newDate = calendar.date(from: components) // Format dates let dateFormatter = DateFormatter() dateFormatter.locale = Locale(identifier: "en_UK") dateFormatter.dateStyle = .short dateFormatter.timeStyle = .long let formattedDate = dateFormatter.string(from: date) let formattedNewDate = dateFormatter.string(from: newDate!) // Print formatted dates print(formattedDate) // Prints: 30/03/2017, 15:14:41 CEST print(formattedNewDate) // Prints: 30/03/2017, 00:00:00 CEST
# 4. Using NSCalendar range(of:start:interval:for:)
range(of:start:interval:for:) has the following declaration:
func range(of unit: NSCalendar.Unit, start datep: AutoreleasingUnsafeMutablePointer<NSDate?>?, interval tip: UnsafeMutablePointer<TimeInterval>?, for date: Date) -> Bool
Returns by reference the start time and duration of this calendar block, which contains the specified date.
The following is an example of a playground code:
import Foundation let date = Date() // Get new date let calendar = Calendar.current as NSCalendar var newDate: NSDate? calendar.range(of: .day, start: &newDate, interval: nil, for: date) // Format dates let dateFormatter = DateFormatter() dateFormatter.locale = Locale(identifier: "en_UK") dateFormatter.dateStyle = .short dateFormatter.timeStyle = .long let formattedDate = dateFormatter.string(from: date) let formattedNewDate = dateFormatter.string(from: newDate as! Date) // Print formatted dates print(formattedDate) // Prints: 30/03/2017, 15:14:41 CEST print(formattedNewDate) // Prints: 30/03/2017, 00:00:00 CEST