Reputation: 74
I am trying to create a small js app to display the pro-rated price of a monthly subscription according to the current date. this is the plain english code that I have:
{
get todays date
check which month we are in
according to the month, calculate days remaining in the month
divide the price per month by total days in current month to get daily price
multiply daily price by days remaining in the current month
display pro-rated price
};
I am very new to javascript and have no clue how to go beyond getting todays date.
Upvotes: 0
Views: 2260
Reputation: 12032
This is a simpler version that just looks at the number of days. It counts today
function getProrate(monthlyPrice){
var today = new Date();
var totalDays = new Date(today.getFullYear(), today.getMonth(), 0).getDate();
var daysLeft = (totalDays - today.getDate()) - 1;
var pricePerDay = monthlyPrice/totalDays;
return daysLeft * pricePerDay;
}
It counts the tomorrow as the first day.
Upvotes: 1
Reputation: 386
this code may help you
function code(pricepermonth){
var dt = new Date();
month = dt.getMonth();
day = dt.getDate();
year = dt.getFullYear();
if(month == 11)
var nextMonth = new Date(year+1,0,1);
else
var nextMonth = new Date(year,month+1,1);
var today = new Date(year,month,day);
var remain = (nextMonth.getTime() - today.getTime())/1000;
remain = remain/(60*60*24);
totaldays = day+remain;
var priceperday = pricepermonth/totaldays;
var remainingprice = priceperday*remain;
alert(remainingprice);
return remainingprice;
}
var pricepermonth = 50000;
code(pricepermonth);
Upvotes: 3