从日期开始计算时间,包括年,月,日,小时,分钟和秒

时间:2018-02-04 05:21:08

标签: javascript time

我想从2016年11月28日起抽出时间。 我希望时间显示像是1年2个月6天17小时19分36秒。

var dateString = '11/28/2016';
var now = new Date();
var today = new Date(now.getYear(), now.getMonth(), now.getDate());
var yearNow = now.getYear();
var monthNow = now.getMonth();
var dateNow = now.getDate();
var dob = new Date(dateString.substring(6, 10),
    dateString.substring(0, 2) - 1,
    dateString.substring(3, 5)
);
var yearDob = dob.getYear();
var monthDob = dob.getMonth();
var dateDob = dob.getDate();
var time = {};
yearTime = yearNow - yearDob;
if (monthNow >= monthDob)
    var monthTime = monthNow - monthDob;
else {
    yearTime--;
    var monthTime = 12 + monthNow - monthDob;
}
if (dateNow >= dateDob)
    var dateTime = dateNow - dateDob;
else {
    monthTime--;
    var dateTime = 31 + dateNow - dateDob;

    if (monthTime < 0) {
        monthTime = 11;
        yearTime--;
    }
}
time = {
    years: yearTime,
    months: monthTime,
    days: dateTime
};
if (time.months < 9) {
    time.months = '0' + time.months;
}
if (time.days < 9) {
    time.days = '0' + time.days;
}

$('#cnt_year').text('0' + time.years);
$('#cnt_month').text(now);
$('#cnt_days').text(time.days);

它显示年,月和日,但我如何添加小时和分钟。

2 个答案:

答案 0 :(得分:1)

鉴于一个月内只有30天/所有的十分之一。

	var dateString = '11/28/2016';
	var now = new Date();
	var today = new Date(now.getYear(), now.getMonth(), now.getDate());
	var yearNow = now.getYear();
	var monthNow = now.getMonth();
	var dateNow = now.getDate();
	var dob = new Date(dateString.substring(6, 10),
		dateString.substring(0, 2) - 1,
		dateString.substring(3, 5)
	);
		
	var nTotalDiff = Math.abs(now - dob);
	var oDiff = {};
	
	oDiff.years = Math.floor(nTotalDiff / 1000 / 60 / 60 / 24 / 30 / 12);
	nTotalDiff -= oDiff.years * 1000 * 60 * 60 * 24 * 30 * 12;
	
	oDiff.months = Math.floor(nTotalDiff / 1000 / 60 / 60 / 24 / 30);
	nTotalDiff -= oDiff.months * 1000 * 60 * 60 * 24 * 30;
	
	oDiff.days = Math.floor(nTotalDiff / 1000 / 60 / 60 / 24);
	nTotalDiff -= oDiff.days * 1000 * 60 * 60 * 24;

	oDiff.hours = Math.floor(nTotalDiff / 1000 / 60 / 60);
	nTotalDiff -= oDiff.hours * 1000 * 60 * 60;

	oDiff.minutes = Math.floor(nTotalDiff / 1000 / 60);
	nTotalDiff -= oDiff.minutes * 1000 * 60;

	oDiff.seconds = Math.floor(nTotalDiff / 1000);	
		
	console.log( oDiff );

答案 1 :(得分:0)

以下是如何使用moment.js的diff和添加功能的示例

    const today = moment();
    const dateString = moment('11/28/2016');
    const diffYears = today.diff(dateString, 'years');  // diff gives you the difference between two days in integer.
    dateString.add(diffYears, 'years');   // to get difference in month now, you first need to add `diffYears` to the dateString, same follows for the further calculations.
    const diffMonths = today.diff(dateString, 'months');
    dateString.add(diffMonths, 'months');
    const diffDays = today.diff(dateString, 'days');
    dateString.add(diffDays, 'days');
    const diffHours = today.diff(dateString, 'hours');
    dateString.add(diffHours, 'hours'); 
    const diffMinutes = today.diff(dateString, 'minutes');
    dateString.add(diffMinutes, 'minutes'); 
    const diffSeconds = today.diff(dateString, 'seconds');

您可以在http://momentjs.com/docs/#/displaying/difference/

找到更多示例