NSDateFormatter in Swift ?
The object of NSDateFormatter which create string is represent of NSDate objects. also conversation of dates and times of NSDate object. You have flexibility to change of NSDate object using NSDateFormatter object.
Here some of Example, To change of NSDate object formate using NSDateFormatter.
Thursday, Mar 17, 2016
let dateFormate = NSDateFormatter()
dateFormate.dateFormat = "EEEE, MMM d, yyyy"
let date = NSDate()
let stringOfDate = dateFormate.stringFromDate(date)
print(stringOfDate)
03/17/2016
let dateFormate = NSDateFormatter()
dateFormate.dateFormat = "MM/dd/yyyy"
let date = NSDate()
let stringOfDate = dateFormate.stringFromDate(date)
print(stringOfDate)
03-17-2016 07:10
let dateFormate = NSDateFormatter()
dateFormate.dateFormat = "MM-dd-yyyy HH:mm"
let date = NSDate()
let stringOfDate = dateFormate.stringFromDate(date)
print(stringOfDate)
Mar 17, 7:10 AM
let dateFormate = NSDateFormatter()
dateFormate.dateFormat = "MMM d, H:mm a"
let date = NSDate()
let stringOfDate = dateFormate.stringFromDate(date)
print(stringOfDate)
March 2016
let dateFormate = NSDateFormatter()
dateFormate.dateFormat = "MMMM yyyy"
let date = NSDate()
let stringOfDate = dateFormate.stringFromDate(date)
print(stringOfDate)
17.03.16
let dateFormate = NSDateFormatter()
dateFormate.dateFormat = "dd.MM.yy"
let date = NSDate()
let stringOfDate = dateFormate.stringFromDate(date)
print(stringOfDate)
Mar 17, 2016
let dateFormate = NSDateFormatter()
dateFormate.dateFormat = "MMM d, yyyy"
let date = NSDate()
let stringOfDate = dateFormate.stringFromDate(date)
print(stringOfDate)
Thu, 17 Mar 2016 07:10:30 -0500
let dateFormate = NSDateFormatter()
dateFormate.dateFormat = "E, d MMM yyyy HH:mm:ss Z"
let date = NSDate()
let stringOfDate = dateFormate.stringFromDate(date)
print(stringOfDate)
2016-03-17T07:10:30-0500
let dateFormate = NSDateFormatter()
dateFormate.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = NSDate()
let stringOfDate = dateFormate.stringFromDate(date)
print(stringOfDate)
NSDateFormatterStyle :
You can set dateStyle and timeStyle according to requirement and independently. To display only time then set only timeStyle, for date to set dateStyle, and Both of use then adding dateStyle and timeStyle in NSDateFormate.
3/17/16 : ShortStyle
let dateFormate = NSDateFormatter()
dateFormate.dateStyle = NSDateFormatterStyle.ShortStyle
let date = NSDate()
let stringOfDate = dateFormate.stringFromDate(date)
print(stringOfDate)
Mar 17, 2016, 6:12:30 PM : MediumStyle
let dateFormate = NSDateFormatter()
dateFormate.dateStyle = NSDateFormatterStyle.MediumStyle
dateFormate.timeStyle = NSDateFormatterStyle.MediumStyle
let date = NSDate()
let stringOfDate = dateFormate.stringFromDate(date)
print(stringOfDate)
March 17, 2016 at 6:14:36 PM GMT+5:30 : LongStyle
let dateFormate = NSDateFormatter()
dateFormate.dateStyle = NSDateFormatterStyle.LongStyle
dateFormate.timeStyle = NSDateFormatterStyle.LongStyle
let date = NSDate()
let stringOfDate = dateFormate.stringFromDate(date)
print(stringOfDate)
Thursday, March 17, 2016 : FullStyle
let dateFormate = NSDateFormatter()
dateFormate.dateStyle = NSDateFormatterStyle.FullStyle
let date = NSDate()
let stringOfDate = dateFormate.stringFromDate(date)
print(stringOfDate)
NSDateFormatter in Swift ?
Reviewed by KIRIT MODI
on
21:57:00
Rating:
No comments: