samedi 18 juin 2016

How to get the country time using jqeury/JS does not matter from where i'm logged in

How to get the India time using jqeury does not matter from where i'm logged in. Might be i logged in to different country but it should show me the exact time of other countries. Let's take an example i'm in India and i trying to get Newyork time however i'm using the same application from different time zone it should show me correct time of Newyork. Currently i'm trying to do this using jqeury but i didn't find the consistent time as it's getting the current time of browser. So in diff-diff countries it showing me diff-diff time. Please help me out on this.

It show me that it's a duplicate question(How to get Time of specific timezone using javascript?) but it's totally different. Because here we are fixed that if i'm logged in to that country only than it will give me right result

Take an full example: Like i have created an application and first time i logged in with India and second time i logged in with Australia so in both the countries i want to get the time of USA using same application.

using this code i'm getting correct time of san francisco from Newyork but if i using the application from India it show me incorrect time of san francisco.

<canvas id="canvasFrancisco" width="100" height="100"
style="background-color:#333"></canvas>
<script type="text/javascript">
var canvas1 = document.getElementById("canvasFrancisco");
var ctx1 = canvas1.getContext("2d");
var radius1 = canvas1.height / 2;
ctx1.translate(radius1, radius1);
radius1 = radius1 * 0.90
setInterval(drawClockFrancisco, 1000);

function drawClockFrancisco() {
drawFace(ctx1, radius1);
drawNumbers(ctx1, radius1);
drawTimeFrancisco(ctx1, radius1);
}

function drawTimeFrancisco(ctx, radius) {
var now = new Date();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
//hour
hour = hour - 3;
hour = hour % 12;
hour = (hour * Math.PI / 6) +
(minute * Math.PI / (6 * 60)) +
(second * Math.PI / (360 * 60));
drawHand(ctx, hour, radius * 0.5, radius * 0.07);
//minute
minute = (minute * Math.PI / 30) + (second * Math.PI / (30 * 60));
drawHand(ctx, minute, radius * 0.8, radius * 0.07);
// second
second = (second * Math.PI / 30);
drawHand(ctx, second, radius * 0.9, radius * 0.02);
}

//Common for all watch
function drawFace(ctx, radius) {
var grad;
ctx.beginPath();
ctx.arc(0, 0, radius, 0, 2*Math.PI);
ctx.fillStyle = 'white';
ctx.fill();
grad = ctx.createRadialGradient(0,0,radius*0.95, 0,0,radius*1.05);
grad.addColorStop(0, '#333');
grad.addColorStop(0.5, 'white');
grad.addColorStop(1, '#333');
ctx.strokeStyle = grad;
ctx.lineWidth = radius*0.1;
ctx.stroke();
ctx.beginPath();
ctx.arc(0, 0, radius*0.1, 0, 2*Math.PI);
ctx.fillStyle = '#333';
ctx.fill();
}

function drawNumbers(ctx, radius) {
var ang;
var num;
ctx.font = radius*0.15 + "px arial";
ctx.textBaseline="middle";
ctx.textAlign="center";
for(num = 1; num < 13; num++){
ang = num * Math.PI / 6;
ctx.rotate(ang);
ctx.translate(0, -radius*0.85);
ctx.rotate(-ang);
ctx.fillText(num.toString(), 0, 0);
ctx.rotate(ang);
ctx.translate(0, radius*0.85);
ctx.rotate(-ang);
}
}

function drawHand(ctx, pos, length, width) {
ctx.beginPath();
ctx.lineWidth = width;
ctx.lineCap = "round";
ctx.moveTo(0,0);
ctx.rotate(pos);
ctx.lineTo(0, -length);
ctx.stroke();
ctx.rotate(-pos);
}
//end
</script>

Aucun commentaire:

Enregistrer un commentaire