Skip to content

Instantly share code, notes, and snippets.

@sketchytech
Created November 14, 2016 10:11
Show Gist options
  • Save sketchytech/cf5539762a76182b440ea4f516f37ee3 to your computer and use it in GitHub Desktop.
Save sketchytech/cf5539762a76182b440ea4f516f37ee3 to your computer and use it in GitHub Desktop.
Swift 3: Time and Date
import UIKit
// see here for details about date formatter https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369
func time()->(h:Int,m:Int,s:Int) {
let formatStrings = ["hh","mm","ss"]
var hms = [Int]()
let dateFormatter = DateFormatter()
let date = Date()
for f in formatStrings {
dateFormatter.dateFormat = f
if let formattedDateString = Int(dateFormatter.string(from: date))
{
hms.append(formattedDateString)
}
}
return (h:hms[0],m:hms[1],s:hms[2])
}
let t = time()
t.h
t.m
t.s
func date()->(day:Int,month:Int,year:Int) {
let formatStrings = ["dd","MM","YYYY"]
let dateFormatter = DateFormatter()
let date = Date()
var YMD = [Int]()
for f in formatStrings {
dateFormatter.dateFormat = f
if let formattedDateString = Int(dateFormatter.string(from: date))
{
YMD.append(formattedDateString)
}
}
return (day:YMD[0],month:YMD[1],year:YMD[2])
}
let d = date()
d.day
d.month
d.year
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment