Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xiangyu-sun/e57c5162f2406b512f5a53131cdd5967 to your computer and use it in GitHub Desktop.
Save xiangyu-sun/e57c5162f2406b512f5a53131cdd5967 to your computer and use it in GitHub Desktop.
Using apple's formatter to get Chinese yang calendar detail
let calendar = Calendar(identifier: .chinese)
let formatter = DateFormatter()
formatter.calendar = calendar
formatter.dateStyle = .long
let regex = try? NSRegularExpression(pattern: ##"(\w*)-(\w*)"##)
let str = formatter.string(from: self)
let matches = regex?.matches(in: str, options: [], range: NSRange(location: 0, length: str.count))
var dizhi: String?
matches?.forEach { (result) in
let ns = (str as NSString)
dizhi = ns.substring(with: result.range(at: 2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment