0

Javascript Date function returning different time zone in different operating system like returning "India standard time" windows operating system and "IST" in mac system. i want as "India standard time" in all operating systems.

this is my code:

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = Date();
</script>

</body>
</html>

result In windows : Tue Feb 14 2017 13:03:02 GMT+0530 (India Standard Time) In mac system : Tue Feb 14 2017 13:03:02 GMT+0530 (IST)

  • What do you want to achieve? The datetime strings are equal. If you use getTime or other function, it will return the exact same values. – DomeTune Feb 14 '17 at 09:17
  • Possible duplicate of [How to initialize javascript date to a particular timezone](http://stackoverflow.com/questions/15141762/how-to-initialize-javascript-date-to-a-particular-timezone) – caramba Feb 14 '17 at 09:17
  • @DomeTune: The date time string are equal but look at time zone, i want a constant timezone name. i want to achieve the client's time zone. – Meraj Khan Feb 15 '17 at 05:28
  • You can use `new Date().getTimezoneOffset() / 60;` to get the UTC-offset. (+12 | 0 | -12) Maybe you will need a mapping for each timezone in a function. [HERE is an example](https://jsfiddle.net/DomeTune/2ojqf5mn/)! – DomeTune Feb 15 '17 at 08:13
  • @DomeTune: Thank you this solved my problem :) – Meraj Khan Feb 15 '17 at 09:28
  • @MerajKhan I added it as an answer ;-) – DomeTune Feb 15 '17 at 09:30

3 Answers3

0
<script>
    var currentTime = new Date(),
    hours = currentTime.getHours(),
    minutes = currentTime.getMinutes();

    if (minutes < 10) {
        minutes = "0" + minutes;
    }

    document.write(hours + ":" + minutes)
</script>
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
  • How does it relate to the question? – pawel Feb 14 '17 at 09:29
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Donald Duck Feb 14 '17 at 12:43
  • I only want timezone, basically i want to determine the client's time zone. – Meraj Khan Feb 14 '17 at 13:03
  • @KausharAlam a better solution for the `if-term` is following: `("0"+ minutes).slice(-2)`. It allways gives you back the minutes with two digits. – DomeTune Feb 15 '17 at 07:45
0

I would suggest you to use moment js. if you're using amd module loader then you can do something like this:

require(["moment"],(m)=>{console.log(m().utcOffset(330).format())})

Apoorv Joshi
  • 389
  • 3
  • 15
0

Allready mentioned in the Question-Comments.

Object.defineProperty(Date.prototype, "getTimezoneString", {
    enumerable: false,
    writable: true,
    configurable: false,
    value: function(){
     var retval = "Z";
   var offset = this.getTimezoneOffset() / 60;
   switch (offset) {
       case 14: retval = "M†"; break;
       case 13.75: retval = ""; break;
       case 13: retval = "M†"; break;
       case 12.75: retval = "M"; break;
       case 12: retval = "M"; break;
       case 11: retval = "L"; break;
       case 10.5: retval = "K†"; break;
       case 10: retval = "K"; break;
       case 9.5: retval = "I†"; break;
       case 9: retval = "I"; break;
       case 8.75: retval = "H*"; break;
       case 8.5: retval = "H†"; break;
       case 8: retval = "H"; break;
       case 7: retval = "G"; break;
       case 6.5: retval = "F†"; break;
       case 6: retval = "F"; break;
       case 5.75: retval = "E*"; break;
       case 5.5: retval = "E†"; break;
       case 5: retval = "E"; break;
       case 4.5: retval = "D†"; break;
       case 4: retval = "D"; break;
       case 3.5: retval = "C†"; break;
       case 3: retval = "C"; break;
       case 2: retval = "B"; break;
       case 1: retval = "A"; break;
       case 0: retval = "Z"; break;
       case -1: retval = "N"; break;
       case -2: retval = "O"; break;
       case -3: retval = "P"; break;
       case -3.5: retval = "P†"; break;
       case -4: retval = "Q"; break;
       case -5: retval = "R"; break;
       case -6: retval = "S"; break;
       case -7: retval = "T"; break;
       case -8: retval = "U"; break;
       case -9: retval = "V"; break;
       case -9.5: retval = "V†"; break;
       case -10: retval = "W"; break;
       case -11: retval = "X"; break;
       case -12: retval = "Y"; break;
        default: retval = "Z"; break;
      }
   return retval;
    }
});
//https://en.wikipedia.org/wiki/List_of_UTC_time_offsets
//more detail: https://www.timeanddate.com/time/zones/
var timezone = new Date().getTimezoneString();
document.querySelector("#date").innerHTML = timezone;
<div id="date">

</div>
DomeTune
  • 1,401
  • 10
  • 21
  • I am sorry for this weird code-alignment... [Here is the Fiddle](https://jsfiddle.net/DomeTune/2ojqf5mn/)! – DomeTune Feb 15 '17 at 09:42