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;
});
Related
This question already has answers here:
How can I subtract hours from a HH:MM AM time string in Javascript?
(7 answers)
Closed 1 year ago.
I have 2 strings, that have the minute and hour that a function needs to occur at.
I want to check, if the minute and hour which are specified in string format, and from my database are within 5 minutes of the current time, call this function.
My original thought was something like:
(today.minute is the current minute)
today.minute:
minute = "55"
hour = "14"
var today = new Date();
var time = today.getMinutes(), today.getHours()
if (today.getMinutes() - 5 == minute) {
myFunc()
}
But that isn't going to work, because I need the hour and minute - 5 minutes... how can I do this?
Is this what you are looking for?
let minute = "55"
let hour = "14"
let today = new Date();
//var time = today.getMinutes(), today.getHours()
if (Math.abs(today.getMinutes() - Number(minute)) <= 5) {
myFunc()
}
function myFunc() {
console.log('myFunc called', today.getMinutes());
}
This question already has answers here:
Converting milliseconds to minutes and seconds with Javascript
(14 answers)
converting milliseconds to nearest minute in javascript
(2 answers)
JavaScript - Get minutes between two dates
(12 answers)
Closed 3 years ago.
I have to get the difference of present timings(eg:1579770577465) and some planned time(eg:1579768500000) and I need to convert into minutes. How I can do it?
1579770577465 - 1579768500000 = 2077465
I want 2077465 in minutes.
Here you go:
var deltaMs = msData2 - msData1;
var totalSeconds = parseInt(Math.floor(deltaMs / 1000), 10);
var totalMinutes = parseInt(Math.floor(totalSeconds / 60), 10);
var totalHours = parseInt(Math.floor(totalMinutes / 60), 10);
var days = parseInt(Math.floor(totalHours / 24), 10);
var seconds = parseInt(totalSeconds % 60, 10);
var minutes = parseInt(totalMinutes % 60, 10);
var hours = parseInt(totalHours % 24, 10);
This question already has answers here:
How do I use .toLocaleTimeString() without displaying seconds?
(13 answers)
How do you display JavaScript datetime in 12 hour AM/PM format?
(31 answers)
Closed 3 years ago.
I want to get a JavaScript code that displays only hour, minute and AM or PM. I don't want seconds.
This is the code i have so far:
var d = new Date();
document.getElementById("time").innerHTML = d.toLocaleTimeString();
Date objects have getHours() and getMinutes() methods.
getHours() returns values in the range of [0, 23], meaning you can compare with 12 to determine 'am' or 'pm'.
Lastly, consider using mdn as a helpful reference about the standard API.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getHours
let d = new Date('December 17, 1995 22:11:51');
let pm = d.getHours() >= 12;
let hour12 = d.getHours() % 12;
if (!hour12)
hour12 += 12;
let minute = d.getMinutes();
console.log(`${hour12}:${minute} ${pm ? 'pm' : 'am'}`);
This question already has answers here:
How can I convert a HH:mm:ss string to a JavaScript Date object?
(3 answers)
Closed 4 years ago.
I am having a field called Duration it contains time like 00:20:40.How do i check given duration times (00:20:40,1:20:40,00:00:10) is <20sec, >1hour and <10 seconds .I tried the following but didn't work.
var time = new Date('00:10:40');
time.getMinutes();
Output will look like:
The given time is <20 minute.Hence i need to check like this
if(<20 minutes){...}
You have to create Date Object with Date to use it.
var d = new Date("1970-01-01 20:18:02");
document.write(d.getMinutes());
You can do the following:
var time = "00:20:40".split(":");
var minutes = time[1];
The given string "00:20:40" is not a valid date string and cannot be passed to new Date() as an argument. In this case, you can use the above solution which will split the string and give you an array consisting of [hh, mm, ss] and you will be able to get the minutes at time[1].
I hope it helps.
function toSeconds (duration) {
const regex = /(\d+):(\d+):(\d+)/;
const matched = duration.match(regex);
const hours = parseInt(matched[1]);
const minutes = parseInt(matched[2]);
const seconds = parseInt(matched[3]);
return (hours * 60 * 60) + (minutes * 60) + seconds;
}
function toMinutes (duration) {
const seconds = toSeconds(duration);
return seconds / 60;
}
function toHours (duration) {
const minutes = toMinutes(duration);
return minutes / 60;
}
toSeconds('00:20:40') // 1240
toMinutes('00:20:40') // 20.666666666666668
toMinutes('01:20:40') // 80.66666666666667
toHours('01:20:40') // 1.3444444444444446
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;
}