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

时间:2022-07-30 01:52:34

I am getting the values from two text fields as date

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

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

which gives value

赋予价值

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

now i want to find out the duration in HH:MM format between these two dates (which is 25:00 in this case) how can i do it...

现在我想知道这两个日期之间的HH:MM格式的持续时间(在本例中是25:00)我怎么做……

6 个解决方案

#1


10  

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);

See demo : http://jsfiddle.net/diode/nuv7t/5/ ( change mootools in jsfiddle or open http://jsfiddle.net/nuv7t/564/ )

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

#2


1  

Working example:

工作的例子:

gives alert message as 6:30

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());
});

#3


0  

First, I recommend this: http://www.mattkruse.com/javascript/date/ to convert the string to a date object, but you can convert it any way you want. Once converted, you can do this:

首先,我建议这样做:http://www.mattkruse.com/javascript/date/将字符串转换为日期对象,但是您可以任意转换它。一旦转换,你可以这样做:

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

The getTime() function gets the number of milliseconds since 1970/01/01. Now you have the number of milliseconds of difference, and you can do whatever you want to convert to higher numbers (eg. divide by 1000 for seconds, divide that by 60 for minutes, divide that by 60 for hours, etc.)

函数的作用是:获取1970/01/01之后的毫秒数。现在您有了毫秒差的数量,您可以做任何您想要转换为更高的数字(例如。除以1000秒,除以60分钟,除以60小时,等等)

#4


0  

I did something simular on a blog to see how long im together with my gf :P http://daystogether.blogspot.com/ and this is how I did it:

我在博客上做了一些简单的事情,看看我和我的女朋友在一起多久: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 this is just javascript, no jquery ;)

顺便说一句,这只是javascript,不是jquery;

#5


0  

here you go:

给你:

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);

#6


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;

#1


10  

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);

See demo : http://jsfiddle.net/diode/nuv7t/5/ ( change mootools in jsfiddle or open http://jsfiddle.net/nuv7t/564/ )

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

#2


1  

Working example:

工作的例子:

gives alert message as 6:30

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());
});

#3


0  

First, I recommend this: http://www.mattkruse.com/javascript/date/ to convert the string to a date object, but you can convert it any way you want. Once converted, you can do this:

首先,我建议这样做:http://www.mattkruse.com/javascript/date/将字符串转换为日期对象,但是您可以任意转换它。一旦转换,你可以这样做:

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

The getTime() function gets the number of milliseconds since 1970/01/01. Now you have the number of milliseconds of difference, and you can do whatever you want to convert to higher numbers (eg. divide by 1000 for seconds, divide that by 60 for minutes, divide that by 60 for hours, etc.)

函数的作用是:获取1970/01/01之后的毫秒数。现在您有了毫秒差的数量,您可以做任何您想要转换为更高的数字(例如。除以1000秒,除以60分钟,除以60小时,等等)

#4


0  

I did something simular on a blog to see how long im together with my gf :P http://daystogether.blogspot.com/ and this is how I did it:

我在博客上做了一些简单的事情,看看我和我的女朋友在一起多久: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 this is just javascript, no jquery ;)

顺便说一句,这只是javascript,不是jquery;

#5


0  

here you go:

给你:

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);

#6


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;