Calculating "days left" between two dates
-
Hi everyone,
I am currently building a 14-day demo mode into my plugin.
Is there a quick way to calculate "days left" between two dates in this format: 20240326?I am trying to avoid feeding every month, and the number of days for each month in there, and then calculating everything separately for the year, month, and days.
Of course, if there is no other choice, I will do it, but if there is a more elegant way, that would be awesome. I could not find anything in the API for this task.
Thanks a lot!
Gábor -
I have a video on Patreon that shows this exact thing
-
@d-healey Oh, great! Thanks a lot!
-
@Gabor-K Engine.getSystemTime() returns a standardised format string that you can do string operations on.
You might also want to consider calling with get some of the online clocks, as system time can be manipulated by simply rewinding the system clock.
-
@aaronventure I think the date class is better for this use case.
-
@d-healey I think the format is the same, the date class has some methods with calculations already done. OP will still have to do string operations. The most interesting one is to find out the day of the year.
inline function getDayOfYear() { // Get the current time in ISO 8601 format without milliseconds local dateTimeISO8601 = Date.getSystemTimeISO8601(false); // Extract year, month, and day from the ISO 8601 format local year = parseInt(dateTimeISO8601.substring(0, 4)); local month = parseInt(dateTimeISO8601.substring(4, 6)); local day = parseInt(dateTimeISO8601.substring(6, 8)); // Initialize an array to hold the number of days for each month local daysInMonth = [31, isLeapYear(year) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; // Calculate the day of the year local dayOfYear = day; for( i = 0; i < month - 1; ++i) // Subtract 1 because array is zero-based and months are 1-based { dayOfYear += daysInMonth[i]; } return dayOfYear; } // Function to check if a year is a leap year inline function isLeapYear(year) { return (year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)); }
-
@aaronventure said in Calculating "days left" between two dates:
The most interesting one is to find out the day of the year.
I agree it's interesting to do, instead of substring you can probably just use split on the
-
character. But why does he need to know the day?This is my update checker function. It checks if 7 days have passed since the last check and if so it performs a new check and updates the saved timestamp - https://codeberg.org/LibreWave/Rhapsody/src/branch/main/Scripts/UpdateChecker.js#L95
-
I have a video on Patreon that shows this exact thing
@d-healey LOL of course you do.