-1

I have 2 variable with the value of school starting time and prayer duration in minutes

var schoolStart = localStorage.getItem("start"); //9:58
var prayerduration = localStorage.getItem("prayer"); //10

I want the out put as 10:08

and if the starting time is 9:00 and duration is 10 then I want the out put as 9:10

noushad mohammed
  • 375
  • 1
  • 4
  • 20
  • Your example seems confusing - shouldn't the output of the example be `10:08`, not `10:58`? – Rory McCrossan Jan 09 '18 at 12:40
  • Take what you got in response to your other question, https://stackoverflow.com/q/48094066/1427878, to convert your starting time into minutes, then add your prayer minutes, and then reverse the process to get your minute value displayed in h:mm format again …? (Might make more sense if you stored minutes as integer to begin with, and only format the value as needed when you _output_ it.) – CBroe Jan 09 '18 at 12:40
  • @Rory yep it should be 10:08 – noushad mohammed Jan 09 '18 at 12:41
  • @CBroe let me try:) – noushad mohammed Jan 09 '18 at 12:44

2 Answers2

0

Use Moment.js which Parse, validate, manipulate, and display dates and times in JavaScript.

var fixedFormat = 'hh:mm';
var currentDate = moment().format( fixedFormat );
console.log( currentDate );

localStorage.setItem("start", currentDate );
var prayerTime = '10';
localStorage.setItem("prayer", prayerTime );

var schoolStart = localStorage.getItem("start");
var prayerduration = localStorage.getItem("prayer");

console.log( schoolStart, ' : ', prayerduration );

var final = moment.utc( schoolStart, fixedFormat )
                  .add(prayerduration ,'minutes').format( fixedFormat );
console.log( final );

Get Current Time:

var dt = new Date();
var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
console.log( time );

var epochDate = new Date($.now()).getTime();
var date = new Date( epochDate ); // 1515502267978
console.log( date ); // Tue Jan 09 2018 18:20:43 GMT+0530 (India Standard Time)
Yash
  • 9,250
  • 2
  • 69
  • 74
0

const addMinuteToTime = (time,minutesAdd) => {
  const hoursMinutes = time.split(":");
  const hours = parseInt(hoursMinutes[0]);
  const minutes = hoursMinutes[1] ? parseInt(hoursMinutes[1]) : 0;
  timee = ((hours * 60 + minutes + minutesAdd ) / 60).toFixed(2) ; 
  let [newHours, newMinutess] = timee.toString().split('.') 
  newMinutess  =Math.round( newMinutess /100 * 60)  || "00"
  return newHours+":"+newMinutess
}
console.log(addMinuteToTime("10:10",60))
Amit Wagner
  • 3,134
  • 3
  • 19
  • 35