Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save billwang1990/7d804c9e9234f442e227ae07e4781863 to your computer and use it in GitHub Desktop.
Save billwang1990/7d804c9e9234f442e227ae07e4781863 to your computer and use it in GitHub Desktop.
Introduce local extension
//: Introduce Local Extension
/*:
*/
import Foundation
struct DateWrapper {
private var origDate: Date
init(date: Date) {
origDate = date
}
init?(dateString: String) {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd hh:mm"
if let date = formatter.date(from: dateString) {
origDate = date
} else {
return nil
}
}
func getPreviousDate() -> Date {
return Date(timeIntervalSinceNow: -60 * 60 * 24 )
}
func getNextDate() -> Date {
return Date(timeIntervalSinceNow: 60 * 60 * 24)
}
func getYear() -> String {
return generateDateFormatter(partType: "yyyy").string(from: origDate)
}
func getMonth() -> String {
return generateDateFormatter(partType: "MM").string(from: origDate)
}
func getDays() -> String {
return generateDateFormatter(partType: "dd").string(from: origDate)
}
private func generateDateFormatter(partType: String) -> DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = partType
return formatter
}
}
let dateWrapper = DateWrapper(date: Date())
let previousDate = dateWrapper.getPreviousDate()
let nextDate = dateWrapper.getNextDate()
let year = dateWrapper.getYear()
let month = dateWrapper.getMonth()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment