Learn Courses My Dashboard

Days Between Days

Hello, I’m trying to calculate days between dates, and I thought it was just ‘1’ off for some of them, but maybe it takes time into account?

I just want days so, tomorrow is “1” day away. Yesterday is -1 days away, today is 0 days away.

but, it seems today =1 and tomorrow =1 in this thing. any ideas? thanks

let startDate = Date()
let eventDateRaw = 2021-11-16 05:00:00 +0000)
       
let dayDiffs = Calendar.current.dateComponents([.day], from: startDate, to: eventDateRaw!)

PRINT OUTPUT
Optional(“Today 2021”)
Optional(2021-11-16 05:00:00 +0000)
1
Optional(“Today + 1”)
Optional(2021-11-17 05:00:00 +0000)
1

Both values return “1”.

I don’t even want to store the time, but it looks like it makes you. I don’t enter it, something is picking 5:00 AM for me, which is my only guess as to why it’s not working how I’d like.

days that are not today, seem to work fine.

thanks for any help!

Maybe it’s not time, well, kinda.

Looks like Date() for me is 2021-11-16 16:22:00 +0000

but, it’s 11:22 AM here. so -5 time zone. maybe the issue is that. But I’d like the app to work in any timezone, since really I just want the date.

maybe strip the time from Date() before feeding it to the dayDiff line?

I’ll play with it and see what happens :slight_smile:

Please post all of the relevant code when asking for help. What is eventDateRaw?

This works for me:

let startDate = Date()
let endDate = Calendar.current.date(byAdding: .day, value: -1, to: startDate)!
let daysDiff = Calendar.current.dateComponents([.day], from: startDate, to: endDate)
print(daysDiff.day!)
//returns -1
1 Like

Sorry, still new to this.

It’s part of a loop I have for displaying the data in a table.

2021-11-16 05:00:00 +0000)

and

2021-11-17 05:00:00 +0000)

for both values, it returns “1” (in my app)

for the 11/16/2021 I’d expect it to return “0”

When I try it playground, I currently get: Can’t assign value of type string to date, I assume because I don’t have the format correct.

thanks again


import UIKit

let startDate = Date()
// var endDate = Date()

endDate = "2021-11-16 16:22:00 +0000"  // I know this is string, but not sure how to format for date. Thanks


let dayDiffs = Calendar.current.dateComponents([.day], from: startDate, to: endDate)

print(startDate)
print(endDate)
print(dayDiffs)

Currently I’m prompting for a date with a text field, which I want to replace with a datepicker. I just don’t know how to do that yet, as I’ve only been doing this for a few weeks.

Maybe if I did that first I wouldn’t have to hassle with dates vs. strings.

Should have time to play with datepicker tomorrow.

What output do you get for this code:

let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd HH:mm:ssZ"


extension Date {
    var startOfDay: Date {
        Calendar.current.startOfDay(for: self)
    }
}

let startDate = Date().startOfDay
let endDate = formatter.date(from: "2021-11-16 16:22:00 +0000")!.startOfDay

let dayDiffs = Calendar.current.dateComponents([.day], from: startDate, to: endDate)

print(startDate)
print(endDate)
print(dayDiffs)
1 Like

2021-11-16 05:00:00 +0000
2021-11-16 05:00:00 +0000
day: 0 isLeapMonth: false

thanks. I’ll play around with this and see what happens. Thank you much.

if I understand correctly, you’re forcing ‘start of day’. Which should fix the time issues.

This is so fun, I sat with my laptop for almost 3 hours yesterday before realizing the time.

I was able to get this in my original code, and was also able to remove a bunch of lines that no longer applied. thanks again!

I didn’t force end of day on the 2nd date, if it’s stored with only that value when created, it shouldn’t be needed, but then I remember to assume all input is hostile.

You might want to check out this set of documents:

I’m still reading it, but it looks like you can learn anything you ever wanted to know about dates and times.

1 Like