-2

I have a web service that returns the current time in Decimal format. How I can convert it to a human-readable format using JavaScript.

Here is what documentation says. The web service returns dates and times as floating-point values. The fraction represents the time (0.5 is noon).

e.g.

 0.5 is 12:00 

 0.681944 is 16:22 
Abdul Waheed
  • 383
  • 1
  • 4
  • 20

1 Answers1

2

Convert the decimal representation to seconds in a day

const seconds = decimalData * 60 * 60 * 24

Take the reminder of 60 to the the seconds

const sec = decimalData % 60

Take the reminder of 3600 as the minutes divided by 60

const minutes = Math.floor((second % 3600) / 60)

Find the hour by div with 3600

const hours = Math.floor(seconds / 3600)

Now you just have to convert it to a string.

const formatted = hours.toString().padStart(2, '0') + ":" + minutes.toString().padStart(2, '0') + ":" + sec.toString().padStart(2, '0')
Radu Diță
  • 13,476
  • 2
  • 30
  • 34
  • Thanks, @Radu Dita; can you please add the seconds part as well. How the seconds will be calculated in formated constant. – Abdul Waheed Apr 07 '20 at 08:46
  • Dita maybe a bit off to the actual question. Is this possible to get the current system time in Decimal format? The same format in which I get the time from web service. I actually want to calculate the time difference in seconds between (current-system-time - web-service-time). – Abdul Waheed Apr 07 '20 at 12:06
  • Yes, you can, but you should post another question regarding this. – Radu Diță Apr 07 '20 at 12:11
  • Dita could you please answer here; https://stackoverflow.com/questions/61081381/how-to-get-current-time-in-seconds-decimal-using-javascript – Abdul Waheed Apr 07 '20 at 13:41