two different hours subtraction Angular 6 - angular6

I am trying to do how to subtract two different hours that is captured:
Here is TS code
clockingIn() {
var dt = new Date()
this.clockedIn= new Date().getHours()+':'+ new Date().getMinutes()+':'+ new Date().getSeconds();
console.log('clockin', this.clockedIn);
this.disableClockIn = true;
this.disableClockOut = false;
}
clockingOut() {
var dt2 = new Date();
this.clockedOut= new Date().getHours()+':'+ new Date().getMinutes()+':'+ new Date().getSeconds();
console.log('clockout', this.clockedOut);
this.disableClockIn = false;
this.disableClockOut = true;
}
subtraction() {
var now = "04/09/2013 15:00:00";
var then = "02/09/2013 14:20:30";
var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
}
//table
test() {
const data = {
dateClock: this.dateobj(),
Job: this.selectedJob,
clockI: this.clockedIn,
clockO: this.clockedOut,
Hoursworked: this.subtraction()
};
this.dataSource.data.push(data);
this.refresh();
console.log(this.dataSource);
}
So this is a clock in and Clock out.
I want these two can calculate (Clock Out - Clock In) to see the difference of two hours in the table that I made. So this way, I can see the diff time between Clock Out and Clock In.

I am assuming your duration will be calculated in subtraction function.
Below changes will get the result :
subtraction() {
var now = "04/09/2013 15:00:00";
var then = "02/09/2013 14:20:30";
// I have set to minutes. Change unit as per your requirement by
// referring doc given as reference
var unit = 'm';
return moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"), unit);
}
Moment.js duration

Related

How can I order my string in as3

A complex question :
I've got this code (not the complete code, but the essentials for the question, I think) :
var $pmm:String;
var $pms:String;
var $bmm:String;
var $bms:String;
function get haute1():String { return $pmm; };
function get haute2():String { return $pms; }
function get basse1():String { return $bmm; };
function get basse2():String { return $bms; };
accueil.todayHaute_txt.htmlText = haute1;
accueil.todayBasse_txt.htmlText = basse1;
accueil.todayHauteSecond_txt.htmlText = haute2;
accueil.todayBasseSecond_txt.htmlText = basse2;
"haute1" is an hour (in 24h format). Something like "13h25".
It changes everyday.
Question : How can put them in ascending order in AS3 ?
Example : If haute1 = 15h20, haute2= 6h00, basse1= 11h and basse2 = 17h, the function would put them in this order :
"haute2", then "basse1", then "haute1" and finally "basse2".
Thx
EDIT
I add this code that I have. is it helping you ?
/ Assigns hours and tidal heights
$pmm = convdateheure($tpbs[1 + $deltapm]);
$pms = convdateheure($tpbs[3 + $deltapm]);
$bmm = convdateheure($tpbs[2 - $deltapm]);
$bms = convdateheure($tpbs[4 - $deltapm]);
function convdateheure($valeur:Number):String
{
var $heure:Number = Math.floor($valeur);
var $minute:Number = Math.floor(Math.floor(($valeur - Math.floor($valeur)) * 100) * 0.6);
var hoursLabel:String = "", minsLabel:String = "";
if ($heure == 24) $heure = 0; // Check if at the 24 hour mark, change to 0
if ($heure < 10) hoursLabel += "0" + $heure.toString(); else hoursLabel = $heure.toString();
if ($minute < 10) minsLabel += "0" + $minute.toString(); else minsLabel = $minute.toString();
return hoursLabel + ":" + minsLabel;
}
If you want to order some dates written in some String format:
One way would be, depending on you date string format, just to push them into array and sort them as strings, then read them all.
Another way would be to first parse those strings into Date instances, and push their Date.time property to array, sort it, then do reverse: parse all time values from sorted array into new Date instances then use Date.toString or similar.
Assuming that $valuer is a numerical value:
var timesArray:Array = new Array();
var convertedTimesArray:Array = new Array();
function sortTimes():void{
timesArray.push($valuer);
timesArray.sort(Array.NUMERIC);
}
function convertTimes():void{
convertedTimesArray = []; // clear the array
for (var i:int = 0; i < timesArray.length; i++){
var s:String = convdateheure(timesArray[i]);
convertedTimesArray.push(s);
}
}
That should give you one array of actual times, sorted in numerical order, and one array sorted in the same numerical order, but converted to String values using your function.

Trigger UiApp-builder callback within another routine

Serge's solution here seemed like the way to go about this, but I'm a bit afraid that my circumstances may be too different...
I have a button where users can add a new set of rows with controls to a FlexTable, in order to allow them to insert a new member into a record set. After designing and building the app to do this (and despite assurances to the contrary), a requirement was then added for the users to be able to edit the record sets at a later date.
I've finally managed to get the data retrieved and correctly displayed on the Ui - for single member record sets. As a final stage, I am now attempting to extend this to accommodate record sets having more than one member. Obviously this requires determining how many members there are in the record set, and then adding the new rows/control group to the FlexTable, before loading the member into each control group.
So within this routine, (depending on how many members there are) I may need to trigger the same callback, which the user normally does with a button. However, the difference with Serge's fine example, is that his code triggers the checkbox callback at the end of his routine once all the Ui components are in place. My situation needs to do this on the fly - and so far I'm getting 'Unexpected error', which suggests to me that the Ui is not able to update with the added FlexTable controls before my code attempts to assign values to them.
Does anyone have any insight into this problem? Is my only recourse to completely re-build a fixed Ui and dispense with the dynamic rowset model?
Code follows -
1. event for adding controls:
var app = UiApp.getActiveApplication();
var oFlexGrid = app.getElementById('ExpenseDetail');
var oRowCount = app.getElementById('rowCount');
var oScriptDBId = app.getElementById('scriptDBId');
var iRows = parseInt(e.parameter.rowCount);
var sVId = e.parameter.scriptDBId;
var vGridDefs = loadArrayById(sVId); //retrieve upload definition array from ScriptDB
var vControlNames = [];
if (isOdd(iRows)){
var sColour = 'AliceBlue';
} else {
var sColour = 'LavenderBlush';
};
oFlexGrid.insertRow(0);
oFlexGrid.insertRow(0);
oFlexGrid.insertRow(0);
oFlexGrid.insertRow(0);
oFlexGrid.setRowStyleAttributes(0,{'backgroundColor':sColour});
oFlexGrid.setRowStyleAttributes(1,{'backgroundColor':sColour});
oFlexGrid.setRowStyleAttributes(2,{'backgroundColor':sColour});
oFlexGrid.setRowStyleAttributes(3,{'backgroundColor':sColour});
var vExpenseDef = Get_NamedRangeValues_(CONST_SSKEY_APP,'UIAPP_GridExpense');
iRows = iRows+1;
vControlNames = CreateGrid_MixedSet_(iRows, vExpenseDef, oFlexGrid, app);
oRowCount.setText(iRows.toString()).setValue(iRows.toString());
//SOME INCONSEQUENTIAL CODE REMOVED HERE, LET ME KNOW IF YOU NEED IT
vGridDefs = vGridDefs.concat(vControlNames); // unify grid definition arrays
var sAryId = saveArray('expenseFieldDef', vGridDefs);
oScriptDBId.setText(sAryId).setValue(sAryId); //store array and save ScriptDB ID
if (e.parameter.source == 'btnExpenseAdd'){
hideDialog(); //IGNORE CHEKCBOX-DRIVEN CALLS
};
return app;
2. routine that calls the event
var app = UiApp.getActiveApplication();
var oPanelExpense = app.getElementById('mainPanelExpense');
var oPanelIncome = app.getElementById('mainPanelIncome');
var oPanelEdit = app.getElementById('mainPanelEdit');
var chkExpenseAdd= app.getElementById('chkExpenseAdd');
var bExpenseTrigger = e.parameter.chkExpenseAdd;
var sVoucherId = nnGenericFuncLib.cacheLoadObject(CACHE_EDIT_VOUCHERID);
var sVoucher = e.parameter.ListSearch1Vouchers;
var aryVoucherInfo = getVoucherEditDetail(sVoucherId);
//SAVE FOR RECORD MARKING CALLBACK
nnGenericFuncLib.cacheSaveObject(CACHE_EDIT_OLDRECORDS, JSON.stringify(aryVoucherInfo), CACHE_TIMEOUT);
sVoucher = nnGenericFuncLib.textPad(sVoucher, '0', 7);
var bExp = (sVoucher.substring(0,2) == '03')
var oRowCount = app.getElementById('rowCount');
var iRowCount = parseInt(e.parameter.rowCount);
var sControlName = '';
var vControlVal = '';
var iExpIdx = 0;
var sControlType = '';
var oControl = '';
var vSummaryTotal = 0;
for (var iVal in aryVoucherInfo){
sControlName = aryVoucherInfo[iVal][2];
vControlVal = aryVoucherInfo[iVal][3];
switch (sControlName){
case 'ESUM60':
vSummaryTotal = vControlVal;
break;
case 'EXUSRN':
continue; //DON'T OVERWRITE CURRENT USERNAME
break;
};
if (sControlName.indexOf('_')!=-1){ //TEST FOR CONTROL SET MEMBER
var aryControlSet = sControlName.split('_');
if (parseInt(aryControlSet[1])>iRowCount){//*** TRIGGER THE EVENT ***
Logger.log(bExpenseTrigger + ' - ' + !bExpenseTrigger);
chkExpenseAdd.setValue(!bExpenseTrigger, true);
iRowCount = iRowCount +1;
};
};
oControl = app.getElementById(sControlName);
var vCache = cacheSaveReturn(CACHE_UIEX_LISTS,sControlName);
if (typeof vCache == 'undefined'){
oControl.setValue(vControlVal);
oControl.setText(vControlVal);
//controlSetTextBox(oControl,vControlVal);
//controlSetDateBox(oControl,vControlVal);
} else {
if (!(nnGenericFuncLib.arrayIsReal(vCache))){
vCache = JSON.parse(vCache);
};
vCache = vCache.indexOf(vControlVal);
if (vCache != -1){
oControl.setSelectedIndex(vCache);
} else {
controlSetListBox(oControl,vControlVal);
};
};
};
//SOME CODE REMOVED HERE
hideDialog();
return app;
Mogsdad to the rescue!
The answer (see above) for those at the back of the class (with me) is to simply pass the app instance parameter (e) to the event function, calling it directly from the main routine, thus keeping the chronology in step for when it returns the app to complete the routine. No need for the checkbox in this situation.
This only took me all day, but thanks Mogsdad! :)
Snippet below taken from 1/2 way down code sample 2 in the OP:
if (sControlName.indexOf('_')!=-1){ //TEST FOR CONTROL SET MEMBER
var aryControlSet = sControlName.split('_');
if (parseInt(aryControlSet[1])>iRowCount){
eventAddExpense(e); //THAT'S ALL IT TAKES
iRowCount = iRowCount +1;
};
};

calculate an average with 2 variables

I'm creating a game in AS3.
I've got a toolbar with a string for money, one for happiness and one for tourists.
The happiness is coded like that :
var happyString:String = "0";
var happyNumber = Number(happyString);
bonheur.text = String(happyString);
trace(happyString);
I'd like the "happy" value depends on two variables (invisibles for the player).
A variable "confort" and a variable "distraction".
And the "happy" value would be = ("confort"+"distraction")/2
I don't really know how to do it though...
I did that :
var confortString:String = "0";
var confortNumber = Number(confortString);
var distractionString:String = "0";
var distractionNumber = Number(distractionString);
stageRef.addEventListener("PiscineBuilt", piscineVariables, false, 0, true);
private function piscineVariables(event):void{
confortNumber = Number(confortString) +3;
trace(confortNumber);
distractionNumber = Number(distractionString) +20;
trace(distractionNumber);
}
First, I don't think the "confort" and "distraction" values are added each time the function is called...
Second, How I can do in order to have the value "happy" (which is visible for the player in the toolbar) equal to ("confort"+"distraction")/2
var happyString:String = distractionNumber + confortNumber ;
var happyNumber = Number(happyString);
bonheur.text = String(happyString);
trace(happyString);
??
Thank you for your help !
EDIT
So, here I am now :
var confort:Number = 0;
var distraction:Number = 0;
var happy:Number = 0;
happy=(confort+distraction)/2;
bonheur.text=String(happy);
stageRef.addEventListener("PiscineBuilt", piscineVariables, false, 0, true);
private function piscineVariables(event):void{
confort+=10;
distraction+=30;
trace(confort);
trace(distraction);
}
GOOD:
Forgot to change the value of my bonheur.text :
bonheur.text = String( Number(bonheur.text ) +(confort+distraction)/2 );
Why do you store strings and make numbers dependant on them? You just do:
var confort:Number = 0;
var distraction:Number = 0;
var happy:Number = 0;
And whenever your numbers are updated, you should also update the text in bonheur:
happy=(confort+distraction)/2; // making an average is as simple as this, if you're not using strings!!!
bonheur.text=String(happy);

JUnit Test of Spreadsheet using Apache POI

I am trying to verify a date that is in a spreadsheet created with Apache POI. When I do a System.out.print the Date variable and the cell contents are the same. Why is the test failing?
#Test
public void testSpreadsheetCont() throws Exception {
Date date = new Date();
boolean success = false;
FileInputStream fileInputStream = new FileInputStream("File Location.xls");
HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
HSSFSheet worksheet = workbook.getSheetAt(0);
HSSFRow row1 = worksheet.getRow(2);
HSSFCell cell0 = row1.getCell(0);
System.out.println(cell0.getDateCellValue());
System.out.println(date);
if (cell0.getDateCellValue() == date) {
success = true;
} else {
success = false;
}
Assert.assertTrue(success);
}
I think that the error is how I am formatting the date in the class that generates the spreadsheet. There it is formatted as ("mm/dd/yyyy hh:mm"). But when I format the date variable to ("mm/dd/yyyy hh:mm") and try to compare the two elements it's unsuccessful.
Date date = new Date();
SimpleDateFormat ft = new SimpleDateFormat("mm/dd/yyyy hh:mm");
if (cell0.getDateCellValue().toString() == ft.format(date)) {
success = true;
} else {
success = false;
}
use following for comparison. Perhaps this may be the reason.
if(cell0.getDateCellValue().compareTo(date)==0){
success = true;
}else{
success = false;
}
No wonder it does not work because you are comparing two Date references - you will always get false there.

Run JQuery Function on Page Load and Store the Result

I've got a script that hides most of the page when you first load the webpage.
When it does this, I want it to run a function which gets the current date, then saves it.
I've having trouble getting to do this for a few reasons.
Here is the page load code, where it hides the page if it's not been setup:
$(document).ready(
function() {
if (setup=="true") {
$("#show-page").show();
loadSetup();
} else {
$("#page-nav").hide();
}
});
Here is the date function:
function getStartDate() {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth();
var yyyy = today.getYear();
var startDate = dd + (mm * 30) + (yyyy * 360)
//edit
// return startDate
$("#start-date").val(startDate);
localStorage.startDate = $("#start-date").val();
}
I then want an if function in the first part of the loading page which says if the stored start date + 30 is less than the current date, show a message that gives them an updated url (isNewUrlNeeded()).
This was my attempt:
$(document).ready(
function() {
if (setup=="true") {
$("#show-page").show();
loadSetup();
isNewUrlNeeded();
} else {
$("#page-nav").hide();
getStartDate();
}
});
The problem here is that the getStartDate value isn't stored and I'm not sure how to store it. The reason it needs to be stored is so I can call on it later in the function isNewUrlNeeded.
Any help would be greatly appreciated.
Thank you!
Kind Regards,
Gary
You can try this :
function getStartDate() {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth();
var yyyy = today.getYear();
var startDate = dd + (mm * 30) + (yyyy * 360)
$("#show-page").data("start-date", startDate);
}
To retrieve this date you could do the follow :
var startDate = $("#show-page").data("start-date");