Krutarth Patel Krutarth Patel - 7 months ago 28
iOS Question

how to get start and end time of today's date in ios?

I am getting current date and time by using this code

let today: NSDate = NSDate()
let dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.timeStyle = NSDateFormatterStyle.MediumStyle
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
dateFormatter.timeZone = NSTimeZone(abbreviation: "SGT");

but i want to get start time and end time of today’s date

example :
12-09-2016 00:00:00 AND 12-09-2016 23:59:59

How to get start and end time of current date?


You can use startOfDayForDate to get today's midnight date and then get end time from that date.

let calendar = NSCalendar.currentCalendar()
calendar.timeZone = NSTimeZone(abbreviation: "UTC")!
let dateAtMidnight = calendar.startOfDayForDate(NSDate())
let dateAtEnd = dateAtMidnight.dateByAddingTimeInterval(60*60*24-1)