can someone help me out to write a function in react native to convert 234mins to 3hrs:54mins, and when it is less than 1hr, it shows just 59mins instead of 0hrs:59mins. Kindly indicate if I should give an example code.
Here it is:
const convertMinsToTime = (mins) => {
let hours = Math.floor(mins / 60);
let minutes = mins % 60;
minutes = minutes < 10 ? '0' + minutes : minutes;
return `${hours}hrs:${minutes}mins`;
}
Related
I'm converting elapsed ms to HH:mm:ss, but if the elapsed ms are higher of a day, I lost that info:
const elapsedSeconds = 218509
const elapsed = moment.utc(elapsedSeconds * 1000).format('HH:mm:ss');
alert(elapsed); // print 12:41:49
How can I also display days from ms, near the HH:mm:ss?
In this case there are 60hours, so it should print 2(days):12:41:49. Or 60:41:49 at least.
So based on your required ouput you can use moment library to convert date time to specified format.
For that your code looks like :
const elapsedMilliseconds = 218509000;
const duration = moment.duration(elapsedMilliseconds);
const elapsed = duration.days() + "(days):" + duration.hours() + ":" + duration.minutes() + ":" + duration.seconds();
alert(elapsed);
Result :
2(days):12:41:49
If you want to do by javascript to get total HH:mm:ss then :
function padTo2Digits(num) {
return num.toString().padStart(2, '0');
}
function convertMsToHM(milliseconds) {
let seconds = Math.floor(milliseconds / 1000);
let minutes = Math.floor(seconds / 60);
let hours = Math.floor(minutes / 60);
seconds = seconds % 60;
minutes = seconds >= 30 ? minutes + 1 : minutes;
minutes = minutes % 60;
return `${padTo2Digits(hours)}:${padTo2Digits(minutes)}:${padTo2Digits(seconds)}`;
}
console.log(convertMsToHM(218509000));
Result :
"60:42:49"
Okay, so I am trying to calculate the time since 9:30 in the morning in Google Apps Script, and I want the output to look like this: XX hrs XX mins. the problem is when I try calculating the minutes since 9:30, of course, it gives me all the minutes, not just the leftover minutes after I've calculated the hours. I need the minutes to be a decimal so I can times it by 60 and display the output in a cell. This is the code I'm currently using:
function CALCTIME() {
const minutes = 1000 * 60;
const hours = minutes * 60;
const days = hours * 24;
const years = days * 365;
var now = new Date(),
then = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate(),
9,30,0),
diff = now.getTime() - then.getTime();
let hrs = diff / hours;
let mins = Math.floor((diff / minutes) % 60);
return Math.floor(hrs) + " hrs " + mins + " mins";
}
The issue is not the hrs, I have that all good. The minutes are the problem because I can't figure out how to replace just an index from a string. I've looked and tried the methods shown on these web pages and Stack Exchange links for answers and I couldn't find any:
https://www.w3schools.com/jsref/jsref_replace.asp
How do I replace a character at a particular index in JavaScript?
Questions: What do you expect these statements to do and why? mins.replaceAt(0, "0."); mins % 60; The first statement I expected to replace the first character in mins with "0." but then, #jabaa pointed out that I couldn't replace a number for a string, which I totally forgot and didn't take into account. The second statement I just forgot to put mins = mins % 60; which probably wouldn't have solved my problem anyway, I just forgot to put that there.
I've answered your questions, but someone has already answered my questions.
The reason it is not working is because you have:
diff = now.getTime() - then.getTime();
That line is going to get the time difference from now and 9:30am.
var hrs = diff / hours;
var mins = diff / minutes;
The two lines above are getting their own things. The first is how many hours and the second is how many minutes. So inherently you will be getting all the minutes and not the leftovers. There are multiple ways to fix it. Below is one way where the hours are right, so we take out every full hour from the minute's section.
Could look something like this:
let hrs = diff / hours;
let mins = (diff / minutes) % 60;
ALSO: The following line of code you have does nothing because you're not giving it anywhere to be stored in.
mins % 60
To fix you can do something like:
let testvar = mins % 60;
This question already has answers here:
Convert seconds to days, hours, minutes and seconds
(12 answers)
Closed 2 years ago.
I have tried different functions I found in SO but none give me a precise output.
I did one variant of this (adding days and changing to d:h:m)
const convertMinsToHrsMins = (mins) => {
let h = Math.floor(mins / 60);
let m = mins % 60;
h = h < 10 ? '0' + h : h;
m = m < 10 ? '0' + m : m;
return `${h}:${m}`;
}
my last attempt:
// Convert Minutes to Days Hours Minutes
const convertMinutes = (totalMinutes) => {
let Days = Math.floor((totalMinutes / 1440) % 60)
let Hours = Math.floor(totalMinutes / 60)
let Minutes = Math.round(totalMinutes % 60)
let ret = ''
if (Days > 0) {
ret += '' + Days + 'd ' + (Hours < 10 ? '0' : '')
}
ret += '' + Hours + 'h ' + (Minutes < 10 ? '0' : '')
ret += '' + Minutes + 'm'
return ret
}
totalMinutes receive a sum of different inputs(all in minutes!). I need this function to work as close as precise as possible. e.g.: convertMinutes(totalminutes)
937d 23h 59m 8d 00h 01m
Convert 1 day, 1 hour and 1 minute into minutes; subtract one day from your input till you can't anymore; then subtract one hour from your input till you can't anymore; then subtract one minute from your input till you can't anymore or either return the rest. You have to respect that order. Here is a function that mets your necessity:
function converter(minutes)
{
dates=[1440,60,1]
output=[0,0,0];
for(x=0; x<3; x++)
{
while(minutes>=dates[x])
{
minutes-=dates[x]
output[x]++
}
}
return output[0]+"Days;"+output[1]+"Hours;"+output[2]+"Minutes."
}
Use division to get the days, then use modulo to get the remaining minutes which don't sum up to a full day. Use the remaining minutes and and do the same with hours (division and modulo).
const convertMinutes = (totalMinutes) => {
const minutesInDay = 24 * 60;
const minutesInHour = 60;
let days = Math.floor(totalMinutes / minutesInDay);
let remainingMinutes = totalMinutes % minutesInDay;
let hours = Math.floor(remainingMinutes / minutesInHour);
let minutes = remainingMinutes % minutesInHour;
return `${days}d:${hours}h:${minutes}`;
}
On my project requires this kind of text response on a day and hours.
"2hrs"-- data as milliseconds, If i get 1h 30mins, It should be rounded-up to 2hrs.
i tried so many times but cannot catch the value. now am getting 1 hrs for below function.
can anyone help me to do this ? here is the function which i am using
const milliSec = 85600000;
const hrs = moment(moment.duration(milliSec)._data).format('HH[hrs]');
There are a couple of ways to solve your problem
Simple logic
let milliseconds = 85600000;
let hours = Math.floor(milliseconds/(1000*3600))
let minutes = Math.floor(milliseconds/(1000*60)) - hours * 60
if(minutes > 29){
console.log(hours + 1);
}
console.log(hours)
Using Moment.js
let milliseconds = 85600000
let hours = Math.floor(moment.duration(milliseconds).asHours())
let mins = Math.floor(moment.duration(milliseconds).asMinutes()) - hours * 60;
if(minutes > 29){
console.log(hours + 1);
}
console.log(hours)
Hope this helps you. Feel free for doubts.
I'm looking to turn 165 seconds into 2:40 not 0:2:45
The function needs to be able to adapt to how large the seconds value is.
I know there's infinite ways to do this, but I'm looking for a clean way to do it without any external libraries other than jQuery.
Something like: [Math.floor(165/60),165%60].join(':') should work. Actually, it's 2:45 ;~)
[edit] based on your comment a function to convert seconds into a (zero padded, hours trimmed) hr:mi:se string
function hms(sec){
var hr = parseInt(sec/(60*60),10)
, mi = parseInt(sec/60,10)- (hr*60)
, se = sec%60;
return [hr,mi,se]
.join(':')
.replace(/\b\d\b/g,
function(a){
return Number(a)===0 ? '00' : a<10? '0'+a : a;
}
)
.replace(/^00:/,'');
}
alert(hms(165)); //=> 02:45
alert(hms(3850)); //=> 01:04:10
Check this answer : Convert seconds to HH-MM-SS with JavaScript?
hours = totalSeconds / 3600;
totalSeconds %= 3600;
minutes = totalSeconds / 60;
seconds = totalSeconds % 60;
Try something like this (i've included padding to format the digits to two characters each):
String.prototype.padLeft = function(n, pad)
{
t = '';
if (n > this.length){
for (i = 0; i < n - this.length; i++) {
t += pad;
}
}
return t + this;
}
var seconds = 3850;
var hours = Math.floor(seconds / 3600);
var minutes = Math.floor(seconds % 3600 / 60);
var time = [hours.toString().padLeft(2, '0'),
minutes.toString().padLeft(2, '0'),
(seconds % 60).toString().padLeft(2, '0')].join(':');