1

So i have a really complex system i wrote for my teacher it does a bunch of math that he had in a spreadsheet that finds out basically how long it takes someone to save up enough money to cover an asset of there. Long story short i currently have it saying for example "You will receive enough money to cover this asset in 0.38 months"

I would like to be as user friendly as possible, i dont care if i have to come up with a math function for each month. My question is, how would i spit out 0.39 Month to Weeks and Possibly if i can Days (which would actually make more sense because i can make a script that says if greater then 7 then that is equal to 1 week) to make it even more precise. I know for starters i need to know how many days are in each month, not every month is the same as we all know. Any starting point after that would be great. Im jQuery/JS for this math functions to this application.

NodeDad
  • 1,519
  • 2
  • 19
  • 48
  • 1
    Umm... Not a duplicate? Thats not what im looking for at all dude. Im talking about converting a non whole number into days – NodeDad Mar 24 '13 at 21:25
  • 2
    Then, assuming there are 30 days in each month `months * 30` = `days` and `Math.floor((months*30)/7)` ~= `weeks` – Jon Mar 24 '13 at 21:29
  • 1
    But there are not 30 days in each month so how would i do what your talking about with an array of the months eg: `January = 31; February = 28; March = 31; April = 30 May = 31; June = 30; July = 31; August = 31 September = 30; October = 31; November = 30; December = 31` – NodeDad Mar 24 '13 at 21:34
  • 2
    `numDays = new Date(year, month, 0).getDate();` putting in the year and the month that you are wanting to know about. – Jon Mar 24 '13 at 21:37
  • THanks, i'll put this together and see what i come up with, Thanks @Jon – NodeDad Mar 24 '13 at 21:38

1 Answers1

2

To put my comments in answer form:

function getDays(numMonth, month, year) {
    var numDays = new Date(year, month, 0).getDate();
    return numMonth * numDays;
}

function getWeeks(numMonth, month, year) {
    var days = getDays(numMonth, month, year);
    return Math.floor(days / 7);
}

This is where numMonth = 0.39, etc.

Jon
  • 4,746
  • 2
  • 24
  • 37
  • Ahhh thank you so much, i was trying to put it into a statement and couldn't get it to work. I had forgotten the `return` – NodeDad Mar 24 '13 at 22:21
  • Also that other link you said was a duplicate did in fact actually help me out for the second part of this, so thank you for that as well. Helped me figure out how to actually use the correct months between two given points. heres that again for anyone who needs it in the future http://stackoverflow.com/questions/14135818/javascript-months-and-days-between-dates – NodeDad Mar 24 '13 at 22:25