|
So I've spent the last three or four days using most of my free time to try and find an answer to this dilema I'm having. Simply put, Dates are hard. I've looked over a lot of Hudson's documentation on dates, and I've referenced Apples documentation as well. I've looked at various YouTube channels, and I've scoured Stack for help. I need to compare two dates based on a bool, with complexity. If the user is over a certian age, then the length of time between the users input date and the constant date is 24 months. If the user is under a certian age, the constant is 60 months. I need to record the exact day of the users input. This could be the fifth of the month, or the tenth, or the twenty-first. The constant date must always end at the end of the calendar month 24 or 60 months following the users input date. So: The user's certificate was issues on November 1st, 2023. The expiration of the date, if the bool is false, is November 30th, 2025 (the last calendar day of the month). In similar fashion, the bool is a variable based on the users birthdate. I'm comfortable comparing specific constant dates and variables, but I can't quite figure out how to ensure that the specific constant I'm asking for is always the last day of the month two or five years following the users input. Basically, how do I always ensure that shows the input month, at the last day of that month? Anybody have any ideas? I hate dates, and am struggling to learn this part specifically. Thanks, Andy |
|
|
|
Hi Make a new swift file (I called mine
Made a small project to show how top use
If you make the date of birth to be older then 18 (or 21 in second label) then you will see only 24 months added otherwise it 60 months from the Sign up date. |
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts! Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
50 points to the DoubleDecker team for all the excellent comments! Nice explanations. -5 points for "inertial date". (initial? initial date?) 😜 PS: Nigel, I like adding the following
See -> Adding Code Markers |
|
Thank you
Because I have added the methods comment if you option+click on call site you will get the summary and the file name. |
|
Thank you @eoinnorris and @NigelGee for the great feedback and examples. Unfortunately, in the 100 days courses it seems that dates are not covered too in depth, or I should say in depth enough for what it is I'm trying to learn. On that note, does anyone have any suggestions as to what books would cover dates the best? I'm not in a position to buy any of the bundles, but dates are going to be a pivotal part of my projects going forward and I really need to learn the better. For another roadblock I'm experiencing: After calculating the dates, I'm looking to save them in UserDefaults. There's no identifying information (no names, no addresses, no phone numbers, just the birthdate for the sake of validating checkride readiness and medical qualification) saved in the app other than the users birthdate so I'm not terribly concerned about safety. I've tried a few methods to save selections out of a date picker, but I can't get the date picker to run a function to convert the date to a string for storage to AppStorage. What am I doing wrong here? .onCommit and .onSubmit don't work here, and attempting to write it like the following yeilds an error: "Static method 'buildExpression' requires that 'Text' conform to 'TableRowContent'"
I've also tried using didSet on the variable itself, but can't get anything to print in my console indicating that the value was set...
How do you run a function when the user selects a new date in the picker? This seems easy and I feel stupid for having to ask it. |
|
Firstly I do not know anyone that does a book on just dates, however if you search the Forum and type "dates" you will get alot of feed back, there are alot of question (as dates are hard!). Also if you get stuck then there is always here. I tend to uses the Secondly
However this will not work in
Then add
Here the final sample project
|
|
@NigelGee, thank you very much for taking the time to reply, and the knowledge you've shared. I've poured through the knowledge database and looked back through several projects, but it seems like the dates aspect is sort of piecemealed through the course. I'll take a look at that live stream tomorrow, and I'll resume working on the project tomorrow as well. I have today off with my daughter, and since I travel for work, it's always important to me that I make the most of these days 😊 |
|
Dealing with date calculations can indeed be challenging, but with the right approach and libraries, you can simplify the process. It sounds like you're working with a programming language. I'll provide a high-level approach using a hypothetical programming language, but you'll need to adapt this to the specific language you're working with. |
|
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.