ios 如何在日历中获取上个月和下个月 - Swift
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39804669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get previous and next month in calendar-Swift
提问by iDeveloper
I am trying to get previous and next month by click on button but this has changed my year, not month.
我试图通过点击按钮获得上个月和下个月,但这改变了我的年份,而不是月份。
I am trying it from using this linkbut it is in Objective-C.
我正在尝试使用此链接,但它在 Objective-C 中。
func firstDateOfMonth(){
let components = calendar.components([.Year, .Month], fromDate: date)
let startOfMonth = calendar.dateFromComponents(components)!
print(dateFormatatter.stringFromDate(startOfMonth)) // 2015-11-01
}
func lastDateOfMonth(){
let components = calendar.components([.Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: date)
let month = components.month
let year = components.year
let startOfMonth1 = ("01-\(month)-\(year)")
var startOfMonth : NSDate?
var lengthOfMonth : NSTimeInterval = 0
calendar.rangeOfUnit(.Month, startDate: &startOfMonth, interval: &lengthOfMonth, forDate: date)
let endOfMonth = startOfMonth!.dateByAddingTimeInterval(lengthOfMonth - 1)
print(dateFormatatter.stringFromDate(endOfMonth))
}
func monthCalculation(){
firstDateOfMonth()
lastDateOfMonth()
}
@IBAction func previousMonthBtnAction(sender: AnyObject) {
let componen = calendar.components([.Day , .Month , .Year], fromDate: date)
componen.month = -1
self.date = calendar.dateFromComponents(componen)!
print(self.date)
}
@IBAction func nextMonthBtnAction(sender: AnyObject) {
}
So how to do it for both previous and next month?
那么上个月和下个月怎么做呢?
回答by Nirav D
Try like this.
像这样尝试。
To get date of next month.
获取下个月的日期。
Swift 3 and Swift 4
斯威夫特 3 和斯威夫特 4
let nextMonth = Calendar.current.date(byAdding: .month, value: 1, to: Date())
Swift 2.3 or lower
Swift 2.3 或更低版本
let nextMonth = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: 1, toDate: NSDate(), options: [])
To get date of previous month.
获取上个月的日期。
Swift 3
斯威夫特 3
let previousMonth = Calendar.current.date(byAdding: .month, value: -1, to: Date())
Swift 2.3 or lower
Swift 2.3 或更低版本
let previousMonth = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: -1, toDate: NSDate(), options: [])
Note:To get date of next and previous month I have used todays date, you can use date from which you want next and previous month date, just change NSDate()
with your NSDate
object.
注意:要获取下个月和上个月的日期,我使用了今天的日期,您可以使用您想要下个月和上个月日期的日期,只需更改NSDate()
您的NSDate
对象即可。
Edit:For continues traveling of next and previous months you need to store one date object currentDate and when you try to get next and previous month use that date object and update it.
编辑:对于下个月和上个月的继续旅行,您需要存储一个日期对象 currentDate ,当您尝试获取下个月和上个月时,请使用该日期对象并更新它。
For next month.
为下个月。
let currentDate = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: 1, toDate: currentDate, options: [])
For previous month.
对于上个月。
let currentDate = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: -1, toDate: currentDate, options: [])
You can also use extension that will make thing easy.
您还可以使用扩展程序使事情变得简单。
Swift 3
斯威夫特 3
extension Date {
func getNextMonth() -> Date? {
return Calendar.current.date(byAdding: .month, value: 1, to: self)
}
func getPreviousMonth() -> Date? {
return Calendar.current.date(byAdding: .month, value: -1, to: self)
}
}
Swift 2.3 or lower
Swift 2.3 或更低版本
extension NSDate {
func getNextMonth() -> NSDate?{
return NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: 1, toDate: self, options: [])
}
func getPreviousMonth() -> NSDate?{
return NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: -1, toDate: self, options: [])
}
}
Now just get date from currentDate.
现在只需从 currentDate 获取日期。
currentDate = currentDate.getNextMonth()
currentDate = currentDate.getPreviousMonth()
回答by Tikhonov Alexander
var currentDate = NSDate()
func nextMonth() -> NSDate {
let nextDate = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: 1, toDate: currentDate, options: [])!
currentDate = nextDate
return nextDate
}
print(nextMonth()) // 2016-11-01 20:11:15 +0000
print(nextMonth()) // 2016-12-01 21:11:15 +0000
print(nextMonth()) // 2017-01-01 21:11:15 +0000
回答by Ariven Nadar
Swift 5
斯威夫特 5
let date = Date()
let calendar = Calendar.current
let currentYear = Calendar.current.component(.year, from: Date())
let previousYear = currentYear + 1
let nextYear = currentYear + 1
print("\(previousYear)->\(currentYear)->\(nextYear)")
Output:
2018->2019->2020
Happy Coding :)
快乐编码:)
回答by algrid
I'd recommend using SwiftDatelibrary:
我建议使用SwiftDate库:
let date = Date()
let nextMonthDate = date.nextMonth(at: .auto)
let prevMonthDate = date.prevMonth(at: .auto)
You can use .start
and .end
instead of .auto
. Look at the info about nextMonth
for example:
您可以使用.start
和.end
代替.auto
。查看有关nextMonth
例如的信息:
when
.auto
evaluated date is calculated by adding one month to the current date.If you pass
.start
result date is the first day of the next month (at 00:00:00).If you pass
.end
result date is the last day of the next month (at 23:59:59).
当
.auto
评估日期是通过在当前日期上加一个月来计算的。如果您通过
.start
结果日期是下个月的第一天(00:00:00)。如果您通过
.end
结果日期是下个月的最后一天(23:59:59)。
The library also contains prevWeek
/nextWeek
and many other useful methods.
该库还包含prevWeek
/nextWeek
和许多其他有用的方法。