Is it possible to convert days to months and days?
این محتوا هنوز به زبان شما در دسترس نیست.
💡 Struggling with date calculations and algorithms? Need help with complex math logic? 🚀 Get Math Help
LibraryDev_Chris
Posted on July 27, 2025 • Intermediate
📚 Date Conversion for Library System
Hi everyone! I’m building a library management system in Scratch and I’m stuck on the due date calculation. My original script for calculating “2 months and 15 days” broke, so now I’m trying a different approach.
I need to convert a number of days (like 75 days from July 27th) into a readable date format (like October 10th). This is for calculating when books are due back.
Example: If someone borrows a book on July 27th and it’s due in 75 days, what date would that be?
Any help with the math/algorithm would be super appreciated! 🙏
DateMaster_Pro
Replied 30 minutes later • ⭐ Best Answer
Great question @LibraryDev_Chris! Date calculations can be tricky, but I’ll show you a complete solution that handles leap years and all the edge cases:
📅 Date Conversion Algorithm
Here’s how the date calculation process works:
🗓️ Step 1: Month Length Arrays
First, create lists to store the number of days in each month:
when flag clicked delete all of [Month Days v] delete all of [Month Names v] // Days in each month (non-leap year) add [31] to [Month Days v] // January add [28] to [Month Days v] // February add [31] to [Month Days v] // March add [30] to [Month Days v] // April add [31] to [Month Days v] // May add [30] to [Month Days v] // June add [31] to [Month Days v] // July add [31] to [Month Days v] // August add [30] to [Month Days v] // September add [31] to [Month Days v] // October add [30] to [Month Days v] // November add [31] to [Month Days v] // December // Month names for display add [January] to [Month Names v] add [February] to [Month Names v] add [March] to [Month Names v] add [April] to [Month Names v] add [May] to [Month Names v] add [June] to [Month Names v] add [July] to [Month Names v] add [August] to [Month Names v] add [September] to [Month Names v] add [October] to [Month Names v] add [November] to [Month Names v] add [December] to [Month Names v]
🧮 Step 2: Leap Year Detection
Create a function to check 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 [Is Leap v] to [true] else if <((year) mod [400]) = [0]> then set [Is Leap v] to [true] else set [Is Leap v] to [false] end end // Update February days for leap years if <(Is Leap) = [true]> then replace item [2] of [Month Days v] with [29] else replace item [2] of [Month Days v] with [28] end
📊 Step 3: Main Date Calculation Function
Here’s the core algorithm that converts days to a date:
define add days (start month) (start day) (start year) (days to add) // Initialize variables set [Current Month v] to (start month) set [Current Day v] to (start day) set [Current Year v] to (start year) set [Remaining Days v] to (days to add) // Update leap year status is leap year (Current Year) repeat until <(Remaining Days) = [0]> // Get days in current month set [Days In Month v] to (item (Current Month) of [Month Days v]) // Calculate days left in current month set [Days Left In Month v] to ((Days In Month) - (Current Day)) if <(Remaining Days) > (Days Left In Month)> then // Move to next month change [Remaining Days v] by (0 - (Days Left In Month)) change [Remaining Days v] by [-1] // Account for moving to day 1 set [Current Day v] to [1] change [Current Month v] by [1] // Handle year rollover if <(Current Month) > [12]> then set [Current Month v] to [1] change [Current Year v] by [1] is leap year (Current Year) // Update leap year status end else // Final day in current month change [Current Day v] by (Remaining Days) set [Remaining Days v] to [0] end end // Format the result set [Result Date v] to (join (item (Current Month) of [Month Names v]) (join [ ] (join (Current Day) (join [, ] (Current Year))))) say (Result Date) for [3] seconds
🎯 Step 4: Easy-to-Use Interface
Create a simple interface for your library system:
when flag clicked ask [Enter starting month (1-12):] and wait set [Start Month v] to (answer) ask [Enter starting day:] and wait set [Start Day v] to (answer) ask [Enter starting year:] and wait set [Start Year v] to (answer) ask [Enter number of days to add:] and wait set [Days To Add v] to (answer) // Calculate the new date add days (Start Month) (Start Day) (Start Year) (Days To Add) // Display result say (join [Due date: ] (Result Date)) for [5] seconds
📚 Step 5: Library-Specific Features
Add some library-specific functionality:
// Preset loan periods define set loan period (book type) if <(book type) = [regular]> then set [Loan Days v] to [14] // 2 weeks else if <(book type) = [reference]> then set [Loan Days v] to [7] // 1 week else if <(book type) = [dvd]> then set [Loan Days v] to [3] // 3 days else set [Loan Days v] to [21] // 3 weeks for special items end end end // Calculate due date with current date define calculate due date (book type) set loan period (book type) set [Today Month v] to [7] // July set [Today Day v] to [27] // 27th set [Today Year v] to [2025] // 2025 add days (Today Month) (Today Day) (Today Year) (Loan Days) say (join [Book due: ] (Result Date)) for [3] seconds
🔍 Example Usage
For your specific example (75 days from July 27, 2025):
when space key pressed // July 27, 2025 + 75 days add days [7] [27] [2025] [75] // Result: October 10, 2025
This system handles all the tricky parts like leap years, month boundaries, and year rollovers automatically! Perfect for any library or date-based system! 📖
LibraryDev_Chris
Replied 1 hour later
@DateMaster_Pro This is absolutely perfect! 🎉 I tested it with July 27 + 75 days and got October 10th exactly as expected!
The leap year handling is brilliant - I never would have thought of that. My library system is now working flawlessly. Thank you so much!
One quick question: How would I modify this to also show the day of the week (like “Monday, October 10, 2025”)?
DateMaster_Pro
Replied 30 minutes later
@LibraryDev_Chris Great question! Here’s how to add day-of-week calculation:
// Day of week calculation (Zeller's Congruence) define get day of week (month) (day) (year) // Adjust for Zeller's formula (Jan/Feb count as months 13/14 of previous year) if <(month) < [3]> then change [month v] by [12] change [year v] by [-1] end set [q v] to (day) set [m v] to (month) set [K v] to ((year) mod [100]) set [J v] to ([floor v] of ((year) / [100])) // Zeller's formula set [h v] to (((q) + ([floor v] of (((13) * ((m) + [1])) / [5]))) + (K) + ([floor v] of ((K) / [4])) + ([floor v] of ((J) / [4])) + ((5) * (J))) mod [7]) // Convert to day names (0=Saturday, 1=Sunday, etc.) delete all of [Day Names v] add [Saturday] to [Day Names v] add [Sunday] to [Day Names v] add [Monday] to [Day Names v] add [Tuesday] to [Day Names v] add [Wednesday] to [Day Names v] add [Thursday] to [Day Names v] add [Friday] to [Day Names v] set [Day Name v] to (item ((h) + [1]) of [Day Names v])
Now you can format dates like “Monday, October 10, 2025”! 📅
Vibelf_Community
Pinned Message • Moderator
📊 Advanced Date & Time Programming
Excellent discussion on date algorithms! For those building more complex time-based systems, explore these advanced topics:
- 🕐 Time zone conversions
- 📅 Calendar system implementations
- ⏰ Scheduling and reminder systems
- 📈 Time-based data analysis
- 🌍 International date formats
📚 Related Algorithm Topics
Ready to master complex algorithms and mathematical programming? Get personalized tutoring from our expert instructors!