ios - Difference between 2 NSDates, and divide the result with an Int - Swift -
i've been developing swift ios apps few months, , i'm new dates, find bit difficult. want difference in days between 2 nsdate
(one of them today, , other 1 string), , after that, number of days between them, , divide int(datamoneyleft)
, in order money per day.
thanks help!
here's code:
var datepassed1 : string? var datamoneyleft : int? func getdatedifference(){ let date = nsdate() let calendar = nscalendar.currentcalendar() let components = calendar.components([.day, .month], fromdate: date) //let year = components.year //let month = components.month //let day = components.day //-----------************-----------// let dateformatter = nsdateformatter() dateformatter.dateformat = "yyyy-mm-dd" let datestring = dateformatter.datefromstring(datepassed1!) let calendar1: nscalendar = nscalendar.currentcalendar() // replace hour (time) of both dates 00:00 let date1 = calendar1.startofdayfordate(datestring!) let date2 = calendar1.startofdayfordate(date) let flags = nscalendarunit.day let components1 = calendar.components(flags, fromdate: date1, todate: date2, options: []) let finaldifference = components1.day }
first convert second string nsdate using dateformatter. find difference between 2 nsdates using timeintervalsincedate.you timeintervalsincedate nstimeinterval.then can solve problem using difference.
Comments
Post a Comment