Jquery - 在两个日期之间以HH:MM格式获取时间

时间:2012-01-17 07:58:24

标签: javascript jquery jquery-ui-datepicker

我从两个文本字段中获取值为日期

var start_actual_time = $("#startPoint_complete_date").val();
var end_actual_time = $("#endPoint_complete_date").val();

给出值

start_actual_time  =  01/17/2012 11:20
end_actual_time    =  01/18/2012 12:20

现在我想找出这两个日期之间的HH:MM格式的持续时间(在这种情况下是25:00) 我怎么能这样做......

6 个答案:

答案 0 :(得分:12)

var start_actual_time  =  "01/17/2012 11:20";
var end_actual_time    =  "01/18/2012 12:25";

start_actual_time = new Date(start_actual_time);
end_actual_time = new Date(end_actual_time);

var diff = end_actual_time - start_actual_time;

var diffSeconds = diff/1000;
var HH = Math.floor(diffSeconds/3600);
var MM = Math.floor(diffSeconds%3600)/60;

var formatted = ((HH < 10)?("0" + HH):HH) + ":" + ((MM < 10)?("0" + MM):MM)
alert(formatted);

参见demo:http://jsfiddle.net/diode/nuv7t/5/(在jsfiddle中更改mootools 或者打开http://jsfiddle.net/nuv7t/564/

答案 1 :(得分:1)

工作示例:

发出警告信息为6:30

$(function(){
    var startdate=new Date("01/17/2012 11:20");
    var enddate=new Date("01/18/2012 12:20");
    var diff = new Date(enddate - startdate);   
    alert(diff.getHours()+":"+diff.getMinutes());
});

答案 2 :(得分:0)

首先,我建议:http://www.mattkruse.com/javascript/date/将字符串转换为日期对象,但您可以以任何方式转换它。转换后,您可以执行以下操作:

var difference_datetime = end_datetime.getTime() - start_datetime.getTime()

getTime()函数获取自1970/01/01以来的毫秒数。现在你有几毫秒的差异,你可以做任何你想要转换为更高数字的事情(例如,除以数秒为1000,将其除以60分钟,将其除以60小时,等等。)

答案 3 :(得分:0)

我在博客上做了类似的事情,看看我和我的gf:P有多长时间 http://daystogether.blogspot.com/这就是我做到的:

// *****Set the unit values in milliseconds.*****

var msecPerMinute = 1000 * 60;
var msecPerHour = msecPerMinute * 60;
var msecPerDay = msecPerHour * 24;

// *****Setting dates*****

var today = new Date();
var startDate = new Date('10/27/2011 11:00:00');

// *****Calculate time elapsed, in MS*****
var interval = today.getTime() - startDate.getTime();

var days = Math.floor(interval / msecPerDay );
interval = interval - (days * msecPerDay );

var weeks = 0;
while(days >= 7)
{
days = days - 7;
weeks = weeks + 1;
}

var months = 0;
while(weeks >= 4)
{
weeks = weeks - 4;
months = months + 1;
}


// Calculate the hours, minutes, and seconds.
var hours = Math.floor(interval / msecPerHour );
interval = interval - (hours * msecPerHour );

var minutes = Math.floor(interval / msecPerMinute );
interval = interval - (minutes * msecPerMinute );

var seconds = Math.floor(interval / 1000 );

BTW这只是javascript,没有jquery;)

答案 4 :(得分:0)

你去:

function get_time_difference(start,end)
{               
    start = new Date(start);
    end = new Date(end);
    var diff = end.getTime() - start.getTime();                 
    var time_difference = new Object();

    time_difference.hours = Math.floor(diff/1000/60/60);        
    diff -= time_difference.hours*1000*60*60;
    if(time_difference.hours < 10) time_difference.hours = "0" + time_difference.hours;

    time_difference.minutes = Math.floor(diff/1000/60);     
    diff -= time_difference.minutes*1000*60;    
    if(time_difference.minutes < 10) time_difference.minutes = "0" + time_difference.minutes;                                  

    return time_difference;              
}

var time_difference = get_time_difference("01/17/2012 11:20", "01/18/2012 12:20");

alert(time_difference.hours + ":" + time_difference.minutes);

答案 5 :(得分:0)

start_date.setTime(Date.parse(start_actual_time));
end_date.setTime(Date.parse(end_actual_time));

//for check
start_date.toUTCString()
end_date.toUTCString()

/**
 * Different in seconds
 * @var diff int
 */

var diff = (end_date.getTime()-start_date.getTime())/1000;