ksa_coder ksa_coder - 1 month ago 23x
Swift Question

Swift - Convert Milliseconds to Date

I have a data structure that contains timestamps in milliseconds such as 1470075992. I would like to convert this to a date only i.e. example 24/12/2016 without keeping the additional details that results from converting.

What function can I use to accomplish this?



Try this extension:

typealias UnixTime = Int

extension UnixTime {
    private func formatType(form: String) -> NSDateFormatter {
        let dateFormatter = NSDateFormatter()
        dateFormatter.locale = NSLocale(localeIdentifier: "en_US")
        dateFormatter.dateFormat = form
        return dateFormatter
    var dateFull: NSDate {
        return NSDate(timeIntervalSince1970: Double(self))
    var toHour: String {
        return formatType("HH:mm").stringFromDate(dateFull)
    var toDay: String {
        return formatType("MM/dd/yyyy").stringFromDate(dateFull)

var myMilliseconds: UnixTime = 1470075992