1

I have these epochs:

1586998726 and "1586998726"

I need to turn both to:

1586998726000 and "1586998726000"

Can't really figure out. Help appreciated.

4
  • + works fine for concatenating strings. For the number, just multiply by 1000. Commented Apr 16, 2020 at 1:04
  • And in modern JS, use template strings. `${yourval}000`, done. ("modern" with a grain of salt, everything except IE11 has had support for it for years now) Commented Apr 16, 2020 at 1:07
  • @Mike'Pomax'Kamermans yeah but one needs output to be number and other needs string i guess by posted output format Commented Apr 16, 2020 at 1:13
  • 1
    Your epoch value should be a number, wherever you got that string from, you should just immediately cast it to a number at the edges of your system, and never let bad data like this flow through your inner layers. Commented Apr 16, 2020 at 1:27

1 Answer 1

3

If you have

var epochInteger = 1586998726;
var epochString = "1586998726";

You can do:

For the number

epochInteger * 1000 === 1586998726000;

For the string

epochString + "000" === "1586998726000";

To do conversions between them

epochInteger.toString() === epochString;
Number(epochString) === epochInteger;

But note that these conversions work in both cases

epochString.toString() === epochString;
Number(epochInteger) === epochInteger;

So in general you could use something like

var modifiedEpochInteger = Number(epochAnyType) * 1000;
var modifiedEpochString = modifiedEpochInteger.toString();

and you'd get the result in both types, no matter where you started!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.