小编Rom*_* BM的帖子

calendar.date(bySetting: .day, value: 1, of: Date()) wrongly always adding one month

I want to get two dates, the first and last day of current month. Pretty simple. But date(bySetting: Calendar.Component, value: Int, of: Date) are not working properly.

Look this piece of code:

When Date() prints 2019-04-18 14:11:36 UTC

let calendar = Calendar.current

let firstDayDate = calendar.date(bySetting: .day, value: 1, to: Date())! //2019-05-01 03:00:00 UTC  

let firsDayNextMonth = calendar.date(byAdding: .month, value: 1, to: firstDayDate)! //2019-06-01 03:00:00 UTC   
let lastDayMonthDate = calendar.date(bySetting: .day, value: -1, to: firsDayNextMonth)! //2019-05-31 03:00:00 UTC   
Run Code Online (Sandbox Code Playgroud)

Look at …

calendar swift

5
推荐指数
1
解决办法
307
查看次数

标签 统计

calendar ×1

swift ×1