|
In SwiftUI, all I'm looking to do is get the difference between two dates -- as in, 'here is the current date' and 'here is August 5th, 1992' for example, and therefore the number of days, months, years, between those two dates is x number of days, x number of months, and x number of years. This seems relatively straightforward, and yet I can't find any reasonable way to get an accurate answer. I can output dates like the two below, but I can't figure out how to get the difference between them. Is there a simple-ish solution? 2017-08-06 19:20:42 +0000 2020-08-06 19:20:46 +0000 |
|
Use
The result of this code is:
|
|
this is excellent, thank you. I was able to arrive at that same result a different way, but the main thing I'm wondering is, how do you also display that in terms of days? in terms of months? ex. 3 years, 3 months, 15 days between two dates is also, for example, 964 days, or 39 months, or something like that -- essentially the way this website acts:https://www.timeanddate.com/date/duration.html that's the part I really can't figure out!! |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
From Paul's Working with dates
You need to work out what you what to display. This is very good from the WWDC 2020 Formatters: Make data human-friendly. Hope that helps. Also if you subscribe to Hacking with Swift Plus. This is good Using dates safely and effectively |
|
You do this by varying what you pass in as the first parameter to So, for instance, to get the difference between the same two dates but in days, you would do this:
Or this code to get thew diff in hours and minutes:
I forgot to mention this earlier, but you can access the properties of the diff result individually as well:
Note that they are optionals so you have to deal with that. |
|
Hi guys. Great topic! Follow-up question: How do you list the days as dates in a range of dates? like this: let now = Date() let futureDay = now.addingTimeInterval(72 * 3600) let dates = DateInterval(start: now, end: futureDay) I'd like to see if there was some built-in trick to outputting an array like arrayOfDays: [Date] = [28-10-2020, 29-10-2020, 30-10-2020] from that DateInterval The use case is such: I'm asking the user to set a date range from A to B. Then I want to take alle the dates in that range, between A and B and present the user with a list of his/her chosen dates, and then add some data to those days, like a diary. I've tried this a zillion ways, without luck. |
|
You can do something like this:
|
|
And playing around a bit more in a playground:
|
|
.enumerateDates !!! Thats what I was looking for :D @roosterboy, you're a star mate! Ok.. give an inch, take a mile. I have a follwo-up question that's been wrecking my brain for a few hours. Searching has yielded nothing, so I'll give the best forum for Swift a go. ;) EDIT: I decided to create a short video to try to describe a question that I have, and hopefully it could help others struggling with the same question. I put it on YouTube :) I'm using a DatePicker in a view that I'm showing in a sheet. I want to let my users pick a from and to date. I'm trying to store this in an @Published variable contained within a class that conforms to @Observableobject, and the variable that I'm using to store it in is called as an @Observedobject.. But when I call up my sheet with the datepicker (and I'm using the binding) the picked value isn't stored in the variable 🤷🏻♂️ Any ideas? |
|
This works for me:
The properties in the Is this similar to what you're doing? |
|
Thanks so much for the thorough replies @roosterboy I figured what it was, in the. end. It was a silly mistake that I don't think will be much help to anyone 😅 However, I hope that someone looking for the solution to the .enumerateDates will find what they're looking for. This is a good forum 💪 |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.