问题描述
我试图通过单击按钮获取上个月和下个月,但这改变了我的年份,而不是月份.
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和Swift 4
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: [])
获取上个月的日期.
快捷键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
对象.
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: [])
您还可以使用扩展程序,使事情变得简单.
快捷键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()
这篇关于如何在日历中获取上个月和下个月-Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!