2 回答
TA贡献1804条经验 获得超7个赞
使用Date.parse()
(尽管由于实现不一致而不鼓励使用)
function datediff(first, second) {
return Math.round((second-first)/(1000*60*60*24));
}
const deFromDate = Date.parse('10-Nov-2020');
const deToDate = Date.parse('15-Nov-2020');
console.log(datediff(deFromDate, deToDate));
手动解析
var months = {
'Jan' : 1,
'Feb' : 2,
'Mar' : 3,
'Apr' : 4,
'May' : 5,
'Jun' : 6,
'Jul' : 7,
'Aug' : 8,
'Sep' : 9,
'Oct' : 10,
'Nov' : 11,
'Dec' : 12
}
function parseDate(str) {
var [dd, mmm, yyyy] = str.split('-');
return new Date(yyyy, months[mmm]-1, dd); // months are 0 indexed
}
function datediff(first, second) {
return Math.round((second-first)/(1000*60*60*24));
}
const deFromDate = parseDate('10-Nov-2020');
const deToDate = parseDate('15-Nov-2020');
console.log(datediff(deFromDate, deToDate));
TA贡献1847条经验 获得超7个赞
我尝试使用正则表达式:
<script>
var deFromDate;
var deToDate;
var dt1;
var dt2;
function deFromDateChanged(s, e) {
deFromDate = (s.GetInputElement().value)
dt1 = new Date(deFromDate.replace(/(\d{2})-(\d{2})-(\d{4})/, "$2/$1/$3"));
}
function deToDateChanged(s, e) {
deToDate = (s.GetInputElement().value)
dt2 = new Date(deToDate.replace(/(\d{2})-(\d{2})-(\d{4})/, "$2/$1/$3"));
var result = ((dt2 - dt1) / 86400000);
alert(result);
}
</script>
添加回答
举报