javascript - 如何比较日期并执行以下代码?
问题描述
如果购买日期在 31/Mar/xxxx 之后,则不应计算elap_yend、rem_days、depre_cur、cur_wdv。
此外,我必须使用一些选项计算 GST,即如果选择了SGST 和 CGST,我不应该计算 IGST,否则如果选择或标记了 IGST,它不应该计算 SGST 和 CGST。
我该如何解决这个问题?
<body>
<div class="form-group mx-auto" >
<div class="form-row " >
<div class="form-group col-md-2">
<label>Purchase Date</label>
<input type="date" class="form-control" id="pur_date" name="pur_date" onchange="cal()" required/>
</div>
<div class="form-group col-md-2">
<label>End Date</label>
<input type="date" class="form-control" id="end_date" name="end_date" onchange="cal()"required/>
</div>
</div>
<div class="form-row " >
<div class="form-group col-md-2">
<label>Product Price</label>
<input type="text" class="form-control" id="product_price" name="product_price" placeholder="Enter Amount" required/>
</div>
<div class="form-group col-md-2">
<label>IGST</label>
<input type="text" class="form-control" id="igst" name="igst" required/>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-2">
<label>SGST</label>
<input type="text" class="form-control" id="sgst" name="sgst" required/>
</div>
<div class="form-group col-md-2">
<label>CGST</label>
<input type="text" class="form-control" id="cgst" name="cgst" required/>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-2">
<label>Total Amount</label>
<input type="text" class="form-control" name="total" id="total" placeholder="Enter Total" required>
</div>
<div class="form-group col-md-2">
<label>Depre Amount</label>
<input type="text" class="form-control" name="dep_amount" id="dep_amount" required>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-2">
<label>Life in Days</label>
<input type="text" class="form-control" name="life_days" id="life_days" placeholder="Enter Total life" required>
</div>
<div class="form-group col-md-2">
<label>Elapsed Days(31/03)</label>
<input type="text" class="form-control" name="elap_yend" id="elap_yend" onchange="cal()" readonly/>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-2">
<label>Remaining Days</label>
<input type="text" class="form-control" name="rem_days" id="rem_days" placeholder="Enter Product No" required>
</div>
<div class="form-group col-md-2">
<label>Current Depreciation</label>
<input type="text" class="form-control" id="depre_cur" name="depre_cur" />
</div>
</div>
<div class="form-row">
<div class="form-group col-md-2">
<label>W.D.V(Curr)</label>
<input type="text" class="form-control" id="cur_wdv" name="cur_wdv" />
</div>
</div>
<div class="form-row " >
<div class="form-group col-md-2">
<label>Next Year Starting</label>
<input class="form-control" id="nextyearstart_date" name="pur_date" >
</div>
<div class="form-group col-md-2">
<label>Next Year End Date</label>
<input class="form-control" id="nextyearend_date" name="nextyearend_date" >
</div>
</div>
<div class="form-row " >
<div class="form-group col-md-2">
<label>Depreciation 2</label>
<input type="text" class="form-control" id="depre_next" name="depre_next" required/>
</div>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-2">
<label>W.D.V(Next)</label>
<input type="text" class="form-control" name="next_wdv" id="next_wdv" />
</div>
<div class="form-group col-md-2">
<label>Accum Depreciation</label>
<input type="text" class="form-control" id="acc_depre" name="acc_depre" placeholder="Enter Location" required/>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-2">
<label>Sale Amount</label>
<input type="text" class="form-control" id="sale_amount" name="sale_amount" placeholder="Enter sale amount" required/>
</div>
<div class="form-group col-md-2">
<label>Profit/Loss</label>
<input type="text" class="form-control" id="pro_los" name="pro_los" placeholder="Enter Product Desc" required/>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
Javascript:
<script type="text/javascript">
function GetDays(){
var dropdt = new Date(document.getElementById("end_date").value);
var pickdt = new Date(document.getElementById("pur_date").value);
//var pickdt = new Date();
return parseInt((dropdt - pickdt) / (24 * 3600 * 1000));
}
function cal(){
if(document.getElementById("end_date")){
document.getElementById("elap_yend").value=GetDays();
}
}
$(product_price).ready(function(){
var $nextyearstart_date = $( "#nextyearstart_date" );
var $nextyearend_date = $( "#nextyearend_date" );
$nextyearstart_date.datepicker();
$nextyearend_date.datepicker({
onClose: function() {
var product_price;
var igst;
var cgst;
var sgst;
product_price = parseFloat($('#product_price').val());
life_days = parseFloat($('#life_days').val());
elap_yend = parseFloat($('#elap_yend').val());
dep_amount = parseFloat($('#dep_amount').val());
depre_cur = parseFloat($('#depre_cur').val());
igst = parseFloat($('#igst').val());
cgst = parseFloat($('#cgst').val());
sgst = parseFloat($('#sgst').val());
var igst = product_price * 0.09;
$('#igst').val(igst.toFixed(2));
var cgst = product_price * 0.09;
$('#cgst').val(cgst.toFixed(2));
var sgst = product_price * 0;
$('#sgst').val(sgst.toFixed(2));
var total = product_price + igst + cgst + sgst;
$('#total').val(total.toFixed(2));
var dep_amount = product_price + (.5 * (igst + cgst + sgst));
$('#dep_amount').val(dep_amount.toFixed(2));
var rem_days = life_days - elap_yend;
$('#rem_days').val(rem_days.toFixed(2));
var depre_cur = (dep_amount/life_days) * elap_yend; //life_days - elap_yend;
$('#depre_cur').val(depre_cur.toFixed(2));
var cur_wdv = dep_amount - depre_cur; //life_days - elap_yend;
$('#cur_wdv').val(cur_wdv.toFixed(2));
var fromDate = $nextyearstart_date.datepicker('getDate');
var toDate = $nextyearend_date.datepicker('getDate');
var diff = new Date(toDate - fromDate);
var Ddays = diff/1000/60/60/24;
var depre_next = Ddays * (dep_amount/life_days);
$('#depre_next').val(depre_next.toFixed(2));
var next_wdv = cur_wdv - depre_next;
$('#next_wdv').val(next_wdv.toFixed(2));
var acc_depre = depre_cur + depre_next;
$('#acc_depre').val(acc_depre.toFixed(2));
var pro_los = depre_cur + depre_next;
$('#pro_los').val(pro_los.toFixed(2));
// alert(depre_next);
}
});
});
</script>
解决方案
JavaScript 中的日期比较可能很棘手。有时最好使用第三方日期库,例如 MomentJS 或 Luxon。
你想做的事情的基础是相当基础的。创建您想要作为基线的日期,然后是您的测试日期,并进行简单的比较。
const cutoffDate = new Date('March 31, 2018 23:59:59');
const testDate = new Date(); // now
if (testDate >= cutoffDate) {
// do this calculation
} else {
// ok, do this calculation
}
总是绊倒人们的是时区(这就是第三方库很好的原因)。使用 JSDate
对象时,请记住它将使用浏览器的时区,因此在纽约系统的用户和上海系统的用户之间创建的日期对象会有所不同。
推荐阅读
- javascript - 一种在 React 中使用 localstorage 显示组件一次的方法
- c# - 如何使用 Entity Framework 6 从 10 多个表中获取记录并从中显示报告?
- if-statement - Python 3.7.2 Elif 仅在 if 语句中不起作用
- node.js - Mongoose - MongoError:无法识别的管道阶段名称:'$eq'
- r - 使用 Prophet 包进行分组预测并创建绘图
- sql-server - 无法完成游标操作,因为设置选项已更改
- ibm-watson - 尝试训练视觉识别模型时出现“请求实体太大”错误
- sql - SQL Join:如何显示第一个表数据,即使第二个表条件为假
- elixir - 如何在运行时调用 Plug.Router 宏定义的路由?
- r - 自定义函数和嵌套变量