Error in conversion code for minutes to days - javascript

Found this lovely piece of code on this site and it worked like a charm until it got over 10,000 minutes
function minToTime(duration) { /*Call function 2 */
var minutes = parseInt(duration%60)
, hours = parseInt((duration/(60))%24)
, days = parseInt((duration/(60*24))%7);
days = (days < 10) ? "0" + days : days;
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
return days + ":" + hours + ":" + minutes;
}
Can anyone help me find a way to solve this problem? Please bear in mind I am new to javascript and have very basic knowledge.
Thank you in advance

I don't think you need to do the %7 to get the number of days. It might only be beneficial if you further want to aggregate days into weeks. So 13 days would become 1 week and 6 days.
But I don't think you need that, so the following code change should suffice:
var minutes = parseInt(duration%60)
, hours = parseInt((duration/(60))%24)
, days = parseInt((duration/(60*24)); //Don't need the %7 here
I have put together a Jsfiddle to explain the same.

you can use like this COnvertmintoday(minutes);
function COnvertmintoday(newMinutes) {
minutes_day = 24 * 60
minutes = newMinutes;
days = Math.floor(minutes / minutes_day )
return days
}

There is a %7 lurking around in the calculation of days, which must have been added number of weeks.
Remove that and the code should work fine, or calculate number of weeks too:
function minToTime(duration) { /*Call function 2 */
var minutes = parseInt(duration%60)
, hours = parseInt((duration/(60))%24)
, days = parseInt(duration/(60*24)); //remove %7
days = (days < 10) ? "0" + days : days;
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
return days + ":" + hours + ":" + minutes;
}
Alternatively you can use it to get number of weeks:
function minToTime(duration) { /*Call function 2 */
var minutes = parseInt(duration%60)
, hours = parseInt((duration/(60))%24)
, days = parseInt(duration/(60*24)%7)
, weeks = parseInt(duration/(60*24*7));
days = (days < 10) ? "0" + days : days;
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
weeks = (weeks < 10) ? "0" + weeks : weeks;
return weeks + ":" + days + ":" + hours + ":" + minutes;
}

Related

How can I convert total nanoseconds to string in format HH:MM:SS:ms:ns in JavaScript and node?

I am getting back a time in total nanoseconds from an api. I need to be able to print out a time in the format HH:MM:SS:ms:ns. An example output would be "12:34:56.123456789".
JavaScript Date object only supports for the millisecond so what would be the best way to do this?
Desired functionality:
Input: 38145999999999
Output: 10:36:41.999999999
Here's function which will solve your problem:
function msToTime(duration) {
var milliseconds = parseInt((duration%1000)/100)
, seconds = parseInt((duration/1000)%60)
, minutes = parseInt((duration/(1000*60))%60)
, hours = parseInt((duration/(1000*60*60))%24);
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
return hours + ":" + minutes + ":" + seconds + "." + milliseconds;
}

output time in hh:mm:ss format relative to how many seconds

I have created a function within JavaScript which converts seconds to the format hh:mm:ss. It works fine at the moment, however, I'd like it to be a little bit more refined than it currently is.
This is what I have at the moment:
convertTime(secs){
var sec = parseInt(secs, 10),
hours = Math.floor(sec / 3600),
minutes = Math.floor((sec - (hours * 3600)) / 60),
seconds = sec - (hours * 3600) - (minutes * 60);
if(hours < 10){ hours = "0" + hours; }
if(minutes < 10){ minutes = "0" + minutes; }
if(seconds < 10){ seconds = "0" + seconds; }
return hours + ":" + minutes + ":" + seconds;
}
// this will output 00:02:03
convertTime(123);
However, I would prefer the returned outputs to be something along the lines of this instead:
11 -> 0:11
60 -> 1:00
1200 -> 20:00
3600 -> 1:00:00
36000 -> 10:00:00
I have made countless attempts at creating (and finding) a function which can do something similar, but all JavaScript conversions I've come across (and / or made) are always done in the format hh:mm:ss, or are just output incorrectly.
All help is really appreciated,
Cheers.
Just change your return statement to
return parseInt( hours ) > 0 ? ( hours + ":" + minutes + ":" + seconds ) : ( minutes + ":" + seconds ) ;
Simply don't return hours if hours is not greater than 0
In case, your minutes need to be trimmed if there are no hours then update your padding logic to
if(hours > 0 && minutes < 10){ minutes = "0" + minutes; }
A more compact version of #gurvinder372 response
return (parseInt(hours) > 0 ? hours + ":" : "") + minutes + ":" + seconds;

Adding AM/PM using javascript [duplicate]

This question already has answers here:
How do you display JavaScript datetime in 12 hour AM/PM format?
(31 answers)
Closed 6 years ago.
I have simple code for 12hr format time
// This function gets the current time and injects it into the DOM
function updateClock() {
// Gets the current time
var now = new Date();
// Get the hours, minutes and seconds from the current time
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
// Format hours, minutes and seconds
if (hours > 12) {
hours = hours - 12;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
// Gets the element we want to inject the clock into
var elem = document.getElementById('clock');
// Sets the elements inner HTML value to our clock data
elem.innerHTML = hours + ':' + minutes + ':' + seconds ;
}
I want to add AM/PM please help me thanks in advance
Im just beginner on javascript
After editting your own code:
// Get the hours, minutes and seconds from the current time
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var amOrPm = 'AM';
// Format hours, minutes and seconds
if (hours > 12) {
amOrPm = 'PM';
hours = hours - 12;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
// Gets the element we want to inject the clock into
var elem = document.getElementById('clock');
// Sets the elements inner HTML value to our clock data
elem.innerHTML = hours + ':' + minutes + ':' + seconds + ' ' + amOrPm;
Try this...
var ampm = hours >= 12 ? 'pm' : 'am';
then
elem.innerHTML = hours + ':' + minutes + ':' + seconds + ' ' + ampm;
I don't know if you want to code it by yourself, but there is a really really great library which handles everything with date stuff you can imagine.
Just checkout momentjs.
It's an easy AF lib to transform dates (also in pm/am).
If you've any question :) just comment ...

Increment time interval by 15 minutes in javascript

As stated in the example below, I would like to create an array that is incremented with 15 minute interval.. Irrespective of the getTime() , the array should start from 12.00 AM until 11.45 P.M. Example:
[12.00 AM, 12.15 AM, 12.45 AM, 1.00 AM ... 11.45 P.M.]
Thank you for looking. I found the solution of what I needed.
var hours, minutes, ampm;
var time = [];
for(var i = 0; i <= 1440; i += 15){
hours = Math.floor(i / 60);
minutes = i % 60;
if (minutes < 10){
minutes = '0' + minutes; // adding leading zero
}
ampm = hours % 24 < 12 ? 'AM' : 'PM';
hours = hours % 12;
if (hours === 0){
hours = 12;
}
time.push(hours + ':' + minutes + ' ' + ampm);
}
document.getElementById("Time").innerText = time ;

Javascript date time two hours off

I have a javascript code set into my webpage but the date time is always two hours off. If anyone knows what's wrong please help.
Here's my relevant JavaScript code:
function show() {
var Digital = new Date()
var hours = Digital.getHours()
var minutes = Digital.getMinutes()
var seconds = Digital.getSeconds()
var dn = "AM"
if (hours > 12) {
dn = "PM"
hours = hours - 12
}
if (hours == 0) c
hours = 12
if (minutes <= 9)
minutes = "0" + minutes
if (seconds <= 9)
seconds = "0" + seconds
document.dform.currenttime.value = hours + ":" + minutes + ":" + seconds + " " + dn
setTimeout("show()", 1000)
}
show();
You have a c right here:
if (hours==0)c
Delete the c. It works. You're welcome.
I suggest proofreading your code before you come asking for help, but more importantly you should format your code so that it's legible enough to proofread. As an example:
function show() {
var Digital = new Date();
var hours = Digital.getHours();
var minutes = Digital.getMinutes();
var seconds = Digital.getSeconds();
var dn = "AM";
if(hours > 12) {
dn = "PM";
hours -= 12;
}
if(hours == 0) hours = 12;
if(minutes <= 9) minutes = "0" + minutes;
if(seconds <= 9) seconds = "0" + seconds;
document.dform.currenttime.value = hours + ":" + minutes + ":" + seconds + " " + dn;
}
var clock = setInterval(show, 1000);
This is easier to read and you likely would've noticed the erroneous c.

Categories