Swift NSDateFormatter

Swift NSDateFormatter

Created By :Debasis Das (28-Feb-2016)

In this article we will write a couple of sample code for Swift NSDateFormatter

Before we start lets understand the NSDateFormatter and the different methods available in a NSDateFormatter class

    
    //Understanding the NSDateFormatter
    func dateFormatterTest11(){
        let date = NSDate(timeIntervalSince1970: 0)
        print(date) 
       //1970-01-01 00:00:00 +0000
        
        let date1 = NSDate(timeIntervalSince1970: 20*365*24*60*60)
        print(date1)    
//1989-12-27 00:00:00 +0000
        
        let dateFormatter = NSDateFormatter()
        print(dateFormatter.locale.debugDescription)

        print(dateFormatter.timeZone)
        //Asia/Kolkata (IST) offset 19800
        
        print(dateFormatter.twoDigitStartDate)
        //Optional(1950-01-01 00:00:00 +0000)
 
        print(dateFormatter.defaultDate)                
        //nil

        print(dateFormatter.eraSymbols)                 
        //["BC", "AD"]
        print(dateFormatter.monthSymbols)               
        //["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
        print(dateFormatter.shortMonthSymbols)          
        //["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

        print(dateFormatter.weekdaySymbols)             
        //["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

        print(dateFormatter.shortWeekdaySymbols)        
       //["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]

        print(dateFormatter.veryShortWeekdaySymbols)    
        //["S", "M", "T", "W", "T", "F", "S"]

        print(dateFormatter.standaloneWeekdaySymbols)   
       //["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

        print(dateFormatter.AMSymbol)                  
        //am

        print(dateFormatter.PMSymbol)                   
        //pm

        print(dateFormatter.longEraSymbols)             
        //["Before Christ", "Anno Domini"]

        print(dateFormatter.veryShortMonthSymbols)      
        //["J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"]

        print(dateFormatter.standaloneMonthSymbols)     
       //["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

        print(dateFormatter.shortStandaloneMonthSymbols)
        //["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

        print(dateFormatter.veryShortStandaloneMonthSymbols)
        //["J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"]

        print(dateFormatter.veryShortWeekdaySymbols)        
        //["S", "M", "T", "W", "T", "F", "S"]

        print(dateFormatter.standaloneWeekdaySymbols)       
        //["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

        print(dateFormatter.shortStandaloneWeekdaySymbols)  
        //["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]

        print(dateFormatter.veryShortStandaloneWeekdaySymbols) 
        //["S", "M", "T", "W", "T", "F", "S"]

        print(dateFormatter.quarterSymbols)              
       //["1st quarter", "2nd quarter", "3rd quarter", "4th quarter"]

        print(dateFormatter.shortQuarterSymbols)        
        //["Q1", "Q2", "Q3", "Q4"]

        print(dateFormatter.standaloneQuarterSymbols)    
        //["1st quarter", "2nd quarter", "3rd quarter", "4th quarter"]

        print(dateFormatter.shortStandaloneQuarterSymbols)
        //["Q1", "Q2", "Q3", "Q4"]

        print(dateFormatter.gregorianStartDate)          
        //Optional(1582-10-15 00:00:00 +0000)

        print(dateFormatter.doesRelativeDateFormatting)         
        //false

    }
    //String from date using NSDateFormatter - Different Date Styles
    func dateFormatterTest1(){
        let date = NSDate()
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateStyle = .NoStyle
        let dateString = dateFormatter.stringFromDate(date)
        print(dateString) 
        //Does not print any thing
        
    }

ShortStyle NSDateFormatter

    func dateFormatterTest2(){
        let date = NSDate()
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateStyle = .ShortStyle
        let dateString = dateFormatter.stringFromDate(date)
        print("short style date \(dateString)") 
        //short style date 28/02/16
        
    }

MediumStyle NSDateFormatter

    func dateFormatterTest3(){
        let date = NSDate()
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateStyle = .MediumStyle
        let dateString = dateFormatter.stringFromDate(date)
        print("Medium Style date \(dateString)") 
        //Medium Style date 28-Feb-2016
        
    }

LongStyle NSDateFormatter

    func dateFormatterTest4(){
        let date = NSDate()
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateStyle = .LongStyle
        let dateString = dateFormatter.stringFromDate(date)
        print("Long Style date \(dateString)") 
        //Long Style date 28 February 2016
        
    }

FullStyle NSDateFormatter

    func dateFormatterTest5(){
        let date = NSDate()
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateStyle = .FullStyle
        let dateString = dateFormatter.stringFromDate(date)
        print("Full Style date \(dateString)") 
        //Full Style date Sunday, 28 February 2016
        
    }

Custom NSDateFormatter MM-dd-yyyy hh:mm

   
    func dateFormatterTest6(){
        let date = NSDate()
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "MM-dd-yyyy hh:mm"
        let dateString = dateFormatter.stringFromDate(date)
        print("Custom date format Sample 1 =  \(dateString)") 
        //Custom date format Sample 1 =  02-28-2016 11:41
    }

Custom NSDateFormatter dd-MMM-yyyy hh:mm:ss

    func dateFormatterTest7(){
        let date = NSDate()
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "dd-MMM-yyyy hh:mm:ss"
        let dateString = dateFormatter.stringFromDate(date)
        print("Custom Date Format Sample 2 \(dateString)") 
        //Custom Date Format Sample 2 28-Feb-2016 11:41:51
        
    }

Custom NSDateFormatter

   
    func dateFormatterTest8(){
        let date = NSDate()
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "dd/MMM/yyyy hh:mm:ss"
        let dateString = dateFormatter.stringFromDate(date)
        print("Custom Date Format Sample 3 \(dateString)") 
        //Custom Date Format Sample 3 28/Feb/2016 11:41:51
        
    }

Custom NSDateFormatter with AM/PM

    //AM PM
    func dateFormatterTest9(){
        let date = NSDate()
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "dd-MMM-yyyy hh:mm a" //use a for am /pm
        let dateString = dateFormatter.stringFromDate(date)
        print("Custom Date Format Sample 4 with AM/PM \(dateString)") 
       //Custom Date Format Sample 4 with AM/PM 28-Feb-2016 11:41 pm
        
    }

FullStyle NSDateFormatter

   
    //Both date and time style
    func dateFormatterTest10(){
        let date = NSDate()
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateStyle = .FullStyle
        dateFormatter.timeStyle = .FullStyle
        /*
        Other Time styles are as follows
            NoStyle
            ShortStyle
            MediumStyle
            LongStyle
            FullStyle
        */
        let dateString = dateFormatter.stringFromDate(date)
        print("FullStyle Date Format = \(dateString)") 
        //FullStyle Date Format = Sunday, 28 February 2016 11:41:51 pm India Standard Time
        
        dateFormatter.PMSymbol = "Post Noon"
        dateFormatter.AMSymbol = "Before Noon"
        
        let dateString1 = dateFormatter.stringFromDate(date)
        print("FullStyle Date Format With Custom Label for AM /PM = \(dateString1)")
        //FullStyle Date Format With Custom Label for AM /PM = Sunday, 28 February 2016 11:41:51 Post Noon India Standard Time

        
    }

Date from String using NSDateFormatter

    
    //    Date from String using Date Formatter
    func dateFormatterTest12(){
        //converting a date from string
        let dateString = "2016-02-26"
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        let date = dateFormatter.dateFromString(dateString)
        print("Date from String = \(date)") 
        //Date from String = Optional(2016-02-25 18:30:00 +0000)
        
    }
}
Posted in Swift Tagged with: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Recent Posts


Hit Counter provided by technology news