ios - 获取上个月的天数

标签 ios swift xcode date

如何获取前一个月的总天数?

i.e = Current Month is June, So I want May month Total number of days.(Output = 31)

这是我尝试过的方法,但没有用。

让 PrevDate = Calendar.current.date(byAdding: .month, value: -1, to: Date())

最佳答案

这些是用法:

let calendar = Calendar.current
let date1 = calendar.date(byAdding: .month, value: -1, to: Date())! // -1 mean previous month

let range = calendar.range(of: .day, in: .month, for: date1)!
let numDays = range.count
print(numDays)

输出:

31

关于ios - 获取上个月的天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56579272/

相关文章:

android - 更改移动应用程序的主屏幕位置

arrays - 如何从 Swift 3 中的结构数组生成 JSON?

ios - 更新 UIImageView 会导致内存泄漏

ios - iPhone 应用程序 : use max screen size when run on iPad

iOS SDK : set background color of button by its tag number

ios - 与 "Type ' ViewController' 相关的编译和构建错误不符合协议(protocol) 'UITableViewDataSource' ”

ios - 如何处理 Tesseract OCR 4.0 iOS Swift 中的 fatal error

javascript - 将 JSON 数组解析为 Swift 数组

ios - 如何去除IOS App swift上top Bar的透明度

ios - XCode 没有启用纹理图集生成的选项