Swift DateFormatter

Thursday, Mar 17, 2016

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

03/17/2016

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

03-17-2016 07:10

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

Mar 17, 7:10 AM

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

March 2016

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

17.03.16

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

Mar 17, 2016

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

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

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

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

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

NSDateFormatterStyle

3/17/16 (ShortStyle )

let dateFormate = DateFormatter()
dateFormate.dateStyle = .shortStyle    
let date = Date()
let stringOfDate = dateFormate.string(from: date)
print(stringOfDate)

Mar 17, 2016, 6:12:30 PM ( MediumStyle )

let dateFormate = DateFormatter()
dateFormate.dateStyle = .mediumStyle
dateFormate.timeStyle = .mediumStyle    
let date = Date()
let stringOfDate = dateFormate.string(from: date)
print(stringOfDate)

March 17, 2016 at 6:14:36 PM GMT+5:30 (LongStyle )

let dateFormate = DateFormatter()
dateFormate.dateStyle = .longStyle
dateFormate.timeStyle = .longStyle    
let date = Date()
let stringOfDate = dateFormate.string(from: date)
print(stringOfDate)

Thursday, March 17, 2016 (FullStyle)

let dateFormate = DateFormatter()
dateFormate.dateStyle = .fullStyle 
let date = Date()
let stringOfDate = dateFormate.string(from: date)
print(stringOfDate)

你可能感兴趣的:(Swift DateFormatter)