Function for add minutes [duplicate] - javascript

This question already has answers here:
How to format numbers by prepending 0 to single-digit numbers?
(36 answers)
How do I format a date in JavaScript?
(68 answers)
Closed 5 years ago.
I need to add minutes in an specif hour.
It is in js.
I have a hour = 09:30 and need to add more 30minutes, but the output shows me '10:0', one '0' less.
Soma_minutos: function(hour, minutes) {
// This case minutes = 30; hour = 09:30
var tempo, hora_nova, horas = '';
tempo = new Date("T"+hora);
tempo.setTime(tempo.getTime() + minutos*60000);
horas = tempo.getHours().toString(); // 10
minutos = tempo.getMinutes().toString(); // 0
hora_nova = horas + ':' + minutos;
return hora_nova;
},

something like this:
if (minotos.length = 1)
{
minotos = "0" + minotos;
}

Related

Convert giving number to minutes and seconds [duplicate]

This question already has answers here:
Convert seconds to HH-MM-SS with JavaScript?
(38 answers)
Javascript seconds to minutes and seconds
(30 answers)
Closed 23 days ago.
I found a way to convert to hours and munites, how can I convert to minutes and seconds?
function time_convert(num)
{
var hours = Math.floor(num / 60);
var minutes = num % 60;
return hours + ":" + minutes;
}
console.log(time_convert(71));
console.log(time_convert(450));
console.log(time_convert(1441));

How to get current time HH:MM:SS in Audio element? [duplicate]

This question already has answers here:
Convert seconds to HH-MM-SS with JavaScript?
(38 answers)
Javascript/HTML5: get current time of audio tag
(2 answers)
Closed 1 year ago.
How to get current time HH: MM: SS in Audio JavaScript DOM?
I want to display audio object current time like HH: MM: SS in PureJS!
Example:
//// get current time
myPlayer.addEventListener("timeupdate", function(){
myRange.value = myPlayer.currentTime;
tt = "0";
var Amin = Math.floor(myPlayer.currentTime/60);
var Asec = Math.floor(myPlayer.currentTime - Amin * 60);
if(Asec < 10){
Asec = "0" + Asec;
}
if(Amin > 10){
tt="";
}
currenttime.innerHTML = tt+Amin+":"+Asec;
});

Why isn't my IF statement applying to time? [duplicate]

This question already has answers here:
getMinutes() 0-9 - How to display two digit numbers?
(21 answers)
Closed 3 years ago.
I would like to get it to display 0 infront of the output if it's under 10.
const time = new Date();
let hour = today.getHours();
let minute = today.getMinutes();
let second = today.getSeconds();
if (second < 10) {
second = 0 + second;
}
console.log(`Time is ${hour} : ${minute} : ${second}`);
Instead of showing for example 19:5:7, I would like to see 19:05:07
///
Ok, I found out what the problem was. 0 was a number not a string. Just started with JS.
Thanks for the help!
You could pad the value with leading zeroes.
const pad2 = s => s.toString().padStart(2, '0');
let today = new Date;
let hour = pad2(today.getHours());
let minute = pad2(today.getMinutes());
let second = pad2(today.getSeconds());
console.log(`Time is ${hour}:${minute}:${second}`);

Convert Military Hours to Normal Hours [duplicate]

This question already has answers here:
How do you display JavaScript datetime in 12 hour AM/PM format?
(31 answers)
Closed 5 years ago.
I'd like my hours section to be set to 1–12 and not 0–23. Thank you.
Here's the JavaScript:
setInterval(function(){ time();}, 1000)
function time(){
var dates = new Date();
var newDates = dates.toDateString();
// var clock = dates.toLocaleTimeString();
var seconds = dates.getSeconds();
var minutes = dates.getMinutes();
var hours = dates.getHours();
var stringSeconds= String(seconds);
var stringMinutes= String(minutes);
var stringHours= String(hours);
newDate.textContent = newDates;
newDivSeconds.textContent = stringSeconds;
newDivMinutes.textContent = stringMinutes + ' :' ;
newDivHours.textContent = stringHours + ' :';
const usHours = (date.getHours() % 12) || 12;
Use the modulus operator
var usHours = date.getHours() % 12;

Convert the 12 hr time format to 24 hr format using javascript [duplicate]

This question already has answers here:
convert 12-hour hh:mm AM/PM to 24-hour hh:mm
(38 answers)
Closed 8 years ago.
I have the time which is in the format "07:30PM" .I need to convert into 24 hr format using javascript to do further calculations.Please assist me in doing this.
Duplicate of
this
Simple solution:
function ampmTo24(time)
{
var hours = Number(time.match(/^(\d+)/)[1]);
var minutes = Number(time.match(/:(\d+)/)[1]);
var AP = time.match(/\s(.*)$/);
if (!AP) AP = time.slice(-2);
else AP=AP[1];
if(AP == "PM" && hours<12) hours = hours+12;
if(AP == "AM" && hours==12) hours = hours-12;
var Hours24 = hours.toString();
var Minutes24 = minutes.toString();
if(hours<10) Hours24 = "0" + Hours24;
if(minutes<10) Minutes24 = "0" + Minutes24;
return Hours24 + ":" + Minutes24
}

Categories