current date, last date , first date , compared date swift 3

let date = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day], from: date)

let year =  components.year
let month = components.month
let day = components.day

print(year)
print(month)
print(day)

You get the first day of the month simply with
let components = calendar.components([.Year, .Month], fromDate: date)
let startOfMonth = calendar.dateFromComponents(components)!
print(dateFormatter.stringFromDate(startOfMonth)) // 2015-11-01
To get the last day of the month, add one month and subtract one day:
let comps2 = NSDateComponents()
comps2.month = 1
comps2.day = -1
let endOfMonth = calendar.dateByAddingComponents(comps2, toDate: startOfMonth, options: [])!
print(dateFormatter.stringFromDate(endOfMonth)) // 2015-11-30

Alternatively, use the rangeOfUnit method which gives you the start and the length of the month:
var startOfMonth : NSDate?
var lengthOfMonth : NSTimeInterval = 0
calendar.rangeOfUnit(.Month, startDate: &startOfMonth, interval: &lengthOfMonth, forDate:
For a date on the last day of month, add the length of the month minus one second:
let endOfMonth = startOfMonth!.dateByAddingTimeInterval(lengthOfMonth - 1)
func getDayOfWeek(today:String)->Int {

    let formatter  = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    let todayDate = formatter.dateFromString(today)!
    let myCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    let myComponents = myCalendar.components(.Weekday, fromDate: todayDate)
    let weekDay = myComponents.weekday
    return weekDay
}

let weekday = getDayOfWeek("2014-08-27")
print(weekday) // 4 = Wednesday
extension Date {
    var weekdayOrdinal: Int {
        return Calendar.current.component(.weekday, from: self)
    }
}

If you want the full "Wednesday", "Sunday", etc.
extension NSDate {
    func dayOfTheWeek() -> String? {        
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "EEEE"
        return dateFormatter.stringFromDate(self)
    }
}

extension Date {
    func dayOfWeek() -> String? {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "EEEE"
        return dateFormatter.string(from: self).capitalized 
        // or use capitalized(with: locale) if you want
    }
}

print(Date().dayOfWeek()!) // Wednesday
func compareDate(date1: NSDate, toDate date2: NSDate, toUnitGranularity unit: NSCalendarUnit) -> NSComparisonResult
let now = NSDate()
// "Sep 23, 2015, 10:26 AM"
let olderDate = NSDate(timeIntervalSinceNow: -10000)
// "Sep 23, 2015, 7:40 AM"

var order = NSCalendar.currentCalendar().compareDate(now, toDate: olderDate, 
                toUnitGranularity: .Hour)

switch order {
case .OrderedDescending:
    print("DESCENDING")
case .OrderedAscending:
    print("ASCENDING")
case .OrderedSame:
    print("SAME")
}

// Compare to hour: DESCENDING

order = NSCalendar.currentCalendar().compareDate(now, toDate: olderDate, 
            toUnitGranularity: .Day)

switch order {
case .OrderedDescending:
    print("DESCENDING")
case .OrderedAscending:
    print("ASCENDING")
case .OrderedSame:
    print("SAME")
}

// Compare to day: SAME
// compare dates
func compareDate(date1:NSDate, date2:NSDate) -> Bool {
    let order = NSCalendar.currentCalendar().compareDate(date1, toDate: date2,
        toUnitGranularity: .Day)
    switch order {
    case .OrderedSame:
        return true
    default:
        return false
    }
}
if compareDate(today, date2: anotherDate) {
    // The two dates are on the same day.
}

Popular posts from this blog

How to Use pagination ScrollView in Swift

UISearchBar search text color , background color swift 3