Skip to content

Instantly share code, notes, and snippets.

Avatar

Kevin Peng aoenth

  • Toronto, Canada
View GitHub Profile
View CZ001.swift
var normalCalendarDateComponents = DateComponents()
normalCalendarDateComponents.year = 2020
normalCalendarDateComponents.month = 5
normalCalendarDateComponents.day = 1
View CZ005.csv
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
View CZ002.swift
let calendar = Calendar(identifier: .gregorian)
let normalDate = calendar.date(from: normalCalendarDateComponents)!
View CZ003.swift
let formatter = DateFormatter()
formatter.dateFormat = "MMM d, yyyy"
print(formatter.string(from: normalDate))
View CZ004.swift
let formatter = DateFormatter()
formatter.dateFormat = "MMM d, yyyy"
formatter.calendar = .init(identifier: .chinese)
formatter.dateStyle = .full
print(formatter.string(from: normalDate))
View CZ006.swift
let dict = [
"zi": "rat",
"chou": "ox",
"yin": "tiger",
"mao": "rabbit",
"chen": "dragon",
"si": "snake",
"wu": "horse",
"wei": "goat",
"shen": "monkey",
View CZ007.swift
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)
View CZ008.swift
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
}
View CZ009.swift
let date = normalDate(fromYear: 1991, month: 6, day: 22)
let zodiac = zodiacFrom(date: date)
print(zodiac)
View CZ010.swift
let anotherDate = normalDate(fromYear: 1991, month: 1, day: 20)
let anotherZodiac = zodiacFrom(date: anotherDate)
print(anotherZodiac)