使用javascript保存为字符串的两次之间的差异

时间:2021-10-13 21:32:05

I have two time picker jquery controls that have time in the format of "10 AM", "11 AM", "12 PM" etc.. How do I get the difference between these two time selections.

我有两个时间选择器jquery控件有时间格式为“上午10点”,“上午11点”,“下午12点”等。我如何得到这两个时间选择之间的差异。

For example, if I pick 10 AM in first control, 1 PM in second control, I need to the difference in hours which is 3. How can I do this in javascript.

例如,如果我在第一个控件中选择10 AM,在第二个控件中选择1 PM,我需要以小时为单位的差异为3.我如何在javascript中执行此操作。

var FirstTime = container.find('#1time').val();
var SecondTime = container.find('#2time').val();

alert(FirstTime); // 11 AM
alert(SecondTime);// 1 PM

var diff = (SecondTime - FirstTime); // Need value of 3 here..

I can have the time picker controls set to military time format and parse it to get the numbers out and get the difference. But thats not an option. I understand the numbers are getting saved as strings with the AM and PM attached.. is there a good way to do this?

我可以将时间选择器控件设置为军事时间格式并解析它以获取数字并获得差异。但那不是一个选择。我知道这些数字会被保存为附加AM和PM的字符串..是否有一个很好的方法来做到这一点?

3 个解决方案

#1


2  

function timeDiff( first, second ) {
    var f = first.split(' '), s = second.split(' ');

    if( first == '12 AM' ) f[0] = '0';
    if( first == '12 PM' ) f[1] = 'AM';
    if( second == '12 AM' ) s[0] = '24';
    if( second == '12 PM' ) s[1] = 'AM';

    f[0] = parseInt( f[0], 10 ) + (f[1] == 'PM' ? 12 : 0);
    s[0] = parseInt( s[0], 10 ) + (s[1] == 'PM' ? 12 : 0);

    return s[0] - f[0];
}

var FirstTime = container.find('#1time').val();
var SecondTime = container.find('#2time').val();

alert(FirstTime); // 11 AM
alert(SecondTime);// 1 PM

var diff = timeDiff( FirstTime, SecondTime );

Demo

#2


2  

You can do something like this:

你可以这样做:

var time1;
var time2;
if (FirstTime.split(' ')[1] === "PM") {
    time1 = parseInt(FirstTime.split(' ')[0])+ 12;
}else{
    time1 = parseInt(FirstTime.split(' ')[0])
}

if (SecondTime.split(' ')[1] === "PM") {
    time2 = parseInt(SecondTime.split(' ')[0])+ 12
}else{
    time2 = parseInt(SecondTime.split(' ')[0])
}

diff = time2 - time1;

#3


2  

Since the 12h clock notation don't distinguish between the midnight at the start of a particular day and the midnight at its end, the function below assumes that 12 AM means the start of the day if it is used in the first variable, and the end of the day if is used in the second variable. That means that '12 AM' - '12 AM' = 24.

由于12h时钟表示法不区分特定日期开始的午夜和结束时的午夜,因此下面的函数假定12 AM表示如果在第一个变量中使用它,则表示当天的开始,并且如果在第二个变量中使用,则结束一天。这意味着'12 AM' - '12 AM'= 24。

function timeDiff( first, second ) {
  function to24h(value) {
    value = value.split(' ');
    return (value[0] % 12) + (value[1]==='PM' ? 12:0);
  }
  return (to24h(second)||24) - to24h(first);
}

#1


2  

function timeDiff( first, second ) {
    var f = first.split(' '), s = second.split(' ');

    if( first == '12 AM' ) f[0] = '0';
    if( first == '12 PM' ) f[1] = 'AM';
    if( second == '12 AM' ) s[0] = '24';
    if( second == '12 PM' ) s[1] = 'AM';

    f[0] = parseInt( f[0], 10 ) + (f[1] == 'PM' ? 12 : 0);
    s[0] = parseInt( s[0], 10 ) + (s[1] == 'PM' ? 12 : 0);

    return s[0] - f[0];
}

var FirstTime = container.find('#1time').val();
var SecondTime = container.find('#2time').val();

alert(FirstTime); // 11 AM
alert(SecondTime);// 1 PM

var diff = timeDiff( FirstTime, SecondTime );

Demo

#2


2  

You can do something like this:

你可以这样做:

var time1;
var time2;
if (FirstTime.split(' ')[1] === "PM") {
    time1 = parseInt(FirstTime.split(' ')[0])+ 12;
}else{
    time1 = parseInt(FirstTime.split(' ')[0])
}

if (SecondTime.split(' ')[1] === "PM") {
    time2 = parseInt(SecondTime.split(' ')[0])+ 12
}else{
    time2 = parseInt(SecondTime.split(' ')[0])
}

diff = time2 - time1;

#3


2  

Since the 12h clock notation don't distinguish between the midnight at the start of a particular day and the midnight at its end, the function below assumes that 12 AM means the start of the day if it is used in the first variable, and the end of the day if is used in the second variable. That means that '12 AM' - '12 AM' = 24.

由于12h时钟表示法不区分特定日期开始的午夜和结束时的午夜,因此下面的函数假定12 AM表示如果在第一个变量中使用它,则表示当天的开始,并且如果在第二个变量中使用,则结束一天。这意味着'12 AM' - '12 AM'= 24。

function timeDiff( first, second ) {
  function to24h(value) {
    value = value.split(' ');
    return (value[0] % 12) + (value[1]==='PM' ? 12:0);
  }
  return (to24h(second)||24) - to24h(first);
}