0

So i am making an app that counts how many Calories/protein etc you eat each day and then you can compare after lets say 1 week how much you ate at average day. Everything is working perfect its just that it counts from the exact time you start the session to the exact time when you check the overall statistic. Here i will show some code i use for this function to work:

here is the code to save the start date on the first View:

let currentDate = NSDate()

let defaults5 = NSUserDefaults.standardUserDefaults()
defaults5.setObject(currentDate, forKey: "startDay")

Here is the code to check the end date and compare the dates to see how many days it has passed(this is on another view):

let currentDate = NSDate()
var startDay = NSDate()
var totalDays = Int()

    let defaults1 = NSUserDefaults.standardUserDefaults()
    if let search1 = defaults1.objectForKey("startDay")
    {
        startDay = search1 as! NSDate
    }else{
        print("there is no start date")
    }

func daysBetweenDate(startDate: NSDate, endDate: NSDate) -> Int
{
    let calendar = NSCalendar.currentCalendar()

    let components = calendar.components([.Day], fromDate: startDate, toDate: endDate, options: [])

    return components.day
}

    totalDays = daysBetweenDate(startDay, endDate: currentDate) + 1
    dayLabel.text = String(totalDays)

So in this case the DayLabel will show how many days it is between start day and currentDate (+1) but i want this ignore hours for example if i start the session 16.00 on Tuesday and i check 15.00 on Wednesday it will still count as 0 days because it has only passed 23 hours, i would want it to start the startDay at 00.00 every time.

Wain
  • 118,658
  • 15
  • 128
  • 151
Robinhiio
  • 105
  • 1
  • 11
  • Possible duplicate of [Number of days between two NSDates](http://stackoverflow.com/questions/4739483/number-of-days-between-two-nsdates) – vadian Jan 21 '16 at 12:59

1 Answers1

0

Try this out:

n.b. just using dateFormatter for some sample dates.

let start = "2016-01-20 18"
let end = "2016-01-21 17"

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH"

var startDate:NSDate = dateFormatter.dateFromString(start)!
var endDate:NSDate = dateFormatter.dateFromString(end)!

let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!

startDate = cal.startOfDayForDate(startDate)
endDate = cal.startOfDayForDate(endDate)

let totalDays = cal.components(.Day, fromDate: startDate, toDate: endDate, options: []).day

print(totalDays)

Here we set the time of both days to 12:00am, so that the time doesn't really matter, just the days between.

Matt Le Fleur
  • 2,708
  • 29
  • 40
  • hmm i can't see where you set the time thath looks exactlylike my code what am i missing :P ? – Robinhiio Jan 21 '16 at 13:26
  • `startDate` and `endDate` are the equivalent of your `startDay` and `currentDay` respectively, just using example dates (which are similar to your example - 23 hours apart on different days) – Matt Le Fleur Jan 21 '16 at 13:30
  • but with you example its just like mine, if u start session 2016-20-18 and then check summary 2016-21-17 then it will show 0 Days difference, i want it to just Count the start date Always as for example 2016-20-00.00 even if you start it 2016-20-18.00, i don't know if that is what you are doing and i just don't understand it or you don't understand my (bad) explanation :P – Robinhiio Jan 21 '16 at 14:25
  • If you run the above code (try Playground), it will print 1 for `totalDays` :) – Matt Le Fleur Jan 21 '16 at 14:26