UTC live clock, not working with negative timezone





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















In my application, I need to create live clock according to different timezones which are stored into database.



I have almost succeeded it.



But now I'm facing negative time in clock, and I'm out of ideas to figure out a solution.



I'm getting UTC time with the help of new Date() and calculating time with provided timezone from database.



Case 1: 0:31 (UTC time) + 5:30 (timezone) = '06:01'



Case 2: 06:31 (UTC time) - 6:30 (timezone) = '00:01'



Case 3: 5:0 (UTC time) - 7:0 (timezone) = '-02:00'



Case 1 and 2 is working properly but I'm getting negative value in 3rd case which is wrong.



I have tried to add comments in code to have better understanding of what I'm doing here. I hope it helps.



Any help will be highly appreciated.






function runClock() {

setInterval(function() {
//debugger
var time = new Date();
// take timezone from HTML element
// ex: +:5:30
var getTimezone = "-:7:0" //$("#timeZone").text();

// split into array to get oparator (Positive and Negative Timezone)
var oparator = getTimezone.split(":")[0];
var timezone = getTimezone.split(":")[1] + ":" + getTimezone.split(":")[2];

// get UTC hours
var hours = 5 //time.getUTCHours();
var minutes = 0 //time.getUTCMinutes();
var UTCTIME = timeStringToFloat(hours + ":" + minutes);
var TIMEZONEOFFSETTIME = timeStringToFloat(timezone);
var finalTime = "";


// Convert time folloed by Colon into decimals
// ex: 1:45 = 1.75
function timeStringToFloat(time) {
var hoursMinutes = time.split(/[.:]/);
var hh = parseInt(hoursMinutes[0], 10);
var mm = hoursMinutes[1] ? parseInt(hoursMinutes[1], 10) : 0;
return hh + mm / 60;
}


// Convert time folloed by float into Colon
// ex: 1.75 = 1:45
function floatToTime(FT) {
var splittedTime = FT.toString().split(".");
var hh = splittedTime[0];
var mm = "";

if (splittedTime[1]) {
mm = Math.round((splittedTime[1] / 100) * 60);
} else {
mm = "0";
}

finalTime = hh + ":" + ((mm < 10) ? ("0" + mm) : mm);
}


// Calculate time (UTC + or - Timezone)
// Ex: 00:15 (UTC) + 5:30 = 5:45
function CalcTime(UTCTIME, TIMEZONEOFFSETTIME) {
if (oparator == "+") {
var FT = UTCTIME + TIMEZONEOFFSETTIME;
FT = FT.toFixed(2);
floatToTime(FT);
} else {
var FT = UTCTIME - TIMEZONEOFFSETTIME;
FT = FT.toFixed(2);
floatToTime(FT);
}
}


// Parse Seconds
function seconds() {
var j = "";
if (time.getUTCSeconds() < 10) {
j = "0" + time.getUTCSeconds();
} else {
j = time.getUTCSeconds()
}
return j;
}

CalcTime(UTCTIME, TIMEZONEOFFSETTIME);

$("#clockTime").text(finalTime + ":" + seconds());
}, 1000);

}
runClock();

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<b id="clockTime"></b>












share|improve this question


















  • 2





    Relevant: youtu.be/-5wpm-gesOY

    – deceze
    Jan 4 at 6:54


















1















In my application, I need to create live clock according to different timezones which are stored into database.



I have almost succeeded it.



But now I'm facing negative time in clock, and I'm out of ideas to figure out a solution.



I'm getting UTC time with the help of new Date() and calculating time with provided timezone from database.



Case 1: 0:31 (UTC time) + 5:30 (timezone) = '06:01'



Case 2: 06:31 (UTC time) - 6:30 (timezone) = '00:01'



Case 3: 5:0 (UTC time) - 7:0 (timezone) = '-02:00'



Case 1 and 2 is working properly but I'm getting negative value in 3rd case which is wrong.



I have tried to add comments in code to have better understanding of what I'm doing here. I hope it helps.



Any help will be highly appreciated.






function runClock() {

setInterval(function() {
//debugger
var time = new Date();
// take timezone from HTML element
// ex: +:5:30
var getTimezone = "-:7:0" //$("#timeZone").text();

// split into array to get oparator (Positive and Negative Timezone)
var oparator = getTimezone.split(":")[0];
var timezone = getTimezone.split(":")[1] + ":" + getTimezone.split(":")[2];

// get UTC hours
var hours = 5 //time.getUTCHours();
var minutes = 0 //time.getUTCMinutes();
var UTCTIME = timeStringToFloat(hours + ":" + minutes);
var TIMEZONEOFFSETTIME = timeStringToFloat(timezone);
var finalTime = "";


// Convert time folloed by Colon into decimals
// ex: 1:45 = 1.75
function timeStringToFloat(time) {
var hoursMinutes = time.split(/[.:]/);
var hh = parseInt(hoursMinutes[0], 10);
var mm = hoursMinutes[1] ? parseInt(hoursMinutes[1], 10) : 0;
return hh + mm / 60;
}


// Convert time folloed by float into Colon
// ex: 1.75 = 1:45
function floatToTime(FT) {
var splittedTime = FT.toString().split(".");
var hh = splittedTime[0];
var mm = "";

if (splittedTime[1]) {
mm = Math.round((splittedTime[1] / 100) * 60);
} else {
mm = "0";
}

finalTime = hh + ":" + ((mm < 10) ? ("0" + mm) : mm);
}


// Calculate time (UTC + or - Timezone)
// Ex: 00:15 (UTC) + 5:30 = 5:45
function CalcTime(UTCTIME, TIMEZONEOFFSETTIME) {
if (oparator == "+") {
var FT = UTCTIME + TIMEZONEOFFSETTIME;
FT = FT.toFixed(2);
floatToTime(FT);
} else {
var FT = UTCTIME - TIMEZONEOFFSETTIME;
FT = FT.toFixed(2);
floatToTime(FT);
}
}


// Parse Seconds
function seconds() {
var j = "";
if (time.getUTCSeconds() < 10) {
j = "0" + time.getUTCSeconds();
} else {
j = time.getUTCSeconds()
}
return j;
}

CalcTime(UTCTIME, TIMEZONEOFFSETTIME);

$("#clockTime").text(finalTime + ":" + seconds());
}, 1000);

}
runClock();

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<b id="clockTime"></b>












share|improve this question


















  • 2





    Relevant: youtu.be/-5wpm-gesOY

    – deceze
    Jan 4 at 6:54














1












1








1








In my application, I need to create live clock according to different timezones which are stored into database.



I have almost succeeded it.



But now I'm facing negative time in clock, and I'm out of ideas to figure out a solution.



I'm getting UTC time with the help of new Date() and calculating time with provided timezone from database.



Case 1: 0:31 (UTC time) + 5:30 (timezone) = '06:01'



Case 2: 06:31 (UTC time) - 6:30 (timezone) = '00:01'



Case 3: 5:0 (UTC time) - 7:0 (timezone) = '-02:00'



Case 1 and 2 is working properly but I'm getting negative value in 3rd case which is wrong.



I have tried to add comments in code to have better understanding of what I'm doing here. I hope it helps.



Any help will be highly appreciated.






function runClock() {

setInterval(function() {
//debugger
var time = new Date();
// take timezone from HTML element
// ex: +:5:30
var getTimezone = "-:7:0" //$("#timeZone").text();

// split into array to get oparator (Positive and Negative Timezone)
var oparator = getTimezone.split(":")[0];
var timezone = getTimezone.split(":")[1] + ":" + getTimezone.split(":")[2];

// get UTC hours
var hours = 5 //time.getUTCHours();
var minutes = 0 //time.getUTCMinutes();
var UTCTIME = timeStringToFloat(hours + ":" + minutes);
var TIMEZONEOFFSETTIME = timeStringToFloat(timezone);
var finalTime = "";


// Convert time folloed by Colon into decimals
// ex: 1:45 = 1.75
function timeStringToFloat(time) {
var hoursMinutes = time.split(/[.:]/);
var hh = parseInt(hoursMinutes[0], 10);
var mm = hoursMinutes[1] ? parseInt(hoursMinutes[1], 10) : 0;
return hh + mm / 60;
}


// Convert time folloed by float into Colon
// ex: 1.75 = 1:45
function floatToTime(FT) {
var splittedTime = FT.toString().split(".");
var hh = splittedTime[0];
var mm = "";

if (splittedTime[1]) {
mm = Math.round((splittedTime[1] / 100) * 60);
} else {
mm = "0";
}

finalTime = hh + ":" + ((mm < 10) ? ("0" + mm) : mm);
}


// Calculate time (UTC + or - Timezone)
// Ex: 00:15 (UTC) + 5:30 = 5:45
function CalcTime(UTCTIME, TIMEZONEOFFSETTIME) {
if (oparator == "+") {
var FT = UTCTIME + TIMEZONEOFFSETTIME;
FT = FT.toFixed(2);
floatToTime(FT);
} else {
var FT = UTCTIME - TIMEZONEOFFSETTIME;
FT = FT.toFixed(2);
floatToTime(FT);
}
}


// Parse Seconds
function seconds() {
var j = "";
if (time.getUTCSeconds() < 10) {
j = "0" + time.getUTCSeconds();
} else {
j = time.getUTCSeconds()
}
return j;
}

CalcTime(UTCTIME, TIMEZONEOFFSETTIME);

$("#clockTime").text(finalTime + ":" + seconds());
}, 1000);

}
runClock();

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<b id="clockTime"></b>












share|improve this question














In my application, I need to create live clock according to different timezones which are stored into database.



I have almost succeeded it.



But now I'm facing negative time in clock, and I'm out of ideas to figure out a solution.



I'm getting UTC time with the help of new Date() and calculating time with provided timezone from database.



Case 1: 0:31 (UTC time) + 5:30 (timezone) = '06:01'



Case 2: 06:31 (UTC time) - 6:30 (timezone) = '00:01'



Case 3: 5:0 (UTC time) - 7:0 (timezone) = '-02:00'



Case 1 and 2 is working properly but I'm getting negative value in 3rd case which is wrong.



I have tried to add comments in code to have better understanding of what I'm doing here. I hope it helps.



Any help will be highly appreciated.






function runClock() {

setInterval(function() {
//debugger
var time = new Date();
// take timezone from HTML element
// ex: +:5:30
var getTimezone = "-:7:0" //$("#timeZone").text();

// split into array to get oparator (Positive and Negative Timezone)
var oparator = getTimezone.split(":")[0];
var timezone = getTimezone.split(":")[1] + ":" + getTimezone.split(":")[2];

// get UTC hours
var hours = 5 //time.getUTCHours();
var minutes = 0 //time.getUTCMinutes();
var UTCTIME = timeStringToFloat(hours + ":" + minutes);
var TIMEZONEOFFSETTIME = timeStringToFloat(timezone);
var finalTime = "";


// Convert time folloed by Colon into decimals
// ex: 1:45 = 1.75
function timeStringToFloat(time) {
var hoursMinutes = time.split(/[.:]/);
var hh = parseInt(hoursMinutes[0], 10);
var mm = hoursMinutes[1] ? parseInt(hoursMinutes[1], 10) : 0;
return hh + mm / 60;
}


// Convert time folloed by float into Colon
// ex: 1.75 = 1:45
function floatToTime(FT) {
var splittedTime = FT.toString().split(".");
var hh = splittedTime[0];
var mm = "";

if (splittedTime[1]) {
mm = Math.round((splittedTime[1] / 100) * 60);
} else {
mm = "0";
}

finalTime = hh + ":" + ((mm < 10) ? ("0" + mm) : mm);
}


// Calculate time (UTC + or - Timezone)
// Ex: 00:15 (UTC) + 5:30 = 5:45
function CalcTime(UTCTIME, TIMEZONEOFFSETTIME) {
if (oparator == "+") {
var FT = UTCTIME + TIMEZONEOFFSETTIME;
FT = FT.toFixed(2);
floatToTime(FT);
} else {
var FT = UTCTIME - TIMEZONEOFFSETTIME;
FT = FT.toFixed(2);
floatToTime(FT);
}
}


// Parse Seconds
function seconds() {
var j = "";
if (time.getUTCSeconds() < 10) {
j = "0" + time.getUTCSeconds();
} else {
j = time.getUTCSeconds()
}
return j;
}

CalcTime(UTCTIME, TIMEZONEOFFSETTIME);

$("#clockTime").text(finalTime + ":" + seconds());
}, 1000);

}
runClock();

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<b id="clockTime"></b>








function runClock() {

setInterval(function() {
//debugger
var time = new Date();
// take timezone from HTML element
// ex: +:5:30
var getTimezone = "-:7:0" //$("#timeZone").text();

// split into array to get oparator (Positive and Negative Timezone)
var oparator = getTimezone.split(":")[0];
var timezone = getTimezone.split(":")[1] + ":" + getTimezone.split(":")[2];

// get UTC hours
var hours = 5 //time.getUTCHours();
var minutes = 0 //time.getUTCMinutes();
var UTCTIME = timeStringToFloat(hours + ":" + minutes);
var TIMEZONEOFFSETTIME = timeStringToFloat(timezone);
var finalTime = "";


// Convert time folloed by Colon into decimals
// ex: 1:45 = 1.75
function timeStringToFloat(time) {
var hoursMinutes = time.split(/[.:]/);
var hh = parseInt(hoursMinutes[0], 10);
var mm = hoursMinutes[1] ? parseInt(hoursMinutes[1], 10) : 0;
return hh + mm / 60;
}


// Convert time folloed by float into Colon
// ex: 1.75 = 1:45
function floatToTime(FT) {
var splittedTime = FT.toString().split(".");
var hh = splittedTime[0];
var mm = "";

if (splittedTime[1]) {
mm = Math.round((splittedTime[1] / 100) * 60);
} else {
mm = "0";
}

finalTime = hh + ":" + ((mm < 10) ? ("0" + mm) : mm);
}


// Calculate time (UTC + or - Timezone)
// Ex: 00:15 (UTC) + 5:30 = 5:45
function CalcTime(UTCTIME, TIMEZONEOFFSETTIME) {
if (oparator == "+") {
var FT = UTCTIME + TIMEZONEOFFSETTIME;
FT = FT.toFixed(2);
floatToTime(FT);
} else {
var FT = UTCTIME - TIMEZONEOFFSETTIME;
FT = FT.toFixed(2);
floatToTime(FT);
}
}


// Parse Seconds
function seconds() {
var j = "";
if (time.getUTCSeconds() < 10) {
j = "0" + time.getUTCSeconds();
} else {
j = time.getUTCSeconds()
}
return j;
}

CalcTime(UTCTIME, TIMEZONEOFFSETTIME);

$("#clockTime").text(finalTime + ":" + seconds());
}, 1000);

}
runClock();

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<b id="clockTime"></b>





function runClock() {

setInterval(function() {
//debugger
var time = new Date();
// take timezone from HTML element
// ex: +:5:30
var getTimezone = "-:7:0" //$("#timeZone").text();

// split into array to get oparator (Positive and Negative Timezone)
var oparator = getTimezone.split(":")[0];
var timezone = getTimezone.split(":")[1] + ":" + getTimezone.split(":")[2];

// get UTC hours
var hours = 5 //time.getUTCHours();
var minutes = 0 //time.getUTCMinutes();
var UTCTIME = timeStringToFloat(hours + ":" + minutes);
var TIMEZONEOFFSETTIME = timeStringToFloat(timezone);
var finalTime = "";


// Convert time folloed by Colon into decimals
// ex: 1:45 = 1.75
function timeStringToFloat(time) {
var hoursMinutes = time.split(/[.:]/);
var hh = parseInt(hoursMinutes[0], 10);
var mm = hoursMinutes[1] ? parseInt(hoursMinutes[1], 10) : 0;
return hh + mm / 60;
}


// Convert time folloed by float into Colon
// ex: 1.75 = 1:45
function floatToTime(FT) {
var splittedTime = FT.toString().split(".");
var hh = splittedTime[0];
var mm = "";

if (splittedTime[1]) {
mm = Math.round((splittedTime[1] / 100) * 60);
} else {
mm = "0";
}

finalTime = hh + ":" + ((mm < 10) ? ("0" + mm) : mm);
}


// Calculate time (UTC + or - Timezone)
// Ex: 00:15 (UTC) + 5:30 = 5:45
function CalcTime(UTCTIME, TIMEZONEOFFSETTIME) {
if (oparator == "+") {
var FT = UTCTIME + TIMEZONEOFFSETTIME;
FT = FT.toFixed(2);
floatToTime(FT);
} else {
var FT = UTCTIME - TIMEZONEOFFSETTIME;
FT = FT.toFixed(2);
floatToTime(FT);
}
}


// Parse Seconds
function seconds() {
var j = "";
if (time.getUTCSeconds() < 10) {
j = "0" + time.getUTCSeconds();
} else {
j = time.getUTCSeconds()
}
return j;
}

CalcTime(UTCTIME, TIMEZONEOFFSETTIME);

$("#clockTime").text(finalTime + ":" + seconds());
}, 1000);

}
runClock();

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<b id="clockTime"></b>






javascript html datetime






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 4 at 6:33









Abhishek PandeyAbhishek Pandey

10.2k82249




10.2k82249








  • 2





    Relevant: youtu.be/-5wpm-gesOY

    – deceze
    Jan 4 at 6:54














  • 2





    Relevant: youtu.be/-5wpm-gesOY

    – deceze
    Jan 4 at 6:54








2




2





Relevant: youtu.be/-5wpm-gesOY

– deceze
Jan 4 at 6:54





Relevant: youtu.be/-5wpm-gesOY

– deceze
Jan 4 at 6:54












4 Answers
4






active

oldest

votes


















1














You should just be "looping back" to 23:59 when you go negative. You can just add something to check if it goes negative then just re-add the missing time:



function CalcTime(UTCTIME, TIMEZONEOFFSETTIME) {
if (oparator == "+") {
var FT = UTCTIME + TIMEZONEOFFSETTIME;
FT = FT.toFixed(2);
floatToTime(FT);
} else {
var FT = UTCTIME - TIMEZONEOFFSETTIME;

// Offset any negative times;
if (FT < 0) {
FT += 24;
}

FT = FT.toFixed(2);
floatToTime(FT);
}
}


But ideally you really don't want to be handling these kinds of Timezone issues as other libraries are already handling it, i.e. moment.js






function runClock() {

setInterval(function() {
//debugger
var time = new Date();
// take timezone from HTML element
// ex: +:5:30
var getTimezone = "-:7:0" //$("#timeZone").text();

// split into array to get oparator (Positive and Negative Timezone)
var oparator = getTimezone.split(":")[0];
var timezone = getTimezone.split(":")[1] + ":" + getTimezone.split(":")[2];

// get UTC hours
var hours = 5 //time.getUTCHours();
var minutes = 0 //time.getUTCMinutes();
var UTCTIME = timeStringToFloat(hours + ":" + minutes);
var TIMEZONEOFFSETTIME = timeStringToFloat(timezone);
var finalTime = "";


// Convert time folloed by Colon into decimals
// ex: 1:45 = 1.75
function timeStringToFloat(time) {
var hoursMinutes = time.split(/[.:]/);
var hh = parseInt(hoursMinutes[0], 10);
var mm = hoursMinutes[1] ? parseInt(hoursMinutes[1], 10) : 0;
return hh + mm / 60;
}


// Convert time folloed by float into Colon
// ex: 1.75 = 1:45
function floatToTime(FT) {
var splittedTime = FT.toString().split(".");
var hh = splittedTime[0];
var mm = "";

if (splittedTime[1]) {
mm = Math.round((splittedTime[1] / 100) * 60);
} else {
mm = "0";
}

finalTime = hh + ":" + ((mm < 10) ? ("0" + mm) : mm);
}


// Calculate time (UTC + or - Timezone)
// Ex: 00:15 (UTC) + 5:30 = 5:45
function CalcTime(UTCTIME, TIMEZONEOFFSETTIME) {
if (oparator == "+") {
var FT = UTCTIME + TIMEZONEOFFSETTIME;
FT = FT.toFixed(2);
floatToTime(FT);
} else {
var FT = UTCTIME - TIMEZONEOFFSETTIME;

// Offset any negative times;
if (FT < 0) {
FT += 24;
}

FT = FT.toFixed(2);
floatToTime(FT);
}
}


// Parse Seconds
function seconds() {
var j = "";
if (time.getUTCSeconds() < 10) {
j = "0" + time.getUTCSeconds();
} else {
j = time.getUTCSeconds()
}
return j;
}

CalcTime(UTCTIME, TIMEZONEOFFSETTIME);

$("#clockTime").text(finalTime + ":" + seconds());
}, 1000);

}
runClock();

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<b id="clockTime"></b>








share|improve this answer
























  • Yeah, I thinking the same, but wasn't sure about this calculation.

    – Abhishek Pandey
    Jan 4 at 7:03



















1














You want to recover/display the time for a given timezone offset? Unless you like doing this for a hobby, stay away from string methods and use the date functions, no?






var offsetMS = -5.5 * 3600000
var myDate = new Date()
var dateWithOffset = new Date( myDate.getTime() + offsetMS )
var formatted = dateWithOffset.toLocaleString("en-GB",{timeZone:"UTC",hour:"numeric",minute:"numeric"})
console.log(formatted)





Even manipulating timezone offset directly is to be avoided. If you can, use toLocaleString with a real timezone name, then issues like daylight saving will be handled for you. Chrome seems to support a number of named timezones, but I can't easily find the definitive list, and it will likely vary from browser to browser.






share|improve this answer


























  • The reason of using string is that I'm getting time zone in time format ex. +5:50 (colon separated), in your case it would be -5:30, and if I use your method, I still need to convert time to binary first.

    – Abhishek Pandey
    Jan 4 at 9:21













  • anyway, it's a good idea.

    – Abhishek Pandey
    Jan 4 at 9:23



















0














Add 24 to your FT value and take division remain of 24:



function CalcTime(UTCTIME, TIMEZONEOFFSETTIME) {
if (oparator == "+") {
var FT = UTCTIME + TIMEZONEOFFSETTIME;
} else {
var FT = UTCTIME - TIMEZONEOFFSETTIME + 24;
FT = FT % 24;
}

FT = FT.toFixed(2);
floatToTime(FT);
}


Working sample: https://codepen.io/anon/pen/VqQqoo






share|improve this answer































    0














    If you do not want to use a library like moment.js that comes with time zone handling and simply need to add/subtract hours from a given date, how about converting them to a common time unit and operating on that?



    const timezoneString = '-:12:30';
    const parts = timezoneString.split(':');
    const timezone = (parts[0] === '-' ? -1 : 1) * (parseInt(parts[1]) + (parseInt(parts[2]) / 60.0));
    const timeZoneMillis = timezone * 3600 * 1000;
    const now = new Date();
    const inZone = new Date(now.getTime() + timeZoneMillis);
    console.log(now, inZone);


    Something like this would convert them to a millisecond timestamp and subtract that many hours and convert it back to a date.






    share|improve this answer


























    • I doubt this will work with colons.

      – Abhishek Pandey
      Jan 4 at 7:06











    • With your special format of 'operator:hours:minutes' you just convert it to hours. See updated code.

      – Yanick Salzmann
      Jan 4 at 7:31











    • Alternatively you can also convert it to minutes to avoid precision issues in the / 60.0 with (parts[0] === '-' ? -1 : 1) * (parseInt(parts[1]) * 60 + parseInt(parts[2])) and change line 4 to timezone * 60 * 1000.

      – Yanick Salzmann
      Jan 4 at 7:33












    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54034080%2futc-live-clock-not-working-with-negative-timezone%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You should just be "looping back" to 23:59 when you go negative. You can just add something to check if it goes negative then just re-add the missing time:



    function CalcTime(UTCTIME, TIMEZONEOFFSETTIME) {
    if (oparator == "+") {
    var FT = UTCTIME + TIMEZONEOFFSETTIME;
    FT = FT.toFixed(2);
    floatToTime(FT);
    } else {
    var FT = UTCTIME - TIMEZONEOFFSETTIME;

    // Offset any negative times;
    if (FT < 0) {
    FT += 24;
    }

    FT = FT.toFixed(2);
    floatToTime(FT);
    }
    }


    But ideally you really don't want to be handling these kinds of Timezone issues as other libraries are already handling it, i.e. moment.js






    function runClock() {

    setInterval(function() {
    //debugger
    var time = new Date();
    // take timezone from HTML element
    // ex: +:5:30
    var getTimezone = "-:7:0" //$("#timeZone").text();

    // split into array to get oparator (Positive and Negative Timezone)
    var oparator = getTimezone.split(":")[0];
    var timezone = getTimezone.split(":")[1] + ":" + getTimezone.split(":")[2];

    // get UTC hours
    var hours = 5 //time.getUTCHours();
    var minutes = 0 //time.getUTCMinutes();
    var UTCTIME = timeStringToFloat(hours + ":" + minutes);
    var TIMEZONEOFFSETTIME = timeStringToFloat(timezone);
    var finalTime = "";


    // Convert time folloed by Colon into decimals
    // ex: 1:45 = 1.75
    function timeStringToFloat(time) {
    var hoursMinutes = time.split(/[.:]/);
    var hh = parseInt(hoursMinutes[0], 10);
    var mm = hoursMinutes[1] ? parseInt(hoursMinutes[1], 10) : 0;
    return hh + mm / 60;
    }


    // Convert time folloed by float into Colon
    // ex: 1.75 = 1:45
    function floatToTime(FT) {
    var splittedTime = FT.toString().split(".");
    var hh = splittedTime[0];
    var mm = "";

    if (splittedTime[1]) {
    mm = Math.round((splittedTime[1] / 100) * 60);
    } else {
    mm = "0";
    }

    finalTime = hh + ":" + ((mm < 10) ? ("0" + mm) : mm);
    }


    // Calculate time (UTC + or - Timezone)
    // Ex: 00:15 (UTC) + 5:30 = 5:45
    function CalcTime(UTCTIME, TIMEZONEOFFSETTIME) {
    if (oparator == "+") {
    var FT = UTCTIME + TIMEZONEOFFSETTIME;
    FT = FT.toFixed(2);
    floatToTime(FT);
    } else {
    var FT = UTCTIME - TIMEZONEOFFSETTIME;

    // Offset any negative times;
    if (FT < 0) {
    FT += 24;
    }

    FT = FT.toFixed(2);
    floatToTime(FT);
    }
    }


    // Parse Seconds
    function seconds() {
    var j = "";
    if (time.getUTCSeconds() < 10) {
    j = "0" + time.getUTCSeconds();
    } else {
    j = time.getUTCSeconds()
    }
    return j;
    }

    CalcTime(UTCTIME, TIMEZONEOFFSETTIME);

    $("#clockTime").text(finalTime + ":" + seconds());
    }, 1000);

    }
    runClock();

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


    <b id="clockTime"></b>








    share|improve this answer
























    • Yeah, I thinking the same, but wasn't sure about this calculation.

      – Abhishek Pandey
      Jan 4 at 7:03
















    1














    You should just be "looping back" to 23:59 when you go negative. You can just add something to check if it goes negative then just re-add the missing time:



    function CalcTime(UTCTIME, TIMEZONEOFFSETTIME) {
    if (oparator == "+") {
    var FT = UTCTIME + TIMEZONEOFFSETTIME;
    FT = FT.toFixed(2);
    floatToTime(FT);
    } else {
    var FT = UTCTIME - TIMEZONEOFFSETTIME;

    // Offset any negative times;
    if (FT < 0) {
    FT += 24;
    }

    FT = FT.toFixed(2);
    floatToTime(FT);
    }
    }


    But ideally you really don't want to be handling these kinds of Timezone issues as other libraries are already handling it, i.e. moment.js






    function runClock() {

    setInterval(function() {
    //debugger
    var time = new Date();
    // take timezone from HTML element
    // ex: +:5:30
    var getTimezone = "-:7:0" //$("#timeZone").text();

    // split into array to get oparator (Positive and Negative Timezone)
    var oparator = getTimezone.split(":")[0];
    var timezone = getTimezone.split(":")[1] + ":" + getTimezone.split(":")[2];

    // get UTC hours
    var hours = 5 //time.getUTCHours();
    var minutes = 0 //time.getUTCMinutes();
    var UTCTIME = timeStringToFloat(hours + ":" + minutes);
    var TIMEZONEOFFSETTIME = timeStringToFloat(timezone);
    var finalTime = "";


    // Convert time folloed by Colon into decimals
    // ex: 1:45 = 1.75
    function timeStringToFloat(time) {
    var hoursMinutes = time.split(/[.:]/);
    var hh = parseInt(hoursMinutes[0], 10);
    var mm = hoursMinutes[1] ? parseInt(hoursMinutes[1], 10) : 0;
    return hh + mm / 60;
    }


    // Convert time folloed by float into Colon
    // ex: 1.75 = 1:45
    function floatToTime(FT) {
    var splittedTime = FT.toString().split(".");
    var hh = splittedTime[0];
    var mm = "";

    if (splittedTime[1]) {
    mm = Math.round((splittedTime[1] / 100) * 60);
    } else {
    mm = "0";
    }

    finalTime = hh + ":" + ((mm < 10) ? ("0" + mm) : mm);
    }


    // Calculate time (UTC + or - Timezone)
    // Ex: 00:15 (UTC) + 5:30 = 5:45
    function CalcTime(UTCTIME, TIMEZONEOFFSETTIME) {
    if (oparator == "+") {
    var FT = UTCTIME + TIMEZONEOFFSETTIME;
    FT = FT.toFixed(2);
    floatToTime(FT);
    } else {
    var FT = UTCTIME - TIMEZONEOFFSETTIME;

    // Offset any negative times;
    if (FT < 0) {
    FT += 24;
    }

    FT = FT.toFixed(2);
    floatToTime(FT);
    }
    }


    // Parse Seconds
    function seconds() {
    var j = "";
    if (time.getUTCSeconds() < 10) {
    j = "0" + time.getUTCSeconds();
    } else {
    j = time.getUTCSeconds()
    }
    return j;
    }

    CalcTime(UTCTIME, TIMEZONEOFFSETTIME);

    $("#clockTime").text(finalTime + ":" + seconds());
    }, 1000);

    }
    runClock();

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


    <b id="clockTime"></b>








    share|improve this answer
























    • Yeah, I thinking the same, but wasn't sure about this calculation.

      – Abhishek Pandey
      Jan 4 at 7:03














    1












    1








    1







    You should just be "looping back" to 23:59 when you go negative. You can just add something to check if it goes negative then just re-add the missing time:



    function CalcTime(UTCTIME, TIMEZONEOFFSETTIME) {
    if (oparator == "+") {
    var FT = UTCTIME + TIMEZONEOFFSETTIME;
    FT = FT.toFixed(2);
    floatToTime(FT);
    } else {
    var FT = UTCTIME - TIMEZONEOFFSETTIME;

    // Offset any negative times;
    if (FT < 0) {
    FT += 24;
    }

    FT = FT.toFixed(2);
    floatToTime(FT);
    }
    }


    But ideally you really don't want to be handling these kinds of Timezone issues as other libraries are already handling it, i.e. moment.js






    function runClock() {

    setInterval(function() {
    //debugger
    var time = new Date();
    // take timezone from HTML element
    // ex: +:5:30
    var getTimezone = "-:7:0" //$("#timeZone").text();

    // split into array to get oparator (Positive and Negative Timezone)
    var oparator = getTimezone.split(":")[0];
    var timezone = getTimezone.split(":")[1] + ":" + getTimezone.split(":")[2];

    // get UTC hours
    var hours = 5 //time.getUTCHours();
    var minutes = 0 //time.getUTCMinutes();
    var UTCTIME = timeStringToFloat(hours + ":" + minutes);
    var TIMEZONEOFFSETTIME = timeStringToFloat(timezone);
    var finalTime = "";


    // Convert time folloed by Colon into decimals
    // ex: 1:45 = 1.75
    function timeStringToFloat(time) {
    var hoursMinutes = time.split(/[.:]/);
    var hh = parseInt(hoursMinutes[0], 10);
    var mm = hoursMinutes[1] ? parseInt(hoursMinutes[1], 10) : 0;
    return hh + mm / 60;
    }


    // Convert time folloed by float into Colon
    // ex: 1.75 = 1:45
    function floatToTime(FT) {
    var splittedTime = FT.toString().split(".");
    var hh = splittedTime[0];
    var mm = "";

    if (splittedTime[1]) {
    mm = Math.round((splittedTime[1] / 100) * 60);
    } else {
    mm = "0";
    }

    finalTime = hh + ":" + ((mm < 10) ? ("0" + mm) : mm);
    }


    // Calculate time (UTC + or - Timezone)
    // Ex: 00:15 (UTC) + 5:30 = 5:45
    function CalcTime(UTCTIME, TIMEZONEOFFSETTIME) {
    if (oparator == "+") {
    var FT = UTCTIME + TIMEZONEOFFSETTIME;
    FT = FT.toFixed(2);
    floatToTime(FT);
    } else {
    var FT = UTCTIME - TIMEZONEOFFSETTIME;

    // Offset any negative times;
    if (FT < 0) {
    FT += 24;
    }

    FT = FT.toFixed(2);
    floatToTime(FT);
    }
    }


    // Parse Seconds
    function seconds() {
    var j = "";
    if (time.getUTCSeconds() < 10) {
    j = "0" + time.getUTCSeconds();
    } else {
    j = time.getUTCSeconds()
    }
    return j;
    }

    CalcTime(UTCTIME, TIMEZONEOFFSETTIME);

    $("#clockTime").text(finalTime + ":" + seconds());
    }, 1000);

    }
    runClock();

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


    <b id="clockTime"></b>








    share|improve this answer













    You should just be "looping back" to 23:59 when you go negative. You can just add something to check if it goes negative then just re-add the missing time:



    function CalcTime(UTCTIME, TIMEZONEOFFSETTIME) {
    if (oparator == "+") {
    var FT = UTCTIME + TIMEZONEOFFSETTIME;
    FT = FT.toFixed(2);
    floatToTime(FT);
    } else {
    var FT = UTCTIME - TIMEZONEOFFSETTIME;

    // Offset any negative times;
    if (FT < 0) {
    FT += 24;
    }

    FT = FT.toFixed(2);
    floatToTime(FT);
    }
    }


    But ideally you really don't want to be handling these kinds of Timezone issues as other libraries are already handling it, i.e. moment.js






    function runClock() {

    setInterval(function() {
    //debugger
    var time = new Date();
    // take timezone from HTML element
    // ex: +:5:30
    var getTimezone = "-:7:0" //$("#timeZone").text();

    // split into array to get oparator (Positive and Negative Timezone)
    var oparator = getTimezone.split(":")[0];
    var timezone = getTimezone.split(":")[1] + ":" + getTimezone.split(":")[2];

    // get UTC hours
    var hours = 5 //time.getUTCHours();
    var minutes = 0 //time.getUTCMinutes();
    var UTCTIME = timeStringToFloat(hours + ":" + minutes);
    var TIMEZONEOFFSETTIME = timeStringToFloat(timezone);
    var finalTime = "";


    // Convert time folloed by Colon into decimals
    // ex: 1:45 = 1.75
    function timeStringToFloat(time) {
    var hoursMinutes = time.split(/[.:]/);
    var hh = parseInt(hoursMinutes[0], 10);
    var mm = hoursMinutes[1] ? parseInt(hoursMinutes[1], 10) : 0;
    return hh + mm / 60;
    }


    // Convert time folloed by float into Colon
    // ex: 1.75 = 1:45
    function floatToTime(FT) {
    var splittedTime = FT.toString().split(".");
    var hh = splittedTime[0];
    var mm = "";

    if (splittedTime[1]) {
    mm = Math.round((splittedTime[1] / 100) * 60);
    } else {
    mm = "0";
    }

    finalTime = hh + ":" + ((mm < 10) ? ("0" + mm) : mm);
    }


    // Calculate time (UTC + or - Timezone)
    // Ex: 00:15 (UTC) + 5:30 = 5:45
    function CalcTime(UTCTIME, TIMEZONEOFFSETTIME) {
    if (oparator == "+") {
    var FT = UTCTIME + TIMEZONEOFFSETTIME;
    FT = FT.toFixed(2);
    floatToTime(FT);
    } else {
    var FT = UTCTIME - TIMEZONEOFFSETTIME;

    // Offset any negative times;
    if (FT < 0) {
    FT += 24;
    }

    FT = FT.toFixed(2);
    floatToTime(FT);
    }
    }


    // Parse Seconds
    function seconds() {
    var j = "";
    if (time.getUTCSeconds() < 10) {
    j = "0" + time.getUTCSeconds();
    } else {
    j = time.getUTCSeconds()
    }
    return j;
    }

    CalcTime(UTCTIME, TIMEZONEOFFSETTIME);

    $("#clockTime").text(finalTime + ":" + seconds());
    }, 1000);

    }
    runClock();

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


    <b id="clockTime"></b>








    function runClock() {

    setInterval(function() {
    //debugger
    var time = new Date();
    // take timezone from HTML element
    // ex: +:5:30
    var getTimezone = "-:7:0" //$("#timeZone").text();

    // split into array to get oparator (Positive and Negative Timezone)
    var oparator = getTimezone.split(":")[0];
    var timezone = getTimezone.split(":")[1] + ":" + getTimezone.split(":")[2];

    // get UTC hours
    var hours = 5 //time.getUTCHours();
    var minutes = 0 //time.getUTCMinutes();
    var UTCTIME = timeStringToFloat(hours + ":" + minutes);
    var TIMEZONEOFFSETTIME = timeStringToFloat(timezone);
    var finalTime = "";


    // Convert time folloed by Colon into decimals
    // ex: 1:45 = 1.75
    function timeStringToFloat(time) {
    var hoursMinutes = time.split(/[.:]/);
    var hh = parseInt(hoursMinutes[0], 10);
    var mm = hoursMinutes[1] ? parseInt(hoursMinutes[1], 10) : 0;
    return hh + mm / 60;
    }


    // Convert time folloed by float into Colon
    // ex: 1.75 = 1:45
    function floatToTime(FT) {
    var splittedTime = FT.toString().split(".");
    var hh = splittedTime[0];
    var mm = "";

    if (splittedTime[1]) {
    mm = Math.round((splittedTime[1] / 100) * 60);
    } else {
    mm = "0";
    }

    finalTime = hh + ":" + ((mm < 10) ? ("0" + mm) : mm);
    }


    // Calculate time (UTC + or - Timezone)
    // Ex: 00:15 (UTC) + 5:30 = 5:45
    function CalcTime(UTCTIME, TIMEZONEOFFSETTIME) {
    if (oparator == "+") {
    var FT = UTCTIME + TIMEZONEOFFSETTIME;
    FT = FT.toFixed(2);
    floatToTime(FT);
    } else {
    var FT = UTCTIME - TIMEZONEOFFSETTIME;

    // Offset any negative times;
    if (FT < 0) {
    FT += 24;
    }

    FT = FT.toFixed(2);
    floatToTime(FT);
    }
    }


    // Parse Seconds
    function seconds() {
    var j = "";
    if (time.getUTCSeconds() < 10) {
    j = "0" + time.getUTCSeconds();
    } else {
    j = time.getUTCSeconds()
    }
    return j;
    }

    CalcTime(UTCTIME, TIMEZONEOFFSETTIME);

    $("#clockTime").text(finalTime + ":" + seconds());
    }, 1000);

    }
    runClock();

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


    <b id="clockTime"></b>





    function runClock() {

    setInterval(function() {
    //debugger
    var time = new Date();
    // take timezone from HTML element
    // ex: +:5:30
    var getTimezone = "-:7:0" //$("#timeZone").text();

    // split into array to get oparator (Positive and Negative Timezone)
    var oparator = getTimezone.split(":")[0];
    var timezone = getTimezone.split(":")[1] + ":" + getTimezone.split(":")[2];

    // get UTC hours
    var hours = 5 //time.getUTCHours();
    var minutes = 0 //time.getUTCMinutes();
    var UTCTIME = timeStringToFloat(hours + ":" + minutes);
    var TIMEZONEOFFSETTIME = timeStringToFloat(timezone);
    var finalTime = "";


    // Convert time folloed by Colon into decimals
    // ex: 1:45 = 1.75
    function timeStringToFloat(time) {
    var hoursMinutes = time.split(/[.:]/);
    var hh = parseInt(hoursMinutes[0], 10);
    var mm = hoursMinutes[1] ? parseInt(hoursMinutes[1], 10) : 0;
    return hh + mm / 60;
    }


    // Convert time folloed by float into Colon
    // ex: 1.75 = 1:45
    function floatToTime(FT) {
    var splittedTime = FT.toString().split(".");
    var hh = splittedTime[0];
    var mm = "";

    if (splittedTime[1]) {
    mm = Math.round((splittedTime[1] / 100) * 60);
    } else {
    mm = "0";
    }

    finalTime = hh + ":" + ((mm < 10) ? ("0" + mm) : mm);
    }


    // Calculate time (UTC + or - Timezone)
    // Ex: 00:15 (UTC) + 5:30 = 5:45
    function CalcTime(UTCTIME, TIMEZONEOFFSETTIME) {
    if (oparator == "+") {
    var FT = UTCTIME + TIMEZONEOFFSETTIME;
    FT = FT.toFixed(2);
    floatToTime(FT);
    } else {
    var FT = UTCTIME - TIMEZONEOFFSETTIME;

    // Offset any negative times;
    if (FT < 0) {
    FT += 24;
    }

    FT = FT.toFixed(2);
    floatToTime(FT);
    }
    }


    // Parse Seconds
    function seconds() {
    var j = "";
    if (time.getUTCSeconds() < 10) {
    j = "0" + time.getUTCSeconds();
    } else {
    j = time.getUTCSeconds()
    }
    return j;
    }

    CalcTime(UTCTIME, TIMEZONEOFFSETTIME);

    $("#clockTime").text(finalTime + ":" + seconds());
    }, 1000);

    }
    runClock();

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


    <b id="clockTime"></b>






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 4 at 6:55









    fixtdfixtd

    768612




    768612













    • Yeah, I thinking the same, but wasn't sure about this calculation.

      – Abhishek Pandey
      Jan 4 at 7:03



















    • Yeah, I thinking the same, but wasn't sure about this calculation.

      – Abhishek Pandey
      Jan 4 at 7:03

















    Yeah, I thinking the same, but wasn't sure about this calculation.

    – Abhishek Pandey
    Jan 4 at 7:03





    Yeah, I thinking the same, but wasn't sure about this calculation.

    – Abhishek Pandey
    Jan 4 at 7:03













    1














    You want to recover/display the time for a given timezone offset? Unless you like doing this for a hobby, stay away from string methods and use the date functions, no?






    var offsetMS = -5.5 * 3600000
    var myDate = new Date()
    var dateWithOffset = new Date( myDate.getTime() + offsetMS )
    var formatted = dateWithOffset.toLocaleString("en-GB",{timeZone:"UTC",hour:"numeric",minute:"numeric"})
    console.log(formatted)





    Even manipulating timezone offset directly is to be avoided. If you can, use toLocaleString with a real timezone name, then issues like daylight saving will be handled for you. Chrome seems to support a number of named timezones, but I can't easily find the definitive list, and it will likely vary from browser to browser.






    share|improve this answer


























    • The reason of using string is that I'm getting time zone in time format ex. +5:50 (colon separated), in your case it would be -5:30, and if I use your method, I still need to convert time to binary first.

      – Abhishek Pandey
      Jan 4 at 9:21













    • anyway, it's a good idea.

      – Abhishek Pandey
      Jan 4 at 9:23
















    1














    You want to recover/display the time for a given timezone offset? Unless you like doing this for a hobby, stay away from string methods and use the date functions, no?






    var offsetMS = -5.5 * 3600000
    var myDate = new Date()
    var dateWithOffset = new Date( myDate.getTime() + offsetMS )
    var formatted = dateWithOffset.toLocaleString("en-GB",{timeZone:"UTC",hour:"numeric",minute:"numeric"})
    console.log(formatted)





    Even manipulating timezone offset directly is to be avoided. If you can, use toLocaleString with a real timezone name, then issues like daylight saving will be handled for you. Chrome seems to support a number of named timezones, but I can't easily find the definitive list, and it will likely vary from browser to browser.






    share|improve this answer


























    • The reason of using string is that I'm getting time zone in time format ex. +5:50 (colon separated), in your case it would be -5:30, and if I use your method, I still need to convert time to binary first.

      – Abhishek Pandey
      Jan 4 at 9:21













    • anyway, it's a good idea.

      – Abhishek Pandey
      Jan 4 at 9:23














    1












    1








    1







    You want to recover/display the time for a given timezone offset? Unless you like doing this for a hobby, stay away from string methods and use the date functions, no?






    var offsetMS = -5.5 * 3600000
    var myDate = new Date()
    var dateWithOffset = new Date( myDate.getTime() + offsetMS )
    var formatted = dateWithOffset.toLocaleString("en-GB",{timeZone:"UTC",hour:"numeric",minute:"numeric"})
    console.log(formatted)





    Even manipulating timezone offset directly is to be avoided. If you can, use toLocaleString with a real timezone name, then issues like daylight saving will be handled for you. Chrome seems to support a number of named timezones, but I can't easily find the definitive list, and it will likely vary from browser to browser.






    share|improve this answer















    You want to recover/display the time for a given timezone offset? Unless you like doing this for a hobby, stay away from string methods and use the date functions, no?






    var offsetMS = -5.5 * 3600000
    var myDate = new Date()
    var dateWithOffset = new Date( myDate.getTime() + offsetMS )
    var formatted = dateWithOffset.toLocaleString("en-GB",{timeZone:"UTC",hour:"numeric",minute:"numeric"})
    console.log(formatted)





    Even manipulating timezone offset directly is to be avoided. If you can, use toLocaleString with a real timezone name, then issues like daylight saving will be handled for you. Chrome seems to support a number of named timezones, but I can't easily find the definitive list, and it will likely vary from browser to browser.






    var offsetMS = -5.5 * 3600000
    var myDate = new Date()
    var dateWithOffset = new Date( myDate.getTime() + offsetMS )
    var formatted = dateWithOffset.toLocaleString("en-GB",{timeZone:"UTC",hour:"numeric",minute:"numeric"})
    console.log(formatted)





    var offsetMS = -5.5 * 3600000
    var myDate = new Date()
    var dateWithOffset = new Date( myDate.getTime() + offsetMS )
    var formatted = dateWithOffset.toLocaleString("en-GB",{timeZone:"UTC",hour:"numeric",minute:"numeric"})
    console.log(formatted)






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 4 at 8:57

























    answered Jan 4 at 8:50









    bbsimonbbbbsimonbb

    10.7k63548




    10.7k63548













    • The reason of using string is that I'm getting time zone in time format ex. +5:50 (colon separated), in your case it would be -5:30, and if I use your method, I still need to convert time to binary first.

      – Abhishek Pandey
      Jan 4 at 9:21













    • anyway, it's a good idea.

      – Abhishek Pandey
      Jan 4 at 9:23



















    • The reason of using string is that I'm getting time zone in time format ex. +5:50 (colon separated), in your case it would be -5:30, and if I use your method, I still need to convert time to binary first.

      – Abhishek Pandey
      Jan 4 at 9:21













    • anyway, it's a good idea.

      – Abhishek Pandey
      Jan 4 at 9:23

















    The reason of using string is that I'm getting time zone in time format ex. +5:50 (colon separated), in your case it would be -5:30, and if I use your method, I still need to convert time to binary first.

    – Abhishek Pandey
    Jan 4 at 9:21







    The reason of using string is that I'm getting time zone in time format ex. +5:50 (colon separated), in your case it would be -5:30, and if I use your method, I still need to convert time to binary first.

    – Abhishek Pandey
    Jan 4 at 9:21















    anyway, it's a good idea.

    – Abhishek Pandey
    Jan 4 at 9:23





    anyway, it's a good idea.

    – Abhishek Pandey
    Jan 4 at 9:23











    0














    Add 24 to your FT value and take division remain of 24:



    function CalcTime(UTCTIME, TIMEZONEOFFSETTIME) {
    if (oparator == "+") {
    var FT = UTCTIME + TIMEZONEOFFSETTIME;
    } else {
    var FT = UTCTIME - TIMEZONEOFFSETTIME + 24;
    FT = FT % 24;
    }

    FT = FT.toFixed(2);
    floatToTime(FT);
    }


    Working sample: https://codepen.io/anon/pen/VqQqoo






    share|improve this answer




























      0














      Add 24 to your FT value and take division remain of 24:



      function CalcTime(UTCTIME, TIMEZONEOFFSETTIME) {
      if (oparator == "+") {
      var FT = UTCTIME + TIMEZONEOFFSETTIME;
      } else {
      var FT = UTCTIME - TIMEZONEOFFSETTIME + 24;
      FT = FT % 24;
      }

      FT = FT.toFixed(2);
      floatToTime(FT);
      }


      Working sample: https://codepen.io/anon/pen/VqQqoo






      share|improve this answer


























        0












        0








        0







        Add 24 to your FT value and take division remain of 24:



        function CalcTime(UTCTIME, TIMEZONEOFFSETTIME) {
        if (oparator == "+") {
        var FT = UTCTIME + TIMEZONEOFFSETTIME;
        } else {
        var FT = UTCTIME - TIMEZONEOFFSETTIME + 24;
        FT = FT % 24;
        }

        FT = FT.toFixed(2);
        floatToTime(FT);
        }


        Working sample: https://codepen.io/anon/pen/VqQqoo






        share|improve this answer













        Add 24 to your FT value and take division remain of 24:



        function CalcTime(UTCTIME, TIMEZONEOFFSETTIME) {
        if (oparator == "+") {
        var FT = UTCTIME + TIMEZONEOFFSETTIME;
        } else {
        var FT = UTCTIME - TIMEZONEOFFSETTIME + 24;
        FT = FT % 24;
        }

        FT = FT.toFixed(2);
        floatToTime(FT);
        }


        Working sample: https://codepen.io/anon/pen/VqQqoo







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 4 at 7:18









        blazblaz

        1,834919




        1,834919























            0














            If you do not want to use a library like moment.js that comes with time zone handling and simply need to add/subtract hours from a given date, how about converting them to a common time unit and operating on that?



            const timezoneString = '-:12:30';
            const parts = timezoneString.split(':');
            const timezone = (parts[0] === '-' ? -1 : 1) * (parseInt(parts[1]) + (parseInt(parts[2]) / 60.0));
            const timeZoneMillis = timezone * 3600 * 1000;
            const now = new Date();
            const inZone = new Date(now.getTime() + timeZoneMillis);
            console.log(now, inZone);


            Something like this would convert them to a millisecond timestamp and subtract that many hours and convert it back to a date.






            share|improve this answer


























            • I doubt this will work with colons.

              – Abhishek Pandey
              Jan 4 at 7:06











            • With your special format of 'operator:hours:minutes' you just convert it to hours. See updated code.

              – Yanick Salzmann
              Jan 4 at 7:31











            • Alternatively you can also convert it to minutes to avoid precision issues in the / 60.0 with (parts[0] === '-' ? -1 : 1) * (parseInt(parts[1]) * 60 + parseInt(parts[2])) and change line 4 to timezone * 60 * 1000.

              – Yanick Salzmann
              Jan 4 at 7:33
















            0














            If you do not want to use a library like moment.js that comes with time zone handling and simply need to add/subtract hours from a given date, how about converting them to a common time unit and operating on that?



            const timezoneString = '-:12:30';
            const parts = timezoneString.split(':');
            const timezone = (parts[0] === '-' ? -1 : 1) * (parseInt(parts[1]) + (parseInt(parts[2]) / 60.0));
            const timeZoneMillis = timezone * 3600 * 1000;
            const now = new Date();
            const inZone = new Date(now.getTime() + timeZoneMillis);
            console.log(now, inZone);


            Something like this would convert them to a millisecond timestamp and subtract that many hours and convert it back to a date.






            share|improve this answer


























            • I doubt this will work with colons.

              – Abhishek Pandey
              Jan 4 at 7:06











            • With your special format of 'operator:hours:minutes' you just convert it to hours. See updated code.

              – Yanick Salzmann
              Jan 4 at 7:31











            • Alternatively you can also convert it to minutes to avoid precision issues in the / 60.0 with (parts[0] === '-' ? -1 : 1) * (parseInt(parts[1]) * 60 + parseInt(parts[2])) and change line 4 to timezone * 60 * 1000.

              – Yanick Salzmann
              Jan 4 at 7:33














            0












            0








            0







            If you do not want to use a library like moment.js that comes with time zone handling and simply need to add/subtract hours from a given date, how about converting them to a common time unit and operating on that?



            const timezoneString = '-:12:30';
            const parts = timezoneString.split(':');
            const timezone = (parts[0] === '-' ? -1 : 1) * (parseInt(parts[1]) + (parseInt(parts[2]) / 60.0));
            const timeZoneMillis = timezone * 3600 * 1000;
            const now = new Date();
            const inZone = new Date(now.getTime() + timeZoneMillis);
            console.log(now, inZone);


            Something like this would convert them to a millisecond timestamp and subtract that many hours and convert it back to a date.






            share|improve this answer















            If you do not want to use a library like moment.js that comes with time zone handling and simply need to add/subtract hours from a given date, how about converting them to a common time unit and operating on that?



            const timezoneString = '-:12:30';
            const parts = timezoneString.split(':');
            const timezone = (parts[0] === '-' ? -1 : 1) * (parseInt(parts[1]) + (parseInt(parts[2]) / 60.0));
            const timeZoneMillis = timezone * 3600 * 1000;
            const now = new Date();
            const inZone = new Date(now.getTime() + timeZoneMillis);
            console.log(now, inZone);


            Something like this would convert them to a millisecond timestamp and subtract that many hours and convert it back to a date.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 4 at 7:29

























            answered Jan 4 at 6:49









            Yanick SalzmannYanick Salzmann

            355216




            355216













            • I doubt this will work with colons.

              – Abhishek Pandey
              Jan 4 at 7:06











            • With your special format of 'operator:hours:minutes' you just convert it to hours. See updated code.

              – Yanick Salzmann
              Jan 4 at 7:31











            • Alternatively you can also convert it to minutes to avoid precision issues in the / 60.0 with (parts[0] === '-' ? -1 : 1) * (parseInt(parts[1]) * 60 + parseInt(parts[2])) and change line 4 to timezone * 60 * 1000.

              – Yanick Salzmann
              Jan 4 at 7:33



















            • I doubt this will work with colons.

              – Abhishek Pandey
              Jan 4 at 7:06











            • With your special format of 'operator:hours:minutes' you just convert it to hours. See updated code.

              – Yanick Salzmann
              Jan 4 at 7:31











            • Alternatively you can also convert it to minutes to avoid precision issues in the / 60.0 with (parts[0] === '-' ? -1 : 1) * (parseInt(parts[1]) * 60 + parseInt(parts[2])) and change line 4 to timezone * 60 * 1000.

              – Yanick Salzmann
              Jan 4 at 7:33

















            I doubt this will work with colons.

            – Abhishek Pandey
            Jan 4 at 7:06





            I doubt this will work with colons.

            – Abhishek Pandey
            Jan 4 at 7:06













            With your special format of 'operator:hours:minutes' you just convert it to hours. See updated code.

            – Yanick Salzmann
            Jan 4 at 7:31





            With your special format of 'operator:hours:minutes' you just convert it to hours. See updated code.

            – Yanick Salzmann
            Jan 4 at 7:31













            Alternatively you can also convert it to minutes to avoid precision issues in the / 60.0 with (parts[0] === '-' ? -1 : 1) * (parseInt(parts[1]) * 60 + parseInt(parts[2])) and change line 4 to timezone * 60 * 1000.

            – Yanick Salzmann
            Jan 4 at 7:33





            Alternatively you can also convert it to minutes to avoid precision issues in the / 60.0 with (parts[0] === '-' ? -1 : 1) * (parseInt(parts[1]) * 60 + parseInt(parts[2])) and change line 4 to timezone * 60 * 1000.

            – Yanick Salzmann
            Jan 4 at 7:33


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54034080%2futc-live-clock-not-working-with-negative-timezone%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas