我想用天、小时、分钟、秒、毫秒、纳秒来计算日期差异。我该怎么做呢?
当前回答
抱歉,平毫秒计算不可靠 感谢所有的回复,但我尝试的功能很少失败 1. 一个接近今天的日期 2. 1970年或者 3.闰年的一天。
最适合我的方法,涵盖了所有场景,例如闰年,接近1970年的日期,2月29日等。
var someday = new Date("8/1/1985");
var today = new Date();
var years = today.getFullYear() - someday.getFullYear();
// Reset someday to the current year.
someday.setFullYear(today.getFullYear());
// Depending on when that day falls for this year, subtract 1.
if (today < someday)
{
years--;
}
document.write("Its been " + years + " full years.");
其他回答
假设你有两个Date对象,你可以减去它们,以毫秒为单位得到差值:
var difference = date2 - date1;
从那里,您可以使用简单的算术来推导其他值。
另一种解决方案是将difference转换为一个新的Date对象,并获得该日期的年(与1970年不同)、月、日等。
var date1 = new Date(2010, 6, 17);
var date2 = new Date(2013, 12, 18);
var diff = new Date(date2.getTime() - date1.getTime());
// diff is: Thu Jul 05 1973 04:00:00 GMT+0300 (EEST)
console.log(diff.getUTCFullYear() - 1970); // Gives difference as year
// 3
console.log(diff.getUTCMonth()); // Gives month count of difference
// 6
console.log(diff.getUTCDate() - 1); // Gives day count of difference
// 4
所以差异就像“3年6个月零4天”。如果你想以一种人类可读的风格呈现不同,这将对你有所帮助。
function daysInMonth (month, year) { return new Date(year, month, 0).getDate(); } function getduration(){ let A= document.getElementById("date1_id").value let B= document.getElementById("date2_id").value let C=Number(A.substring(3,5)) let D=Number(B.substring(3,5)) let dif=D-C let arr=[]; let sum=0; for (let i=0;i<dif+1;i++){ sum+=Number(daysInMonth(i+C,2019)) } let sum_alter=0; for (let i=0;i<dif;i++){ sum_alter+=Number(daysInMonth(i+C,2019)) } let no_of_month=(Number(B.substring(3,5)) - Number(A.substring(3,5))) let days=[]; if ((Number(B.substring(3,5)) - Number(A.substring(3,5)))>0||Number(B.substring(0,2)) - Number(A.substring(0,2))<0){ days=Number(B.substring(0,2)) - Number(A.substring(0,2)) + sum_alter } if ((Number(B.substring(3,5)) == Number(A.substring(3,5)))){ console.log(Number(B.substring(0,2)) - Number(A.substring(0,2)) + sum_alter) } time_1=[]; time_2=[]; let hour=[]; time_1=document.getElementById("time1_id").value time_2=document.getElementById("time2_id").value if (time_1.substring(0,2)=="12"){ time_1="00:00:00 PM" } if (time_1.substring(9,11)==time_2.substring(9,11)){ hour=Math.abs(Number(time_2.substring(0,2)) - Number(time_1.substring(0,2))) } if (time_1.substring(9,11)!=time_2.substring(9,11)){ hour=Math.abs(Number(time_2.substring(0,2)) - Number(time_1.substring(0,2)))+12 } let min=Math.abs(Number(time_1.substring(3,5))-Number(time_2.substring(3,5))) document.getElementById("duration_id").value=days +" days "+ hour+" hour " + min+" min " } <input type="text" id="date1_id" placeholder="28/05/2019"> <input type="text" id="date2_id" placeholder="29/06/2019"> <br><br> <input type="text" id="time1_id" placeholder="08:01:00 AM"> <input type="text" id="time2_id" placeholder="00:00:00 PM"> <br><br> <button class="text" onClick="getduration()">Submit </button> <br><br> <input type="text" id="duration_id" placeholder="days hour min">
这段代码将返回两个日期的差值(以天为单位):
const previous_date = new Date("2019-12-23");
const current_date = new Date();
const current_year = current_date.getFullYear();
const previous_date_year =
previous_date.getFullYear();
const difference_in_years = current_year -
previous_date_year;
let months = current_date.getMonth();
months = months + 1; // for making the indexing
// of months from 1
for(let i = 0; i < difference_in_years; i++){
months = months + 12;
}
let days = current_date.getDate();
days = days + (months * 30.417);
console.log(`The days between ${current_date} and
${previous_date} are : ${days} (approximately)`);
有很多方法可以做到这一点。 是的,你可以使用普通的旧JS。试试:
let dt1 = new Date()
let dt2 = new Date()
让我们使用Date.prototype.setMinutes模拟通道,并确保我们在范围内。
dt1.setMinutes(7)
dt2.setMinutes(42)
console.log('Elapsed seconds:',(dt2-dt1)/1000)
或者你也可以使用一些像js-joda这样的库,在那里你可以很容易地做这样的事情(直接从文档中):
var dt1 = LocalDateTime.parse("2016-02-26T23:55:42.123");
var dt2 = dt1
.plusYears(6)
.plusMonths(12)
.plusHours(2)
.plusMinutes(42)
.plusSeconds(12);
// obtain the duration between the two dates
dt1.until(dt2, ChronoUnit.YEARS); // 7
dt1.until(dt2, ChronoUnit.MONTHS); // 84
dt1.until(dt2, ChronoUnit.WEEKS); // 356
dt1.until(dt2, ChronoUnit.DAYS); // 2557
dt1.until(dt2, ChronoUnit.HOURS); // 61370
dt1.until(dt2, ChronoUnit.MINUTES); // 3682242
dt1.until(dt2, ChronoUnit.SECONDS); // 220934532
有更多的ofc库,但js-joda还有一个额外的好处,它也可以在Java中使用,在Java中已经进行了广泛的测试。所有这些测试都已迁移到js-joda,它也是不可变的。