Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Find Current month,Day,WeekDay from Date in Swift

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 5.26k
    Comment on it

    Hi,

    To get the current Year, Current month, Current WeekDay, and current day if the date you can use the following code.

    To use this create a Extension of the NSDate and call the method with NSDate Object.

    And call the method like CURRENT_DATE.get_year()

    It will return the Current Year    

    var CURRENT_DATE = NSDate()
    extension NSDate{ 
    //Function will return the current year   
    func  get_year() -> NSInteger {
            let calendar: NSCalendar = NSCalendar.currentCalendar()
            let component: NSDateComponents = calendar.components(NSCalendarUnit.Year, fromDate: self)
            return component.year;
        } 
    //Function will reutrn the current month Number  
        func  get_Month() -> NSInteger {
            let calendar: NSCalendar = NSCalendar.currentCalendar()
            let component: NSDateComponents = calendar.components(NSCalendarUnit.Month, fromDate: self)
            return component.month;
        }
    //Function Will return the number of day of the month. 
        func  get_Day() -> NSInteger {
            let calendar: NSCalendar = NSCalendar.currentCalendar()
            let component: NSDateComponents = calendar.components(NSCalendarUnit.Day, fromDate: self)
            return component.day;
        }
    //Will return the number day of week. like Sunday =1 , Monday = 2
        func  get_WeekDay() -> NSInteger {
            let calendar: NSCalendar = NSCalendar.currentCalendar()
            let component: NSDateComponents = calendar.components(NSCalendarUnit.Weekday, fromDate: self)
            return component.weekday;
        }
    
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: