PHP Timesheet calculate past 24hrs - javascript

I'm trying to calculate a timesheet i'm working on within a JS onChange event, the following works but it wont work past midnight, for example 23:00 - 01:00 returns 22 instead of 2 or rather 2.00 as I'd like it to report to 2 decimal places.
$startm = date_create({mS}); //create a php datetime object
$finishm = date_create({mF});
$t_diff = date_diff($startm, $finishm); //calculate the difference
{mT} = $t_diff->h + round($t_diff->i/60,2) + {mOt} + {mOt2} /24; //add everything up
The form has the following fields
Start Tme = mS - 24hr time
Finish Time = mF - 24hr time
Overtime = mOt - int
Overtime 2 = mOt2 - int
Total = mT - int

You can just add a check if $startm is past noon and $finishm is NOT past noon, $total -= 12. This of course won't work for shifts that last more than 24 hours, but that doesn't seem to be an acceptance criteria for your code.

Related

Verify date entered on a PDF form is start of a pay period

Working on a PDF form in LiveCycle Designer, I have a field where employees will enter the proposed effective date for a schedule change. These can only be effective on the start of a pay period, which is every other Sunday. (For reference, Sunday, Jan 30, 2022 is a start of a pay period.)
I've tried very many ways of using if statements and the date.getDay() method, but I find that the getDay method sometimes returns the wrong number (the employee enters a date for a Sunday, but the code gets a getDay value of 1 instead of 0). I assume this has something to do with the local time, but not sure.
For reference, here is the code I currently have in my change event. I can't say for sure the rest of it works, as I can't get past the getDay problem. Anyway, appreciate either a fix to my getDay problem, or a more elegant solution to this whole problem that avoids it.
topmostSubform.Page1.Artifact[3].dateEffective::change - (JavaScript, client)
// Check if entered date is a Sunday at the start of a pay period.
// For reference, Sunday 1/30/2022 is the start of a pay period.
// perform this check only if something is entered in this field
xfa.host.messageBox('Value: ' + xfa.event.newText);
xfa.host.messageBox('Is an entry?: ' + !(!xfa.event.newText));
if (!(!xfa.event.newText)) {
const myRefDate = new Date(2022, 1, 30);
const myDate = new Date(xfa.event.newText);
xfa.host.messageBox('Day: ' + myDate.getDay());
// first check if entered date is a Sunday. If so, then execute the other code.
if (myDate.getDay() == 0) {
const diffTime = Math.abs(myRefDate - myDate);
xfa.host.messageBox('diffTime: ' + diffTime);
const diffDays = diffTime / (1000 * 60 * 60 * 24); // includes fractional days
xfa.host.messageBox('diffDays: ' + diffDays);
const diffPP = diffDays / 14; // includes fractional parts of a 14-day pay period
xfa.host.messageBox('diffPP: ' + diffPP);
const remainderPP = diffPP % 1; // calculate remainder of diffPP
xfa.host.messageBox('remainderPP: ' + remainderPP);
// if the remainderPP value indicates that myDate is more than 1 day from a Sunday, then fail
if (remainderPP * 14 > 1) {
// messagebox with error
xfa.host.messageBox('Error1: You must enter the date for a Sunday that is the start of a pay period. Check your Pay/Holiday Schedule.');
xfa.host.setFocus(this.name);
}
}else{
xfa.host.messageBox('Error2: You must enter the date for a Sunday that is the start of a pay period. Check your Pay/Holiday Schedule.');
xfa.host.setFocus(this.name);
}
};

How can we subtract time using moment,js?

I have time1 = '09:00 AM' and time2 = '06:30 PM'.
How can i subtract these two using moment.js, such a way that i get the result = 9hrs 30mins.
I searched through the internet but couldn't find an apt solution.
Any suggestions are much appreciated.
I suggest programming your own custom function that converts the time to 24 hrs, adding 12 hours if PM, 0 if AM. It then converts the times in to minutes, subtracts the two times, and converts them back into HH:MM AM/PM.
(Pseudocode (reads a bit like javascript))
//ap = am or pm; 0 for am, 1 for pm
define "subtractTimes" (time1, time1ap, time2, time2ap):
//gets the length of time1 and time2
set "time1Length" to (length(time1))
set "time2Length" to (length(time2))
//adds 12 hours if pm, converted to minutes
set "time1InMinutes" to (time1ap * (12 * 60))
set "time2InMinutes" to (time2ap * (12 * 60))
//gets minutes from time1 and time2
//You'd have to program your own function "getLetters"
//Unless there's one that I'm unaware of.
set "time1MM" to (getLetters(time1,(time1Length-1),time1Length))
set "time2MM" to (getLetters(time2,(time2Length-1),time2Length))
//this script makes sure the times are the proper length.
if "time1Length" = (4)
set "time1" to (("0")join(time1)
if "time2Length" = (4)
set "time2" to (("0")join(time2)
//gets hours from time1 and time 2
set "time1HH" to (getLetters(time1,(time1Length-4),time1Length-3))
set "time2HH" to (getLetters(time2,(time2Length-4),time2Length-3))
//puts it all together
set "time1InMinutes" to (time1InMinutes+(time1HH*60)+time1MM)
set "time2InMinutes" to (time2InMinutes+(time2HH*60)+time2MM)
set "newTimeInMinutes" to ((time1InMinutes)-(time2InMinutes))
//converts it to HH:MM AM/PM
set "newTime" to (floor(newTimeInMinutes/60))
set newTimeInMinutes" to (newTimeInMinutes-(newTime*60))
if "newTime" > (12):
set "newTime" to (newTime-12)
set "ap" to (AM)
else
set "ap" to (PM)
set "newTime" to ((newTime)join(":")join(newTimeInMinutes))
set "newTime" to ((newTime)join(" ")join(ap)
//end
This should work. If you have any questions, feel free to ask.

javascript wrong date / days calculation

I need to calculate the number of nights between 2 dates, it works but it's very odd.
If I pick dates like 22,06,2015 and 22,07,2015 it shows me 31 nights, which is wrong since June has only 30 days.
if I pick dates like 01,07,2015 and 31,07,2015 it shows me 30 nights, which is correct.
if I pick dates like 01,07,2015 and 1,08,2015 it shows me 31 nights etc.
if I pick dates like 30,09,2015 and 30,10,2015 it shows me 31.041666666666668 nights which is odd and incorrect.
Hope you can help me with this one. Here's the code:
var date11 = $("#in").val();
var date22 = $("#out").val();
// First we split the values to arrays date1[0] is the year, [1] the month and [2] the day
date111 = date11.split('-');
date222 = date22.split('-');
// Now we convert the array to a Date object, which has several helpful methods
date1 = new Date(date111[2], date111[1], date111[0]);
date2 = new Date(date222[2], date222[1], date222[0]);
// We use the getTime() method and get the unixtime (in milliseconds, but we want seconds, therefore we divide it through 1000)
date1_unixtime = parseInt(date1.getTime() / 1000);
date2_unixtime = parseInt(date2.getTime() / 1000);
// This is the calculated difference in seconds
var timeDifference = date2_unixtime - date1_unixtime;
// in Hours
var timeDifferenceInHours = timeDifference / 60 / 60;
// and finaly, in days :)
var timeDifferenceInDays = timeDifferenceInHours / 24;
Thanks a million!
You aren't subtracting 1 from the calendar month number:
date1 = new Date(date111[2], date111[1] - 1, date111[0]);
--------^^^^
Months are zero indexed. you should probably also round the result as if you cross a daylight saving boundary, the time value won't be an even number of days, it will be out by 1 hour (unless you cross two boundaries…)

Convert any String time to seconds

I'm trying to convert 15:00 (15minutes) to seconds though I get 54,000 when I use this below.
I'm trying to convert 15minutes to seconds.
S = '15:00';
D = "1/1/1 "
s = ( new Date(D+S) - new Date(D) )/1000
alert(s);
Though when I do the math, it's 60 x 15 = 900. How do I get 900, since the time is a random string.
Well if your format will always be "mm:ss" you could dome string parsing and do the math manually, of course this would need to be adjusted depending on the input format.
S = '15:25';
var times = S.split(":");
var minutes = times[0];
var seconds = times[1];
seconds = parseInt(seconds, 10) + (parseInt(minutes, 10) * 60);
alert(seconds);​
Note in the example I explicitly added 25 seconds just as demonstration.
http://jsfiddle.net/Jg4gB/
The time string '15:00' in JavaScript refers to the time of day 1500hr, or 3:00 p.m. American-style. That's 15 hours after midnight. That explains why you got 54,000 seconds.
If you wanted to express 15 minutes using your method of manipulating date strings, try '00:15:00'.

Simple javascript time/money per hour calculator

So I'm just learning Javascript and I'm trying to create a calculator that can subtract time. I have the other parts working (although I'd love feedback on my code there as I'm sure it can be majorly improved on), I just need to get the time subtracting right. I'm doing simple math so when I subtract 1:30 (hours and minutes are separate values) from 2:00 it gives me 1:30 instead of just 00:30.
Another problem is the gold per hour doesn't calculate unless I hit the 'Get Results' button twice....
This is the first script I have ever written so please let me know what I should be doing, I want to do this the best and easiest way possible.
Calculator and script are here:
http://www.coolestwebsiteintheuniverse.com/gold-calculator/
http://www.coolestwebsiteintheuniverse.com/gold-calculator/calc.js
I'd also like the ability to expand it to 10 rows and average all of them but I think I could figure that out on my own once this part is figured out.
Thanks
Have you tried using date function to subtract.??
var T1=new Date("September 5, 2012 8:10:00");
var T2=new Date("September 5, 2012 13:35:00");
var diff=new Date();
diff.setTime(T2-T1);
alert(diff.getHours()+":"+diff.getMinutes())
The problem is that you're treating the hours separate from the minutes in making time calculations. You need to combine them with something like this (untested):
var starthh = // ...
var startmm = // ...
var endhh = // ...
var endmm = // ...
var elapsedMinutes = (60 * endhh + endmm) - (60 * starthh + startmm)
var displayTime = Math.floor(elapsedMinutes / 60) + ":"
+ ("00" + (elapsedMinutes % 60)).slice(-2)
That last bit, ("00" + (elapsedMinutes % 60)).slice(-2) takes the minutes modulo 60, appends it to the string "00" and then takes the last two charactes, as a quick way to zero pad single-digit numbers.

Categories