0

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.

  • *"...it comes from multiplying the longitude I get from the web-browser´s Geolocation API times four (x4)..."* Huh? The "minutes" used in longitude and latitude are not related to the minutes used in time. (The first is definition 1b [here](https://www.merriam-webster.com/dictionary/minute), the second is definition 1a.) – T.J. Crowder Feb 20 '21 at 12:56
  • Please visit [help], take [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output, preferably in a [Stacksnippet](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) – mplungjan Feb 20 '21 at 13:02
  • 1
    13 minutes and 48 seconds are not 13.48, 13.48 is 13m29s; 13.80 is 13m48s – mplungjan Feb 20 '21 at 13:03

1 Answers1

0

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}`);
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 13 minutes and 48 seconds are not 13.48 – mplungjan Feb 20 '21 at 13:03
  • @mplungjan - I didn't say is was. If the OP means 13 minutes and 48 seconds, that would be different than a fractional minutes value of 13.48 (approximately 13 minutes and 28.79 seconds). But my read is that they have fractional minutes value. – T.J. Crowder Feb 20 '21 at 13:04
  • @mplungjan - Oh, wait, later in the text they do say *"current UTC Time + 13 minutes and 48 seconds"*. – T.J. Crowder Feb 20 '21 at 13:05
  • Actually I goofed with the given number which I have now corrected above (53.93 not 13.48, sorry), but I managed to solve this already based on your answer for actual minutes of the not-fractional kind (the second example T.J Crowder gave) Again, thank you kindly and I´ll try to improve my clarity when asking questions in the future. – Kiefer 25-WR Feb 20 '21 at 15:12