First my date is in string format as : "06-04-2017 06:28:17 PM"
And then i converted to Date() using:
let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy hh:mm:ss a"
formatter.timeZone = TimeZone(abbreviation: "GMT")
let startdate = formatter.date(from: sdate!)
print(startdate)
This is the output Date from above:
startdate = 2017-04-06 18:28:17 +0000
But when I try to set my minute to value 0 with the following code . I get my day changed .
let calender = Calendar.current
calendar.timeZone = TimeZone(abbreviation: "UTC")!
let date = calender.date(bySettingHour: Int(hour)!, minute: 00, second: 00, of: startdate!)
print(date)
OUTPUT: 2017-04-07 12:15:00 +0000
My timezone is +5:45 , after I am setting that timezone . I am getting the same bug .
Any help . Highly appreciated.
With the following code I get the date same :
let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy hh:mm:ss a"
formatter.timeZone = TimeZone(abbreviation: "GMT")
let startdate = formatter.date(from: sdate!)
print(startdate)
**But I can't set it's minute to 0 here. **