问题描述
我试图通过点击按钮获得上个月和下个月,但这改变了我的年份,而不是月份。
I am trying to get previous and next month by click on button but this has changed my year, not month.
我正在尝试使用但它位于Objective-C。
I am trying it from using this link but it is in 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?
推荐答案
试试这样。
获取下个月的日期。
Swift 3
let nextMonth = Calendar.current.date(byAdding: .month, value: 1, to: Date())
Swift 2.3或更低
let nextMonth = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: 1, toDate: NSDate(), options: [])
获取上个月的日期。
Swift 3
let previousMonth = Calendar.current.date(byAdding: .month, value: -1, to: Date())
Swift 2.3或更低
let previousMonth = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: -1, toDate: NSDate(), options: [])
注意:获取下一个和上一个的日期我已经使用了今天的日期,您可以使用您希望下一个月和上个月日期的日期,只需使用您的 NSDate更改
NSDate()
code> object。
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.
编辑:对于下个月和前几个月的继续旅行,您需要存储一个日期对象currentDate以及当您尝试让下一个月和上个月使用该日期对象并更新它。
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.
下个月。
let currentDate = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: 1, toDate: currentDate, options: [])
上个月。
let currentDate = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: -1, toDate: currentDate, options: [])
您还可以使用扩展功能。
Swift 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或更低
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: [])
}
}
现在只需从currentDate获取日期。
Now just get date from currentDate.
currentDate = currentDate.getNextMonth()
currentDate = currentDate.getPreviousMonth()
这篇关于如何在日历中获取上个月和下个月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!