简体   繁体   English

javascript创建专业的额定价格计算器

[英]javascript create pro rated price calculator

I am trying to create a small js app to display the pro-rated price of a monthly subscription according to the current date. 我正在尝试创建一个小型js应用,以根据当前日期显示每月订阅的按比例价格。 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. 我对javascript非常陌生,不知道如何超越今天的日期。

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);

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. 它把明天视为第一天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM