Ad

How To Calculate Offset Of 4 Hours And 30 Minutes Within Function WorldClockZone

- 1 answer

I want to calculate below code with 4 hours and 30 minutes to GMT but I have no idea to add 30 minutes in this code, can anyone help me?

    function worldClockZone(){
document.getElementById("Dehli").innerHTML = worldClock(4, "India")

While Dehli is City, 4 is the time offset (which I need to 04:30 instead) and India is region.

Thanks in advance

Ad

Answer

I'm going to assume you are using the following code:

https://www.proglogic.com/code/javascript/time/worldclock.php

The code seems to match, so I'll give it a go as it's generic enough and might be of use to others as well. As is, the function worldClock does not support this behaviour. However, you could do some tinkering. Add the following:

// handle float
var remainder = Math.floor( ( zone % 1 ) * 60 )
zone = Math.floor( zone )

before adding the zone parameter to hr:

var hr = gmtTime.getHours() + zone
var min = gmtTime.getMinutes()
var sec = gmtTime.getSeconds()

afterwards handling the remaining minutes:

// make sure to adjust hr and min accordingly
min += Math.abs( remainder )
hr += ( remainder != 0 && min >= 60 ? 1 : 0 )
min %= 60

and then call:

function worldClockZone(){
document.getElementById("Dehli").innerHTML = worldClock(4.5, "India")

This should work for all float numbers, including negatives.

Ad
source: stackoverflow.com
Ad