W3Schools Learner's Blog

W3Schools Programming knowledge summary website

div

12/20/2017

NSDateFormatter Example in Swift

DateFormatter Example in Swift
The object of DateFormatter which create string is represent of Date objects. also conversation of dates and times of Date object. You have flexibility to change of Date object using DateFormatter object.
Here some of Example, To change of Date object formate using DateFormatter. 

Thursday, Mar 17, 2017

12345let dateFormate = DateFormatter()
dateFormate.dateFormat = "EEEE, MMM d, yyyy"
let date = Date()
let stringOfDate = dateFormate.string(date)
print(stringOfDate)

 03/17/2017

12345let dateFormate = DateFormatter()
dateFormate.dateFormat = "MM/dd/yyyy"
let date = Date()
let stringOfDate = dateFormate.string(date)
print(stringOfDate)

03-17-2017 07:10

12345let dateFormate = DateFormatter()
dateFormate.dateFormat = "MM-dd-yyyy HH:mm"
let date = Date()
let stringOfDate = dateFormate.string(date)
print(stringOfDate)

Mar 17, 7:10 AM

12345let dateFormate = DateFormatter()
dateFormate.dateFormat = "MMM d, H:mm a"
let date = Date()
let stringOfDate = dateFormate.string(date)
print(stringOfDate)

March 2017

12345let dateFormate = DateFormatter()
dateFormate.dateFormat = "MMMM yyyy"
let date = Date()
let stringOfDate = dateFormate.string(date)
print(stringOfDate)

17.03.16

12345let dateFormate = DateFormatter()
dateFormate.dateFormat = "dd.MM.yy"
let date = Date()
let stringOfDate = dateFormate.string(date)
print(stringOfDate)

Mar 17, 2017

12345let dateFormate = DateFormatter()
dateFormate.dateFormat = "MMM d, yyyy"
let date = Date()
let stringOfDate = dateFormate.string(date)
print(stringOfDate)

Thu, 17 Mar 2017 07:10:30 -0500

12345let dateFormate = DateFormatter()
dateFormate.dateFormat = "E, d MMM yyyy HH:mm:ss Z"
let date = Date()
let stringOfDate = dateFormate.string(date)
print(stringOfDate)

2017-03-17T07:10:30-0500

12345let dateFormate = DateFormatter()
dateFormate.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = Date()
let stringOfDate = dateFormate.string(date)
print(stringOfDate)

2017-03-17 07:10:30

12345let dateFormate = DateFormatter()
dateFormate.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date = Date()
let stringOfDate = dateFormate.string(date)
print(stringOfDate)

DateFormatterStyle  :

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 DateFormate.

3/17/16 : ShortStyle 

12345let dateFormate = DateFormatter()
dateFormate.dateStyle = DateFormatterStyle.ShortStyle    
let date = Date()
let stringOfDate = dateFormate.string(date)
print(stringOfDate)

Mar 17, 2017, 6:12:30 PM MediumStyle 

123456let dateFormate = DateFormatter()
dateFormate.dateStyle = DateFormatterStyle.MediumStyle
dateFormate.timeStyle = DateFormatterStyle.MediumStyle    
let date = Date()
let stringOfDate = dateFormate.string(date)
print(stringOfDate)

March 17, 2017 at 6:14:36 PM GMT+5:30 LongStyle 

123456let dateFormate = DateFormatter()
dateFormate.dateStyle = DateFormatterStyle.LongStyle
dateFormate.timeStyle = DateFormatterStyle.LongStyle    
let date = Date()
let stringOfDate = dateFormate.string(date)
print(stringOfDate)

Thursday, March 17, 2017 : FullStyle

12345let dateFormate = DateFormatter()
dateFormate.dateStyle = DateFormatterStyle.FullStyle 
let date = Date()
let stringOfDate = dateFormate.string(date)
print(stringOfDate)

No comments:

Post a Comment

Note: only a member of this blog may post a comment.