ios - Creating NSDate from Int components -
i have calendar select date:
func sacalendardate(calendar: sacalendar!, didselectdate day: int32, month: int32, year: int32) { var date = date.from(year: int(year), month: int(month), day: int(day)) print("\(year) , \(month) , \(day)") let formatter = nsdateformatter() formatter.datestyle = nsdateformatterstyle.mediumstyle formatter.timestyle = nsdateformatterstyle.nostyle let datestring = formatter.stringfromdate(date) buttonselectdates.settitle("\(datestring)", forstate: uicontrolstate.normal) getusers(date) }
when feed "date" in method:
class func from(#year:int, month:int, day:int) -> nsdate { var c = nsdatecomponents() c.year = year c.month = month c.day = day var gregorian = nscalendar(identifier:nscalendaridentifiergregorian) var date = gregorian!.datefromcomponents(c) return date! }
i date 1 day behind: selecteddate: 2015-07-14 22:00:00 +0000 , parameters are: 2015, 7 , 15. why happen?
nsdate
single point in time represented seconds since 1.1.1970, not care timezones or - "timezone" gmt , +0000 (no offset). if create nsdate
calendar local timezone, example +0200 timezone offset taken off actual date provide represent point in time without timezone. readable date representation of nsdate
need use nsdateformatter
knows current timezone:
let date = from(2015, month: 7, day: 15) let dateformatter = nsdateformatter() dateformatter.dateformat = "yyyy-mm-dd hh:mm:ss z" print(dateformatter.stringfromdate(date)) print(date)
you receive following printed output:
2015-07-15 00:00:00 +0200
2015-07-14 22:00:00 +0000
Comments
Post a Comment