Date and Arrays

  • Hey friends,

    I'm making an app to be used on both Android and IOS. I have an array. People's personal information is stored in the array.

    In the 3rd column are people's dates of birth. When I start a layout I want to calculate the number of days till each person's birthday, and load that number into the 4th column of the array. How do I do this?

    I've been trying to work it out, but I'm stuck. I've found two different ways of working out the date; browser based, and the date object. I'm studying this capx which I basically understand, and this capx which I'm really struggling to understand.

    How do I do this? And consequently, what is the best method for determining the date in my scenario?

    Any help deeply appreciated, thanks so much everyone x

  • I think the second example you are looking at is doing WAY more then is necessary. I would suggest you ignore that one. All you need to get the date difference is this:

    int(Date.ToTotalHours(abs(Date.Difference(Date.Parse(Date.ToDateString(Date.Now)), Date.Get(year, month, day, hr, min, sec, mil))) ÷ 24)

    The first date, "Date.Parse(Date.ToDateString(Date.Now))" gets the current date without time. That way the time of day won't mess up the difference function.

    The second date is where you pass in the birthdate. For the year, use the year of their next upcoming birthday. If their birthday this year has passed, use next year. If not, use this year. For everything after day, you can just pass in 0.

    To break it down, the date difference function returns the difference between the 2 dates in milliseconds. Abs so we don't get a negative value. the TotalHours function then gives us the millis in hours so we divide by 24. Finally, we pass it into the int function to just get the integer value representing days.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Hey InDWrekt this is really helpful thank you! I'm finding dates in Construct 3/Java to be so confusing. Thanks for helping me understand better.

    So I took your code and added the date of my next birthday into it. (I also added an extra bracket at the end because it was returning a syntax error). This is the code:

    int(Date.ToTotalHours(abs(Date.Difference(Date.Parse(Date.ToDateString(Date.Now)), Date.Get(2021,11,22, 0, 0, 0, 0)))/24))

    However for some reason, this is returning 387 days. Which is more days than are in a year. Unsure what's going wrong here. Any advice would be really appreciated

  • int(Date.ToTotalHours(abs(Date.Difference(Date.Parse(Date.ToDateString(Date.Now)), Date.Parse(right(BDay.Text, 11))))) / 24)

    Try this again, when I copied over the code and removed my project specific stuff, I must have messed up the parens. Here is the code the way I have it in my project. As you can see, there are more parens before the /24. In yours, you put it after. That means you are dividing my 24 too early and it throws off the result.

    In my example project I am using Date.Parse(right(BDay.Text, 11)) to get the birthday info from a text object. This is no different than using the Date.Get(2021,11,22, 0, 0, 0, 0)) method.

    Here is a link to the example I built to figure out how to solve your problem:

    drive.google.com/file/d/1njkOW5E93Z6dAKfrj2u2FB2pDBqoCkmo/view

  • My answer is very wordy. I think all you need to do is move the last parens before the /24.

  • Except, I was just playing with your code and realized 11 doesn't mean November. It means December. Months are 0 based meaning January is 0 and it goes up 1 from there. That may be the one thing causing you the most grief. In programming, it is most common to start counting with 0, not 1. In cases like month in the date object, it can get confusing if you aren't used to this.

  • This looks like it works better:

    Date.ToDateString(Date.parse("11/22/2021"))

    I am available on Discord to chat for a while. discord.gg/48PjzKZW.

    The more I play with it, the more I see things going wrong with the get. Not sure if they are bugs but I can help you work through it if you would like.

  • Haha, okay I see what I've done. So I knew that the months began at 0, but I just added instead of subtracting when I input the month I wanted (October). I've now fixed the brackets and changed the month and it's working great. Thanks so much for your help friend.

    Now I just need to work out a way to generate everyone's next birthday, by using their dates of birth. Just so I don't have to keep changing dates manually.

    If you have any knowledge on that it would be really appreciated, but totally okay if not, you've already helped me loads x

  • I modified the example project with the changes we discussed on Discord. Hopefully seeing them in action will help you finish your project. Good luck.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)