Main code:
Get time model, parameter format: 2016-12-12 18:11:12
Func getdatemodelwithstring (datestr:string)->datemodel? {
var model:datemodel?
Let formatter =nsdateformatter.init ()
Date gets GMT time, which differs from Beijing time by 8 hours and requires the following settings
Formatter.locale = Nslocale.currentlocale ()
Formatter.dateformat = "Yyyy-mm-dd HH:mm:ss"
Let date = formatter.datefromstring (DATESTR)
If date!=nil {
Needrealvaluemessage ("------tool.h----Datemodel")
Model = Datemodel.getmodelwithdate (date!)
}
Return model
}
Class Datemodel:nsobject {
var year:int = 0
var month:int = 0
var day:int = 0
var hour:int = 0
var minute:int = 0
var second:int = 0
Classfunc getmodelwithdate (newdate:nsdate), datemodel{
Let model = Datemodel ()
Let calendar = Nscalendar.init (Identifier:nscalendaridentifiergregorian)
Iflet year = calendar?.component (Nscalendarunit.year, fromdate:newdate) {
Model.year = Year
}
Iflet month = calendar?.component (Nscalendarunit.month, fromdate:newdate) {
Model.month = Month
}
Iflet day = calendar?.component (Nscalendarunit.day, fromdate:newdate) {
Model.day = Day
}
Iflet hour = calendar?.component (Nscalendarunit.hour, fromdate:newdate) {
Model.hour = Hour
}
Iflet minute = Calendar?.component (Nscalendarunit.minute, fromdate:newdate) {
Model.minute = Minute
}
Iflet second = calendar?.component (Nscalendarunit.second, fromdate:newdate) {
Model.second = Second
}
Return model
}
}