In the calendar function "date(byAdding: .day, value: , to: )", does the date we give to the function include the current date or not? - ios

I have an app that uses Core Data. I have a function
func showHistory(for days:Int)->Int {
var historyForXDaysInt = Int()
var calendar = Calendar.current
calendar.locale = .current
calendar.timeZone = .current
var startDate:Date?
var endDate = Date()
switch days{
case 1:
startDate = Date()
startDate = calendar.startOfDay(for: startDate!)
default:
startDate = calendar.date(byAdding: .day, value: -days+1, to: Date())
startDate = calendar.startOfDay(for: startDate!)
}
//There is more to this function, where I make the request but I just included the relevant part for my question
}
My question: I am trying to get relevant data from Core Data between two dates via the predicate let predicate = NSPredicate(format: "(completedDate >= %#) AND (completedDate <= %#)", startDate! as NSDate, endDate as NSDate).There is a completedDate property in my core data model. When I make the request I get data for 1 more day than I asked for. I tried solving it with adding 1 to the days variable. For example, if I am asking for 2 days of data (meaning days = 2) I get data for 3 days. What might be the cause? Why does it not work as intended without the +1 ?

Related

FSCalendar: how to get dates in two dates?

I am using Swift 3, and i would like to print the every day between two dates.
For example:
08-10-2017 -> Start Date
08-15-2017 -> End Date
Should print:
08-10-2017
08-11-2017
08-12-2017
08-13-2017
08-14-2017
08-15-2017
I want to get ranges in two specific date, can someone help me out please. I tried to put these two dates to for loop but no chance.
You need to create a calendar based dates, and start increasing start date until you reach end date. Here is a code snippet, how to do it:
func showRange(between startDate: Date, and endDate: Date) {
// Make sure startDate is smaller, than endDate
guard startDate < endDate else { return }
// Get the current calendar, i think in your case it should some fscalendar instance
let calendar = Calendar.current
// Calculate the endDate for your current calendar
let calendarEndDate = calendar.startOfDay(for: endDate)
// Lets create a variable, what we can increase day by day
var currentDate = calendar.startOfDay(for: startDate)
// Run a loop until we reach the end date
while(currentDate <= calendarEndDate) {
// Print the current date
print(currentDate)
// Add one day at the time
currentDate = Calendar.current.date(byAdding: .day, value: 1, to: currentDate)!
}
}
Usage:
let today = Date()
let tenDaysLater = Calendar.current.date(byAdding: .day, value: 10, to: today)!
showRange(between: today, and: tenDaysLater)

How to query realm db to obtain relevant data for each day based on stored timestamp?

This is how my model look like
class Run: Object {
dynamic var duration:Double = 0
dynamic var elevation:Double = 0.0
dynamic var distance:Double = 0.0
dynamic var filePathString:String = ""
dynamic var exported:Bool = false
dynamic var timestamp:Date = Date()
let locations = List<Location>()
}
List of runs is presented in tableView, where each header denotes the exact date (for instance 10/10/2016).
Is it possible to construct realm query to obtain relevant runs for each day?
To be clear, I want to achieve something like this
let isSameDay = Calendar.current.isDate(date1, inSameDayAs: date2)
using Realm queries.
Realm lets you perform queries using Apple's NSPredicate formatting. In this particular case, it should be able to use the BETWEEN syntax to check which dates are within a specific slice of time, specifically between the timestamp representing the start of a day, as well as the end.
let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)
let components = NSDateComponents()
components.year = 2016
components.month = 10
components.day = 10
components.hour = 0
components.minute = 0
components.second = 0
// Get the start date
let startDate = calendar?.dateFromComponents(components)
// Get the end date
components.day = 11
let endDate = calendar?.dateFromComponents(components)
// Query for the runs between these two dates
let runDates = realm.objects(Run.self).filter("timestamp BETWEEN {%#, %#}", startDate, endDate)
(NSDateComponents code stolen from http://nshipster.com/nsdatecomponents/)

Get number of days between two NSDates when crossing new year

I need to get the number of days between two dates which I get but if I input for example: 2016-12-10 and 2017-01-10 there will be a negative number of days. This is just occur when it´s new year between those two dates.
//Get the number of days between two NSDates
func daysBetween(date: NSDate) -> Int {
let calendar: NSCalendar = NSCalendar.currentCalendar()
let date1 = calendar.startOfDayForDate(self)
let date2 = calendar.startOfDayForDate(date)
let components = calendar.components(.Day, fromDate: date1, toDate: date2, options: [])
return components.day
}
//Using the function
let daysBetween = fromDate.daysBetween(toDate)
//Printing -334 if new year is between fromDate and toDate
print(daysBetween)
How can I modify my daysBetween(date: NSDate) function to prevent this from happening?
Edit:
Please don´t pay attention to why it´s printing exacltly -334. That´s just because fromDate and toDate are different days in month. The problem a wanna solve is why the response is negative and not 31 as it should be.
Solved:
It turned out to be as many of you thought. The fromDate is greater then toDate and causing a negative number. Stupid mistake. Thx guys!
You not need to worry about the days going negative. It's better to know if the first date (for example selected from an input UIDatePicker) is bigger than another. That is handled automatically when you converted back to an NSDate.
If the problem is how to print the days , you can use abs(days).
First compare both dates and then you can get correct positive value
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let startDate:NSDate = dateFormatter.dateFromString("2016-12-10")!
let endDate:NSDate = dateFormatter.dateFromString("2017-01-10")!
let cal = NSCalendar.currentCalendar()
let dayUnit: NSCalendarUnit = .Day
if startDate.compare(endDate) == NSComparisonResult.OrderedDescending{
let components = cal.components(dayUnit, fromDate: endDate, toDate: startDate, options: [])
print(components)
} else {
let components = cal.components(unit, fromDate: startDate, toDate: endDate, options: [])
print(components)
}
As Describe by Leo Dabus
You can do it without comparing the dates also:
let com = cal.components(dayUnit, fromDate: startDate.earlierDate(endDate), toDate: startDate.laterDate(endDate), options: [])
print(com)

Swift Core Data Fetch results from a certain date

I'm fairly new to ioS dev so this might be obvious.
With Core Data, I have an Entity : 'Post' , with an attribute for "dateAdded".
When the user selects an NSDate on a calendar- I want to fetch all the entries on that day only and load that into a table.
I was thinking of using a predicate where dateAdded (NSDate) would be equal to the calendarSelectedNSDate. But I don't think that would work as their NSDates would be different because of different times.
I would really appreciate a solution! Thank you!
Use (NS)Calendar to calculate the start and end of the selected date and create a predicate:
// get the current calendar
let calendar = Calendar.current
// get the start of the day of the selected date
let startDate = calendar.startOfDay(for: calendarSelectedNSDate)
// get the start of the day after the selected date
let endDate = calendar.date(byAdding: .day, value: 1, to: startDate, options: .matchNextTime)!
// create a predicate to filter between start date and end date
let predicate = NSPredicate(format: "dateAdded >= %# AND dateAdded < %#", startDate as NSDate, endDate as NSDate)

check value existence by NSDate as key in dictionary

I have a dictionary like this:
var dic = [NSDate: Int]()
it is used in my iOS to-do app to get the number of finished tasks of a particular date. I only care about the year, month and day sections in NSDate and also want to be able to get the number of tasks in a particular date using this dictionary, how can I do that? thanks.
Instead of storing your date as NSDate in your dictionary you can save it as String so that comparison will be easier. Use following code to store it as a string
func dateFromString(date : NSDate) -> String {
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
return dateFormatter.stringFromDate(date)
}
You can pass NSDate() to above function and it will give you string containing only year, month and date. For retrieving your data from dictionary use following.
func dateFrom(year:Int, month:Int, day:Int) -> String {
let components = NSDateComponents()
components.year = year
components.month = month
components.day = day
let gregorian = NSCalendar(identifier:NSCalendarIdentifierGregorian)
let date = gregorian!.dateFromComponents(components)
return dateFromString(date!)
}
You can pass year, month and date to above function and it will return corresponding date in string format. So your dictionary operations will look like
dict[dateFromString(NSDate())] = 1 //for insertion or updation
let numOfTasks = dict[dateFrom(2016, month: 1, day: 15)] //to get task for any particular day
EDIT
If you want to proceed with NSDate as key for your dictionary then you'll have to modify above code as follows. dateFrom will return date with year,month and date of your choice, and time will be some constant value. Time will be set to midnight in your current time zone if you don't set it.
func dateFrom(year:Int, month:Int, day:Int) -> NSDate {
let components = NSDateComponents()
components.year = year
components.month = month
components.day = day
let gregorian = NSCalendar(identifier:NSCalendarIdentifierGregorian)
let date = gregorian!.dateFromComponents(components)
return date!
}
And for getting current date use following so that you store date object with current year, date, month and time to some constant value.
func getCurrentDate()->NSDate {
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Day , .Month , .Year], fromDate: date)
return dateFrom(components.year, month: components.month, day: components.day)
}
Usage will be as follows
dict[getCurrentDate()] = i //for insertion or updation
let numOfTasks = dict[dateFrom(2016, month: 1, day: 15)] //to get task for any particular day

Resources