Get Date Day, Month or Year in Swift 3.0

Just a quick update to show how to get Date Day, Month or Year in Swift 3

//Here I’m creating the calendar instance that we will operate with:

let calendar = NSCalendar.init(calendarIdentifier: NSCalendar.Identifier.gregorian)

     

 

//Now asking the calendar what month are we in today’s date:

let currentMonthInt = (calendar?.component(NSCalendar.Unit.month, from: Date()))!

       

 

//Now asking the calendar what year are we in today’s date:

let currentYearInt = (calendar?.component(NSCalendar.Unit.year, from: Date()))!

 

Hope this proves useful.

If you would have any suggestions on how to improve reach me @MarcMasVi in Twitter. 

 Marc

Dates & Components in Swift 3.0

The way we work with dates has changed in Swift 3, the change makes it’s way simpler then before. Let’s look at an example:

//CreateDateFromComponents (1st January 2017)

            var newDate = Date()

            let newDateComponents = DateComponents(calendar: Calendar.current, timeZone: nil, era: nil, year: 2017, month: 1, day: 1, hour: nil, minute: nil, second: nil, nanosecond: nil, weekday: nil, weekdayOrdinal: nil, quarter: nil, weekOfMonth: nil, weekOfYear: nil, yearForWeekOfYear: nil)

            newDate = Calendar.current.date(from: newDateComponents)!

 

Hope this proves useful. If you would have any suggestions on how to improve you know where to reach me, @MarcMasVi in Twitter.

 

Marc

 

Get NSDate Day, Month or Year in Swift 2.0

Looking for Swift 3.0 version? -> Click here 

Well, having recharged batteries after vacation I’m back into coding!

And yet again I was faced with another simple problem: how to get the day, month or year from a date. 

This is the simpler solution I’ve found in Swift 2:

 

//Here I’m creating the calendar instance that we will operate with:

let calendar = NSCalendar.init(calendarIdentifier: NSCalendarIdentifierGregorian)

     

 

//Now asking the calendar what month are we in today’s date:

let currentMonthInt = (calendar?.component(NSCalendarUnit.Month, fromDate: NSDate()))!

       

 

//Now asking the calendar what year are we in today’s date:

let currentYearInt = (calendar?.component(NSCalendarUnit.Year, fromDate: NSDate()))!

 

Note that in my code I used NSDate() as the date, this will return today’s date. Also, instead of month or year you could use any of the NSCalendarUnits to retrieve the relevant unit you’re interested in. 

Hope this proves useful. If you would have any suggestions on how to improve you know where to reach me, @MarcMasVi in Twitter.

 

Marc

Dates & Components in Swift 2.0

Another topic that has been changed in Swift 2.0 is how to work with dates.

I’m digging into this topic for my upcoming app and I’ve found that most documentation on the web refers to Swift 1.2. So here are some snippets to create dates using components:

//CreateDateFromComponents (1st January 2016)

       var newDate = NSDate()

       let newDateComponents: NSDateComponents = NSDateComponents()

       newDateComponents.setValue(1, forComponent: NSCalendarUnit.Day)

       newDateComponents.setValue(1, forComponent: NSCalendarUnit.Month)

       newDateComponents.setValue(2016, forComponent: NSCalendarUnit.Year)

       newDate = NSCalendar.currentCalendar().dateFromComponents(newDateComponents)!

 

//AddToDateFromComponents (1 day, 1 month and 1 year to today)

 

        var newDate = NSDate()

        let newDateComponentsWhereQueryShouldBegin: NSDateComponents = NSDateComponents()

        newDateComponents.setValue(1, forComponent: NSCalendarUnit.Day)

        newDateComponents.setValue(1, forComponent: NSCalendarUnit.Month)

        newDateComponents.setValue(1, forComponent: NSCalendarUnit.Year)

        newDate = NSCalendar.currentCalendar().dateByAddingComponents(newDateComponents, toDate: newDate, options: NSCalendarOptions(rawValue: 0))! 

 

Hope this proves useful. If you would have any suggestions on how to improve you know where to reach me, @MarcMasVi in Twitter.

 

Marc