How to get the start date and end date of the current month (Swift 3)

I am trying to get the start and end dates of the current month in a format dd/MM/yyyy. I tried using the extension as mentioned in this QUESTION . But it looks like this is not what I want (the format is different, and it gives me the last month of the last date and the current month of the last, but one date). Can someone help me.

Extension Class:

extension Date {
    func startOfMonth() -> Date? {
        let comp: DateComponents = Calendar.current.dateComponents([.year, .month, .hour], from: Calendar.current.startOfDay(for: self))
        return Calendar.current.date(from: comp)!
    }

    func endOfMonth() -> Date? {
        var comp: DateComponents = Calendar.current.dateComponents([.month, .day, .hour], from: Calendar.current.startOfDay(for: self))
        comp.month = 1
        comp.day = -1
        return Calendar.current.date(byAdding: comp, to: self.startOfMonth()!)
    }
}

My Struct:

struct Constants{

    // keys required for making a Login call (POST Method)
    struct LoginKeys {
       .....
    }

    struct RankingKeys {

        static let DateFrom = String(describing: Date().startOfMonth()) //giving me 2016-11-30 16:00:00 +0000 
        static let DateTo  = String(describing: Date().endOfMonth())
//2016-12-30 16:00:00 +0000
    }
}

Expected Result:

DateFrom  = "01/12/2016"
DateTo = "31/12/2016"
+5
source share
5 answers

You should write this simple code:

let dateFormatter = DateFormatter()
let date = Date()
dateFormatter.dateFormat = "dd-MM-yyyy"

The date of the beginning:

let comp: DateComponents = Calendar.current.dateComponents([.year, .month], from: date)
let startOfMonth = Calendar.current.date(from: comp)!
print(dateFormatter.string(from: startOfMonth))

For the end date:

var comps2 = DateComponents()
comps2.month = 1
comps2.day = -1
let endOfMonth = Calendar.current.date(byAdding: comps2, to: startOfMonth)
print(dateFormatter.string(from: endOfMonth!)) 
+11
source

, . , .

extension Calendar {

func dayOfWeek(_ date: Date) -> Int {
    var dayOfWeek = self.component(.weekday, from: date) + 1 - self.firstWeekday

    if dayOfWeek <= 0 {
        dayOfWeek += 7
    }

    return dayOfWeek
}

func startOfWeek(_ date: Date) -> Date {
    return self.date(byAdding: DateComponents(day: -self.dayOfWeek(date) + 1), to: date)!
}

func endOfWeek(_ date: Date) -> Date {
    return self.date(byAdding: DateComponents(day: 6), to: self.startOfWeek(date))!
}

func startOfMonth(_ date: Date) -> Date {
    return self.date(from: self.dateComponents([.year, .month], from: date))!
}

func endOfMonth(_ date: Date) -> Date {
    return self.date(byAdding: DateComponents(month: 1, day: -1), to: self.startOfMonth(date))!
}

func startOfQuarter(_ date: Date) -> Date {
    let quarter = (self.component(.month, from: date) - 1) / 3 + 1
    return self.date(from: DateComponents(year: self.component(.year, from: date), month: (quarter - 1) * 3 + 1))!
}

func endOfQuarter(_ date: Date) -> Date {
    return self.date(byAdding: DateComponents(month: 3, day: -1), to: self.startOfQuarter(date))!
}

func startOfYear(_ date: Date) -> Date {
    return self.date(from: self.dateComponents([.year], from: date))!
}

func endOfYear(_ date: Date) -> Date {
    return self.date(from: DateComponents(year: self.component(.year, from: date), month: 12, day: 31))!
}
}

let calendar: Calendar = Calendar.current
let startDate = calendar.startOfMonth(Date())
print("startDate :: \(startDate)")
+5

Swift 3 iOS 10 - Calendar dateInterval(of:for:):

guard let interval = calendar.dateInterval(of: .month, for: Date()) else { return }

Then use the date format to print the dates:

let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy"
let dateText = formatter.string(from: interval.start)
+2
source

This extension provides the expected result of your choice.

Here I return the date

extension NSDate {
    func startOfMonth() -> NSDate? {
        guard
            let cal: NSCalendar = NSCalendar.currentCalendar(),
            let comp: NSDateComponents = cal.components([.Year, .Month], fromDate: self) else { return nil }
        comp.to12pm()
        let dateformattor = NSDateFormatter()
        dateformattor.dateFormat = "yyyy-MM-dd"
        dateformattor.timeZone = NSTimeZone.localTimeZone()
        let dt2 = dateformattor.stringFromDate(cal.dateFromComponents(comp)!)
        print(dt2)

        dateformattor.dateFormat = "yyyy-MM-dd"
        dateformattor.timeZone = NSTimeZone.init(abbreviation: "UTC")

        return dateformattor.dateFromString(dt2)
    }

    func endOfMonth() -> NSDate? {
        guard
            let cal: NSCalendar = NSCalendar.currentCalendar(),
            let comp: NSDateComponents = NSDateComponents() else { return nil }
        comp.month = 1
        comp.day = -1
        comp.to12pm()
        let dateformattor = NSDateFormatter()
        dateformattor.dateFormat = "yyyy-MM-dd"
        dateformattor.timeZone = NSTimeZone.localTimeZone()
        let dt2 = dateformattor.stringFromDate(cal.dateByAddingComponents(comp, toDate: self.startOfMonth()!, options: [])!)

        dateformattor.dateFormat = "yyyy-MM-dd"
        dateformattor.timeZone = NSTimeZone.init(abbreviation: "UTC")

        return dateformattor.dateFromString(dt2)
    }
}
internal extension NSDateComponents {
    func to12pm() {
        self.hour = 12
        self.minute = 0
        self.second = 0
    }
}

** OUTPUT: - **

Start Date of Month :- 2016-12-01 00:00:00 +0000
End Date of Month :- 2016-12-31 00:00:00 +0000
0
source

For completeness, the API dateInterval(of:start:interval:for:) Calendarassigns a start date and interval (in seconds) monthfor the current parameters inout.

The date formatter takes into account the current time zone.

let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "dd/MM/yyyy"

var startDate = Date()
var interval = TimeInterval()
Calendar.current.dateInterval(of: .month, start: &startDate, interval: &interval, for: Date())
let endDate = Calendar.current.date(byAdding: .second, value: Int(interval) - 1, to: startDate)!

let fromDate = formatter.string(from: startDate)
let toDate = formatter.string(from: endDate)
print(fromDate, toDate)
0
source

Source: https://habr.com/ru/post/1664129/


All Articles