2016-10-01 13 views
6

Tôi đang cố gắng để có được tháng trước và tháng tiếp theo bằng cách nhấp vào nút nhưng điều này đã thay đổi năm của tôi, không phải tháng.Cách lấy tháng trước và tháng tiếp theo theo lịch

Tôi đang thử sử dụng this link nhưng trong Mục tiêu-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) { 

    } 

Vậy làm cách nào để thực hiện nó cho cả tháng trước và tháng tiếp theo?

Trả lời

16

Hãy thử như thế này.

Để có ngày tháng tiếp theo.

Swift 3

let nextMonth = Calendar.current.date(byAdding: .month, value: 1, to: Date()) 

Swift 2.3 hoặc thấp hơn

let nextMonth = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: 1, toDate: NSDate(), options: []) 

Để có được ngày tháng trước.

Swift 3

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

Swift 2.3 hoặc thấp hơn

let previousMonth = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: -1, toDate: NSDate(), options: []) 

Lưu ý: Để có được ngày tháng tiếp theo và trước tôi đã sử dụng ngày todays, bạn có thể sử dụng ngày mà bạn muốn ngày tháng tiếp theo và trước đó, chỉ cần thay đổi NSDate() bằngcủa bạnđối tượng.

Chỉnh sửa: Để tiếp tục di chuyển các tháng tiếp theo và trước đó, bạn cần lưu trữ một đối tượng ngày hiện tạiNgày và khi bạn cố gắng sử dụng đối tượng ngày đó và cập nhật ngày đó.

Cho tháng tiếp theo.

let currentDate = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: 1, toDate: currentDate, options: []) 

Đối với tháng trước.

let currentDate = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: -1, toDate: currentDate, options: []) 

Bạn cũng có thể sử dụng phần mở rộng đó sẽ làm cho điều dễ dàng.

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 hoặc thấp hơn

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: []) 
    } 
} 

Bây giờ chỉ cần có được ngày từ currentDate.

currentDate = currentDate.getNextMonth() 
currentDate = currentDate.getPreviousMonth() 
+0

Phiên bản Swift 3? '' dateByAddingUnit'' dường như đã được thay thế bởi: '' Calendar.current.date (byAdding: .day, value: days, to: date)! '' Mặc dù, tôi không thể tìm thấy các tùy chọn nữa? – eonist

+0

Swift 3.0.2, NSCalendar.current.date (byAdding: .month, value: 1, to: date) – KTPATEL

+0

** Cách xử lý phạm vi trong swift3 **. 'calendar.range (.Month, startDate: & startOfMonth, interval: & lengthOfMonth, forDate: date)'. ** Lỗi là> Thêm đối số 'forDate' trong cuộc gọi ** – iDeveloper

2
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 
+0

Làm việc hoàn hảo cho nextMonth. những gì về tháng trước – iDeveloper

+0

@ iNhà phát triển chỉ thay đổi 'giá trị' trong hàm thành' -1' –

+0

nhờ công việc hoàn hảo – iDeveloper

0

tôi khuyên bạn nên sử dụng SwiftDate thư viện:

let date = Date() 
let nextMonthDate = date.nextMonth(at: .auto) 
let prevMonthDate = date.prevMonth(at: .auto) 

Bạn có thể sử dụng .start.end thay vì .auto. Xem thông tin về nextMonth ví dụ:

khi .auto ngày đánh giá được tính bằng cách thêm một tháng vào ngày hiện tại.

Nếu bạn vượt qua .start ngày kết thúc là ngày đầu tiên của tháng tiếp theo (lúc 00:00:00).

Nếu bạn vượt qua .end ngày kết quả là ngày cuối cùng của tháng tiếp theo (lúc 23:59:59).

Thư viện cũng chứa prevWeek/nextWeek và nhiều phương pháp hữu ích khác.

Các vấn đề liên quan