How can I convert float number into minutes to be added to the current UTC time with Javascript? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How can I convert float number into minutes to be added to the current UTC time with Javascript? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I apologise in advance for being a complete dunce but these are in fact my very first shaky steps into trying to get something done with Javascript and I feel somewhat lost and confused.

I have the following float: 53.93

This decimal value represents minutes and seconds and it comes from multiplying the longitude I get from the web-browser´s Geolocation API times four (x4). I have this value stored in a variable which I want to convert into minutes and seconds so that I can then add it to the current UTC time so that I can get a new HH:MM:SS time on screen with the added difference(current UTC Time + 53 minutes and 93 seconds.)

I understand that I should first convert times into milliseconds in order to be able to calculate the time difference but I’m stuck at converting the float into minutes and seconds (or should I convert it directly into milliseconds?)

Thank you kindly.

Answer

JavaScript’s Date works in milliseconds, so if you have a number representing minutes (whether or not including fractional minutes), you can convert that to milliseconds by multiplying it by 60 (to get seconds) and 1000 (to get milliseconds), e.g., multiply by 60,000. But, you’ve said you have the value 13.48 and later in the text of your question you’ve said “…current UTC Time + 13 minutes and 48 seconds…” Those are two different things. .48 of a minute is roughly 28.79 seconds, not 48 seconds. So if you really mean that the figure 13.48 is supposed to mean 13 minutes and 48 seconds, the calculation is more compliated:

const value = 13.48;
const wholeMinutes = Math.trunc(value);
const milliseconds = (wholeMinutes * 60 + (value - wholeMinutes)) * 1000;

You can get the current date/time via Date.now(), which gives you milliseconds since The Epoch UTC (Jan 1st 1970 at midnight).

You can create a Date instance from a given milliseconds-since-The-Epoch value via new Date(value).

So if 13.48 represents fractional minutes (13 minutes and roughly 28.79 seconds):

const minutes = 13.48;
const nowPlusThoseMinutes = new Date(Date.now() + (minutes * 60000));

Live Example:

const minutes = 13.48;
const now = Date.now();
const nowPlusThoseMinutes = new Date(now + (minutes * 60000));
console.log(nowPlusThoseMinutes);
console.log(`now                 = ${new Date(now)}`);
console.log(`nowPlusThoseMinutes = ${nowPlusThoseMinutes}`);

(The () around minutes * 60000 aren’t necessary, the * will happen before the + either way because of operator precedence, but they can help make your intent clear to human readers.)

But you mean it to mean 13 minutes and 48 seconds:

const value = 13.48;
const wholeMinutes = Math.trunc(value);
const milliseconds = (wholeMinutes * 60 + (value - wholeMinutes)) * 1000;
const nowPlusValue = new Date(Date.now() + milliseconds);

Live Example:

const value = 13.48;
const wholeMinutes = Math.trunc(value);
const milliseconds = (wholeMinutes * 60 + (value - wholeMinutes)) * 1000;
const now = Date.now();
const nowPlusValue = new Date(now + milliseconds);
console.log(`now          = ${new Date(now)}`);
console.log(`nowPlusValue = ${nowPlusValue}`);
We are here to answer your question about How can I convert float number into minutes to be added to the current UTC time with Javascript? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji