Created
May 30, 2017 14:01
-
-
Save billwang1990/7d804c9e9234f442e227ae07e4781863 to your computer and use it in GitHub Desktop.
Introduce local extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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