Date Calculation in Scratch - Adding Days and Converting Formats
Esta página aún no está disponible en tu idioma.
📅 Need help with date calculations and formatting in Scratch? 🚀 Get Math Help
DateCalc_Pro
Posted on July 29, 2025 • Intermediate
📚 Library System Date Calculation Help
I’m building a library checkout system in Scratch and I need to calculate return dates. When someone checks out a book, I want to automatically calculate the date 75 days from today and display it in a nice format.
For example:
- If today is August 1st, the return date should be around November 15th
- I need it formatted as “Month Day, Year” (like “November 15, 2025”)
- It should handle month boundaries and leap years correctly
I’ve been struggling with the math for converting days across months. Does anyone know how to do this properly in Scratch? 🤔
AlgorithmMaster_Jake
Replied 2 hours later • ⭐ Best Answer
Great question @DateCalc_Pro! Date calculation is tricky but totally doable in Scratch. Here’s a complete solution for your library system:
📅 Date Calculation Flow
Here’s how we’ll approach the date calculation:
🔢 Step 1: Set Up Date Variables
First, create variables to store date information:
when flag clicked // Current date variables set [current year v] to [2025] set [current month v] to [8] // August set [current day v] to [1] // Days in each month (non-leap year) set [days in months v] to [31,28,31,30,31,30,31,31,30,31,30,31] // Month names for formatting set [month names v] to [January,February,March,April,May,June,July,August,September,October,November,December]
📊 Step 2: Convert Date to Days Since Reference
Convert the current date to total days since a reference point (like year 2000):
define convert date to days (year) (month) (day) // Calculate days from years set [total days v] to (((year) - [2000]) * [365]) // Add leap days set [leap years v] to [0] set [check year v] to [2000] repeat until <(check year) = (year)> change [check year v] by [1] if <(is leap year (check year)) = [true]> then change [leap years v] by [1] end end change [total days v] by (leap years) // Add days from months in current year set [month counter v] to [1] repeat until <(month counter) = (month)> if <(month counter) = [2]> then if <(is leap year (year)) = [true]> then change [total days v] by [29] else change [total days v] by [28] end else change [total days v] by (item (month counter) of [days in months v]) end change [month counter v] by [1] end // Add remaining days change [total days v] by (day)
🗓️ Step 3: Check for Leap Years
Create a function to determine if a year is a leap year:
define is leap year (year) if <<((year) mod [4]) = [0]> and <not <((year) mod [100]) = [0]>>> then set [leap year result v] to [true] else if <((year) mod [400]) = [0]> then set [leap year result v] to [true] else set [leap year result v] to [false] end end
➕ Step 4: Add Days and Convert Back
Add 75 days and convert back to a readable date:
define add days to date (days to add) // Convert current date to total days convert date to days (current year) (current month) (current day) // Add the specified days change [total days v] by (days to add) // Convert back to year/month/day convert days to date (total days) // The result will be in [result year], [result month], [result day]
define convert days to date (total days) set [remaining days v] to (total days) set [result year v] to [2000] // Find the year forever if <(is leap year (result year)) = [true]> then set [days in year v] to [366] else set [days in year v] to [365] end if <(remaining days) > (days in year)> then change [remaining days v] by ((-1) * (days in year)) change [result year v] by [1] else stop [this script v] end end // Find the month set [result month v] to [1] forever if <(result month) = [2]> then if <(is leap year (result year)) = [true]> then set [days in current month v] to [29] else set [days in current month v] to [28] end else set [days in current month v] to (item (result month) of [days in months v]) end if <(remaining days) > (days in current month)> then change [remaining days v] by ((-1) * (days in current month)) change [result month v] by [1] else set [result day v] to (remaining days) stop [this script v] end end
📝 Step 5: Format the Date
Create a nicely formatted date string:
define format date (year) (month) (day) set [month name v] to (item (month) of [month names v]) set [formatted date v] to (join (join (join (month name) [ ]) (day)) (join [, ] (year))) // Example output: November 15, 2025
🎯 Step 6: Put It All Together
Here’s how to use everything for your library system:
when [checkout book v] clicked // Get current date (you might get this from sensing blocks or user input) set [current year v] to [2025] set [current month v] to [8] set [current day v] to [1] // Calculate return date (75 days later) add days to date [75] // Format the result format date (result year) (result month) (result day) // Display to user say (join [Return by: ] (formatted date)) for [3] seconds // You could also set this to a variable for storage set [book return date v] to (formatted date)
🚀 Advanced Features
You can extend this system with additional features:
Get Current Date Automatically:
when flag clicked // Get current date from sensing set [current date string v] to (current [date v]) // Parse the date string to extract year, month, day // This depends on your locale format
Different Loan Periods:
define calculate return date (book type) if <(book type) = [regular]> then add days to date [14] // 2 weeks else if <(book type) = [reference]> then add days to date [7] // 1 week else add days to date [75] // Special collection end end
Overdue Calculation:
define days overdue (return year) (return month) (return day) convert date to days (current year) (current month) (current day) set [current total days v] to (total days) convert date to days (return year) (return month) (return day) set [return total days v] to (total days) set [overdue days v] to ((current total days) - (return total days))
This system handles leap years, month boundaries, and gives you a solid foundation for any date-related calculations in your library system! 📚✨
DateCalc_Pro
Replied 1 hour later
@AlgorithmMaster_Jake This is exactly what I needed! 🎉 The step-by-step breakdown makes it so much easier to understand.
I implemented it in my library system and it works perfectly. The leap year handling was the part I was missing. Thank you!
MathTeacher_Lisa
Replied 45 minutes later
Excellent explanation @AlgorithmMaster_Jake! 👏 This is a great example of breaking down a complex problem into manageable steps.
For students learning this concept, I’d recommend:
- Start simple: Try adding just a few days first
- Test edge cases: End of month, end of year, leap years
- Use debugging: Add “say” blocks to see intermediate calculations
- Understand the math: Learn why leap years exist and how they work
This kind of algorithm thinking is valuable beyond just Scratch! 🧮
Vibelf_Community
Pinned Message • Moderator
🧮 Master Advanced Math and Algorithms
Great discussion about date calculations! For those interested in learning more advanced mathematical concepts in programming:
- 📅 Calendar algorithms and date arithmetic
- 🔢 Number theory and modular arithmetic
- ⏰ Time zone calculations and conversions
- 📊 Statistical calculations and data analysis
📚 Related Topics
- Building calendar applications in Scratch
- Mathematical algorithms for beginners
- Time and date manipulation techniques
Ready to dive deeper into mathematical programming? Get expert guidance from our math and computer science tutors!