Skip to content

Instantly share code, notes, and snippets.

View aoenth's full-sized avatar

Kevin Peng aoenth

  • Toronto, Canada
View GitHub Profile
var normalCalendarDateComponents = DateComponents()
normalCalendarDateComponents.year = 2020
normalCalendarDateComponents.month = 5
normalCalendarDateComponents.day = 1
What Swift gives us What it says in Chinese Associated Zodiac
zi rat
chou ox
yin tiger
mao rabbit
chen dragon
si snake
wu horse
wei goat
shen monkey
let calendar = Calendar(identifier: .gregorian)
let normalDate = calendar.date(from: normalCalendarDateComponents)!
let formatter = DateFormatter()
formatter.dateFormat = "MMM d, yyyy"
print(formatter.string(from: normalDate))
@aoenth
aoenth / CZ004.swift
Last active February 24, 2020 20:06
let formatter = DateFormatter()
formatter.dateFormat = "MMM d, yyyy"
formatter.calendar = .init(identifier: .chinese)
formatter.dateStyle = .full
print(formatter.string(from: normalDate))
let dict = [
"zi": "rat",
"chou": "ox",
"yin": "tiger",
"mao": "rabbit",
"chen": "dragon",
"si": "snake",
"wu": "horse",
"wei": "goat",
"shen": "monkey",
@aoenth
aoenth / CZ007.swift
Last active February 24, 2020 20:18
guard let hyphen = chineseDate.firstIndex(of: "-") else {
fatalError("\(chineseDate) is not correctly formatted, use DateFormatter.Style.full")
}
let startIndex = chineseDate.index(after: hyphen)
let endIndex = chineseDate.index(chineseDate.endIndex, offsetBy: -2)
let branch = chineseDate[startIndex ... endIndex]
print(branch)
func normalDate(fromYear year: Int, month: Int, day: Int) -> Date {
var normalCalendarDateComponents = DateComponents()
normalCalendarDateComponents.year = year
normalCalendarDateComponents.month = month
normalCalendarDateComponents.day = day
let normalCalendar = Calendar(identifier: .gregorian)
let normalDate = normalCalendar.date(from: normalCalendarDateComponents)!
return normalDate
}
let date = normalDate(fromYear: 1991, month: 6, day: 22)
let zodiac = zodiacFrom(date: date)
print(zodiac)
let anotherDate = normalDate(fromYear: 1991, month: 1, day: 20)
let anotherZodiac = zodiacFrom(date: anotherDate)
print(anotherZodiac)