I am trying to add two time duration as below, I am getting only the first assigned valued and not the summed value of the above. Please help where it is wrong?
public rasiBalance:'';
public sunRise:'';
// storing the values for the above from some other function
public getDuration(){
console.log('Rasi Balance:'+this.rasiBalance); //Output : 0.31
console.log('Sun Rise:'+this.sunRise); // Output: 6.38
let Lagnam1 = '00:00';
Lagnam1 = moment(this.rasiBalance, 'HH:mm').add(this.sunRise, 'HH:mm').format('HH:mm');
console.log('Lagnam 1:'+Lagnam1);
}
Here is the answer
var moment = require('moment');
let rasiBalance = 0.31;
let sunRise = 6.38;
function getDuration() {
console.log('Rasi Balance : ' + rasiBalance);
console.log('Sun Rise : ' + sunRise);
console.log ("Time addition output :", addTimes(rasiBalance,sunRise));
}
// To add two times
function addTimes(time1,time2){
let convertedTime1 = convertToHoursAndMinutes(time1);
let convertedTime2 = convertToHoursAndMinutes(time2);
return parseFloat(moment(convertedTime1.hours + ":" + convertedTime1.minutes, "HH:mm").add(convertedTime2.hours,"h").add(convertedTime2.minutes,"m").format("HH:mm").replace(":","."));
}
// To split hours and minutes - We can even try to ignore this function
// if time and minutes can be split easily and fed into moment method inside the addTimes function
function convertToHoursAndMinutes(valueToConvert){
var convertedTime = {};
convertedTime.hours = valueToConvert - parseFloat((valueToConvert % 1).toFixed(2));
convertedTime.minutes = parseInt((valueToConvert % 1).toFixed(2).substring(2));
return convertedTime;
}
getDuration();