Difference between two times that are saved as strings using javascript - javascript

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.
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.
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?

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

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;

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

Related

How to achieve the correct date output in my code?

I'm not including the 12:00 PM - 01:00 PM Time
For example:
8:00 AM - 5:00 PM = 8.00
If I input
8:00 AM - 10:00 PM = 2.00
But the correct output is supposed to be (13.00)
Here is my code:
_formatTime = function(amPmString) {
var d = new Date("03/04/2019 " + amPmString);
return d.getHours() + ':' + d.getMinutes();
}
_getTimeDifference = function(startTime, endTime) {
var start = moment.utc(startTime, "HH:mm");
var end = moment.utc(endTime, "HH:mm");
var t = (end.isBefore(start)) ? end.add(1, 'day') : '';
var diff = (end.diff(start)) / 3600000;
return (t) ? diff - 13 : diff;
};
The correct result that I want is that I have to total the hours of the time-in and time-out and disregard only the lunch break which is 1 hour (12pm-1pm) in the computation of total hours. If the time range that I put is not in the lunch break. It will not deduct 1 hours.
Try this:
_getTimeDifference = function(startTime, endTime) {
var start = moment(startTime, "HH:mm A");
var end = moment(endTime, "HH:mm A");
var t = (end.isBefore(start)) ? end : '';
var diff = (end.diff(start)) / 3600000 -1;
return (t) ? diff - 13 : diff;
};
If your datetime present in Am-Pm format :the TimeSpan class in Datejs might help simplify the problem. It could be exactly your answer, if your problem is just subtract to time in AM-Pm format
I suggest such a code
var date1= Date.parse("6:30 am");
var date2= Date.parse("11:00 pm");
var diff = new TimeSpan(date2- date1);
console.log("Hours: ", diff.hours);
console.log("Minutes: ", diff.minutes);
Solution without Moment.js:
// dt1 and dt2 are assumed to be Date() objects:
function hoursDiff( dt1, dt2 ) {
// Difference in hours:
var diff = Math.abs(Math.round( (dt2.getTime() - dt1.getTime()) / 1000 / (60 * 60) ) );
// If range overlaps 12:00 - 1:00 pm, subtract 1 hour:
if (dt1.getHours() <= 12 && dt2.getHours() >= 13) {
diff--
}
return diff;
}
console.log( hoursDiff( new Date('2020-08-12 8:00:00'), new Date('2020-08-12 17:00:00') ) ) // 8am-5pm = 8
console.log( hoursDiff( new Date('2020-08-12 8:00:00'), new Date('2020-08-12 22:00:00') ) ) // 8am-10pm = 13
This doesn't account for times that begin or end within the 12:00 - 1:00 pm range, just dates that overlap it completely or not at all. It also assumes dates begin and end on hour boundaries, i.e. doesn't account for fractional hours.

Time difference between 2 datetime moment

SO i have 2 datetime objects .
now = Nov 15 4:00 PM
later = Nov 15 6:00PM
My objective is to get the total hours between (9AM to 5 PM) , given the now and later times.
resulting answer shud be 1 hour. (since im only concerned about time range that falls within 9AM-5PM)
now = Nov 15 6:00 AM
later = Nov 15 8:00 PM
resulting answer should be 8 hours.
is the best way to achieve this using the diff function in moment and stripping the hour out and calculating individual use cases ( when start time less than 9AM/ start time greater than 9AM) . similarly end time (less than 5PM/greater than 5PM) etc?
Also how to tackle this case where,
now = Nov 15 9:00AM
later = Nov 18 2:00PM
resulting answer shud be ,
8(nov 15)+8(nov 16)+8(nov 17)+5(nov 18) = 29hrs
Here's working solution
var now = moment("15 Nov 2016, 9:00:00 am", "DD MMM yyyy, h:mm:ss a").toDate();
var later = moment("18 Nov 2016, 2:00:00 pm", "DD MMM yyyy, h:mm:ss a").toDate();
function getWorkingHours(now, later) {
var hoursToday = 0;
var workingHourStart = 9;
var workingHourEnd = 17;//5pm
var workDuration = workingHourEnd - workingHourStart;
if(workingHourEnd - getHours(now) > 0) {
hoursToday = (workingHourEnd - getHours(now));
hoursToday = (hoursToday > workDuration) ? workDuration : hoursToday;
}
var hoursLater = 0;
if(getHours(later) - workingHourStart > 0) {
hoursLater = (getHours(later) - workingHourStart);
hoursLater = (hoursLater > workDuration) ? workDuration : hoursLater;
}
var actualDiffHours = (later.getTime() - now.getTime()) / (1000 * 60 * 60);
var actualHoursInBetween = actualDiffHours - (24 - getHours(now)) - getHours(later);
var workingHoursInBetween = (actualHoursInBetween / 24) * 8;
return hoursToday + workingHoursInBetween + hoursLater;
}
function getHours(date) {
var hours = date.getHours() + date.getMinutes() / 60 + date.getSeconds() / 3600 + date.getMilliseconds() / 3600/1000;
return hours;
}
console.log(getWorkingHours(now, later));
<script src="http://momentjs.com/downloads/moment.min.js"></script>
This should do the job:
const now = moment(new Date(2016, 11, 15, 9, 0, 0));
const then = moment(new Date(2016, 11, 18, 14, 0, 0));
function calDiff(now, then) {
if (now.hour() < 9) {
now.hour(9);
}
if (then.hour() > 17) {
then.hour(17);
}
const total = then.diff(now, 'hours');
const day = Math.floor(total / 24);
return total - (16 * day);
}
console.log(calDiff(now, then));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.16.0/moment.min.js"></script>
Complicated... Function getActiveHours calculates all active slots between start and finish dates, both inclusive, and then removes the missing hours at the beginning of the start date and at the end of the finish date.
var getDateObject = function (date) {
if (date && date.constructor.name == "Array") {
while (date.length < 7) {date.push(0);}
date = new Date(date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
} else if (typeof date == 'string' || typeof date == 'number') {
date = new Date(date);
}
return date;
};
var trimDate = function (date, period) {
var periods = ['second', 'minute', 'hour', 'day'];
period = periods.indexOf(period);
if (typeof date != 'number') {date = getDateObject(date).getTime();}
date = Math.floor(date/1000);
if (period > 0) {date = Math.floor(date/60);}
if (period > 1) {date = Math.floor(date/60);}
if (period > 2) {date = Math.floor(date/24);}
return new Date(date*24*60*60*1000);
};
var getOffset = function (date) {return getDateObject(date).getTimezoneOffset()*60*1000;};
var addOffset = function (date) {
date = getDateObject(date);
return new Date(date.getTime()+getOffset(date));
};
var getActiveHours = function (iniDateTime, endDateTime, startHour, finishHour) {
var hourMs = 60*60*1000; // Define daily active hours 0-24 (decimal 17.5 = 5:30pm):
if (startHour == null) {startHour = 9;}
if (finishHour == null) {finishHour = 17;}
startHour *= hourMs; finishHour *= hourMs;
iniDateTime = getDateObject(iniDateTime).getTime();
endDateTime = getDateObject(endDateTime).getTime();
var iniDayTime = addOffset(trimDate(iniDateTime, 'day')).getTime();
var endDayTime = addOffset(trimDate(endDateTime, 'day')).getTime();
var totalHoursMs = (endDayTime-iniDayTime+24*hourMs)*(finishHour-startHour)/hourMs/24;
var iniHoursNotInMs = iniDateTime-iniDayTime-startHour;
var endHoursNotInMs = endDayTime+finishHour-endDateTime;
return (totalHoursMs-iniHoursNotInMs-endHoursNotInMs)/hourMs;
};
console.log(Math.round(getActiveHours('2016-09-13 11:45:38', '2016-09-15 15:30:25'))); // 20 // Use Math round or floor
I had started writing this awhile back when I first saw the question, but got caught up. My answer is very similar to Khang's, but we went about a certain section of it a little differently.
The basic idea behind the code is that it takes two moment objects. If the start hours are less than nine, we set them to be nine, and if the end hours are greater than 17 (5pm) we set them to be 17.
Next we get the difference between the two objects in days. For each day we know that there are 8 hours the person can get credit for. I then move the date of the start day to the end day, and take the hours between them.
The idea behind this is that if both times are within the same days, there will be 0 days difference. If it is 1, then we will get a total of 8 hours regardless where we start in the day. the only cases I haven't tested are things where the start time is greater than the end time (I'll test it ASAP and make an edit if there's anything I need to change)
Edit
there was indeed a problem if the start time was after the end time (the hours).
This was fixed by adding in one if statement.
$(function() {
function getActiveHours(start, end) {
if (start.hours() < 9) start.hours(9);
if (end.hours() > 17) end.hours(17);
//These two if's should remove most of the issues when we are doing basic work
var days = end.diff(start, 'days');
if (days == 0 && (end.date() - start.date()) == 1) days = 1;
var hours = (days * 8); //gets the hours
start.date(end.date());
var diff = end.diff(start, 'hours');
return hours + diff;
}
var start = moment([2016, 10, 15, 9, 0, 0]);
var end = moment([2016, 10, 18, 14, 0, 0]);
$('#results').html('Total hours worked from ' + start.format('MM-DD-YYYY # hh:mm:ss') + ' to ' + end.format('MM-DD-YYYY # hh:mm:ss') + ' is ' + getActiveHours(start, end))
});
<div id="results"></div>

How to convert string to Time with javascript [duplicate]

Please see the below code;
var d = new Date();
var s = "01.00 AM";
d.setTime(s);
I know this code is wrong. Please give me the correct way to set the time. I have 12 hour time in string format in my hand.
The time will vary. Cannot know what will be the time earlier. Also it is 12 hour time. So it will be AM or PM
You can parse the time with a regex, and set the hours and minutes accordingly:
http://jsfiddle.net/54VkC/1/
var d = new Date(),
s = "01.25 PM",
parts = s.match(/(\d+)\.(\d+) (\w+)/),
hours = /am/i.test(parts[3]) ? parseInt(parts[1], 10) : parseInt(parts[1], 10) + 12,
minutes = parseInt(parts[2], 10);
d.setHours(hours);
d.setMinutes(minutes);
alert(d);
Edit 1:
As jaisonDavis pointed out, the original code will not work for AM or PM for 12.XX, which was an oversight since I never use 12-hour format myself, thinking it started at 00.00 which was wrong.
The corrected code which handles these cases can be seen here:
http://jsfiddle.net/54VkC/93/
var test, parts, hours, minutes, date,
d = (new Date()).getTime(),
tests = ['01.25 PM', '11.35 PM', '12.45 PM', '01.25 AM', '11.35 AM', '12.45 AM'],
i = tests.length,
timeReg = /(\d+)\.(\d+) (\w+)/;
for(; i-- > 0;) {
test = tests[i];
parts = test.match(timeReg);
hours = /am/i.test(parts[3]) ?
function(am) {return am < 12 ? am : 0}(parseInt(parts[1], 10)) :
function(pm) {return pm < 12 ? pm + 12 : 12}(parseInt(parts[1], 10));
minutes = parseInt(parts[2], 10);
date = new Date(d);
date.setHours(hours);
date.setMinutes(minutes);
console.log(test + ' => ' + date);
}
I'm late to the party, but I thought I'd share a funtion that doesn't use regex:
function setDateTime(date, time) {
var index = time.indexOf("."); // replace with ":" for differently displayed time.
var index2 = time.indexOf(" ");
var hours = time.substring(0, index);
var minutes = time.substring(index + 1, index2);
var mer = time.substring(index2 + 1, time.length);
if (mer == "PM"){
hours = hours + 12;
}
date.setHours(hours);
date.setMinutes(minutes);
date.setSeconds("00");
return date;
}
Using moment.js will be the easy solution.
const fullDate = new Date();
// fullDate = Tue Dec 12 2017 11:18:30 GMT+0530 (IST) {}
const time = '01.00 AM';
const d = moment(fullDate).format('L'); // d = "12/12/2017"
const date = moment(d +' '+ time).format();
// date = "2017-12-12T01:00:00+05:30"
If you want convert moment date to js date
const jsDate = moment(date).toDate();
function getCurrentDate() {
var lDate = new Date();
var lDay = lDate.getDate();
var lMonth = lDate.getMonth() + 1;
var lYear = lDate.getFullYear();
if (lDay < 10) {
lDay = '0' + lDay
}
if (lMonth < 10) {
lMonth = '0' + lMonth
}
mCurrentDate = lYear + "-" + lMonth + "-" + lDay + "T00:00:00+05:30";
}
I added a few things as an improvement to the accepted answer by #thebreiflabb to suit my use case and thought i'd share.
if the regex is changed to timeReg = /(\d+)[\.|:](\d+)\s?(\w+)/;,
it'll handle a few other common cases.
namely:
using a colon instead of decimal point between hours and minutes
allowing am/pm to immediately follow the time with no space
also, setting the seconds to 0 (since that was my main use case)
the resulting code would be:
var test, parts, hours, minutes, date,
d = (new Date()).getTime(),
tests = ['01.25 PM', '01:25pm', '1:25 PM', '11.35 PM', '12.45 PM', '01.25 AM', '11.35 AM', '12.45 AM'],
i = tests.length,
timeReg = /(\d+)[\.|:](\d+)\s?(\w+)/;
for(; i-- > 0;) {
test = tests[i];
parts = test.match(timeReg);
hours = /am/i.test(parts[3]) ?
function(am) {return am < 12 ? am : 0}(parseInt(parts[1], 10)) :
function(pm) {return pm < 12 ? pm + 12 : 12}(parseInt(parts[1], 10));
minutes = parseInt(parts[2], 10);
date = new Date(d);
date.setHours(hours);
date.setMinutes(minutes);
date.setSeconds(0);
console.log(test + ' => ' + date);
}

Converting time string (e.g. 9:00 am) to 24 hour (0900)

var foo = '1:00 pm'
var bar = to24Hour(foo); //bar would be 1300
function to24Hour(time) {
time.match('(\d+):(\d+) ([ap]m)');
if ($1 > 12 && $3 = pm) {
$1 = 12 + $1;
}
return $1.$2;
}
I'm trying to convert 12 hour times to 24 hours "military" time (i.e. no colon). I'm having trouble with regex capture groups and javascript but above is what I think should work.
Can someone show me the correct way?
I think you misreferensed the regex groups... This should work.
function to24Hour(time) {
var hour, groups = (/(\d+):(\d+) ([ap]m)/i).exec(time);
hour = parseInt(groups[1], 10);
if (hour < 12 && groups[3] === "pm") {
hour += 12;
}
return hour.toString() + groups[2];
}

Javascript set time string to date object

Please see the below code;
var d = new Date();
var s = "01.00 AM";
d.setTime(s);
I know this code is wrong. Please give me the correct way to set the time. I have 12 hour time in string format in my hand.
The time will vary. Cannot know what will be the time earlier. Also it is 12 hour time. So it will be AM or PM
You can parse the time with a regex, and set the hours and minutes accordingly:
http://jsfiddle.net/54VkC/1/
var d = new Date(),
s = "01.25 PM",
parts = s.match(/(\d+)\.(\d+) (\w+)/),
hours = /am/i.test(parts[3]) ? parseInt(parts[1], 10) : parseInt(parts[1], 10) + 12,
minutes = parseInt(parts[2], 10);
d.setHours(hours);
d.setMinutes(minutes);
alert(d);
Edit 1:
As jaisonDavis pointed out, the original code will not work for AM or PM for 12.XX, which was an oversight since I never use 12-hour format myself, thinking it started at 00.00 which was wrong.
The corrected code which handles these cases can be seen here:
http://jsfiddle.net/54VkC/93/
var test, parts, hours, minutes, date,
d = (new Date()).getTime(),
tests = ['01.25 PM', '11.35 PM', '12.45 PM', '01.25 AM', '11.35 AM', '12.45 AM'],
i = tests.length,
timeReg = /(\d+)\.(\d+) (\w+)/;
for(; i-- > 0;) {
test = tests[i];
parts = test.match(timeReg);
hours = /am/i.test(parts[3]) ?
function(am) {return am < 12 ? am : 0}(parseInt(parts[1], 10)) :
function(pm) {return pm < 12 ? pm + 12 : 12}(parseInt(parts[1], 10));
minutes = parseInt(parts[2], 10);
date = new Date(d);
date.setHours(hours);
date.setMinutes(minutes);
console.log(test + ' => ' + date);
}
I'm late to the party, but I thought I'd share a funtion that doesn't use regex:
function setDateTime(date, time) {
var index = time.indexOf("."); // replace with ":" for differently displayed time.
var index2 = time.indexOf(" ");
var hours = time.substring(0, index);
var minutes = time.substring(index + 1, index2);
var mer = time.substring(index2 + 1, time.length);
if (mer == "PM"){
hours = hours + 12;
}
date.setHours(hours);
date.setMinutes(minutes);
date.setSeconds("00");
return date;
}
Using moment.js will be the easy solution.
const fullDate = new Date();
// fullDate = Tue Dec 12 2017 11:18:30 GMT+0530 (IST) {}
const time = '01.00 AM';
const d = moment(fullDate).format('L'); // d = "12/12/2017"
const date = moment(d +' '+ time).format();
// date = "2017-12-12T01:00:00+05:30"
If you want convert moment date to js date
const jsDate = moment(date).toDate();
function getCurrentDate() {
var lDate = new Date();
var lDay = lDate.getDate();
var lMonth = lDate.getMonth() + 1;
var lYear = lDate.getFullYear();
if (lDay < 10) {
lDay = '0' + lDay
}
if (lMonth < 10) {
lMonth = '0' + lMonth
}
mCurrentDate = lYear + "-" + lMonth + "-" + lDay + "T00:00:00+05:30";
}
I added a few things as an improvement to the accepted answer by #thebreiflabb to suit my use case and thought i'd share.
if the regex is changed to timeReg = /(\d+)[\.|:](\d+)\s?(\w+)/;,
it'll handle a few other common cases.
namely:
using a colon instead of decimal point between hours and minutes
allowing am/pm to immediately follow the time with no space
also, setting the seconds to 0 (since that was my main use case)
the resulting code would be:
var test, parts, hours, minutes, date,
d = (new Date()).getTime(),
tests = ['01.25 PM', '01:25pm', '1:25 PM', '11.35 PM', '12.45 PM', '01.25 AM', '11.35 AM', '12.45 AM'],
i = tests.length,
timeReg = /(\d+)[\.|:](\d+)\s?(\w+)/;
for(; i-- > 0;) {
test = tests[i];
parts = test.match(timeReg);
hours = /am/i.test(parts[3]) ?
function(am) {return am < 12 ? am : 0}(parseInt(parts[1], 10)) :
function(pm) {return pm < 12 ? pm + 12 : 12}(parseInt(parts[1], 10));
minutes = parseInt(parts[2], 10);
date = new Date(d);
date.setHours(hours);
date.setMinutes(minutes);
date.setSeconds(0);
console.log(test + ' => ' + date);
}

Categories

Resources