2

JavaScript Date() function returns IST(Indian Standard Time) Thu Apr 07 2016 17:24:07 GMT+0530 (IST).

From this I want to fetch the abbreviation. But this always returns the Indian standard time. How do I get the abbreviation for Srilanka time abbreviation?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Ahalya Hegde
  • 1,571
  • 2
  • 18
  • 41
  • http://stackoverflow.com/questions/8207655/how-to-get-time-of-specific-timezone-using-javascript don't wanna steal credit, but this seems to work just fine – user326964 Apr 07 '16 at 12:12
  • Method given in above link requires city name.In my case, I won't be knowing city name. I just want the time abbreviation of the particular timezone. I have changed my system location to Sri Lanka – Ahalya Hegde Apr 07 '16 at 12:20

6 Answers6

2

I think this is what you expect as the answer

const date = new Date().toLocaleString('en-US', { timeZone: 'Asia/Colombo'});
0

Sri Lanka Time - is abbreviated as IST (India Standard Time). So Sri Lanka TimeZone is same as that of India. Thats why you're getting IST, because it is IST.

Sri Lanka switched to SLST on 11th April 2011 midnight, but its same as IST, JavaScript probably doesn't recognize it.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Dushyant Bangal
  • 6,048
  • 8
  • 48
  • 80
  • Uh, no. SLST and IST have been the same since 2006. Nothing switched in 2011. See https://en.wikipedia.org/wiki/Sri_Lanka_Standard_Time and http://www.timeanddate.com/time/zone/sri-lanka/colombo?syear=2000&eyear=2020 – Matt Johnson-Pint Apr 07 '16 at 22:51
  • I read about it here: http://www.differencebetween.com/difference-between-slst-sri-lanka-standard-time-and-vs-ist-indian-standard-time/ Although, I did say its still the same. – Dushyant Bangal Apr 11 '16 at 05:58
0

I guess this is what you want:

<script>
function myFunction() {
    var theDate = new Date(); 
    var res = theDate.replace("IST", "SLST");
}
</script>

And now you can use res to display the exact same time(since IST and SLST are the same according to what I've read), but with SLST instead of IST at the end. If there's more to replace in the string that Date() generates, then you can replace that too.

user326964
  • 452
  • 1
  • 5
  • 13
  • 1
    Except not every implementation of `Date` will return `"IST"` in the string. That's implementation dependent, and varies considerably across browsers. – Matt Johnson-Pint Apr 07 '16 at 22:52
  • Now that I think about it, to get rid of all possible fallpits, there's a bunch to consider. I was just thinking using var UTCtime = (new Date()).toUTCString(); and then calculating the difference between that and the current timezone's time, storing it in "var offset" and use that, but there's more to it than that. Don't have the time for that right now. – user326964 Apr 08 '16 at 07:08
0

The string that JavaScript's Date function returns when you call .toString() on an instance, or when you implicitly cast to a string such as when writing to the console, is completely implementation dependent. You are not guaranteed to be able to get any time zone abbreviation at all. Some browsers may give an abbreviation, but others may spit out an entire time zone name, such as "Indian Standard Time", and others may just give an offset from UTC or GMT, such as "GMT+05:30". There's nothing in the ECMAScript spec that makes this string anything in particular.

Additionally, recognize that time zone abbreviations can be ambiguous. "IST" might stand for "Indian Standard Time", "Israel Standard Time", or "Ireland Standard Time".

More-so, the abbreviation for Sri Lanka has been "IST" in the time zone database since the switch from +06:00 to +05:30 that occurred in 2006. Before that, the abbreviation "LKT" was used in the database. You can read the entire history here. Wikipedia shows an abbreviation of "SLST", but this isn't implemented in any of the databases that are used for time zones in computing. If you have first-hand knowledge of this abbreviation to be in common use, you could request it to be updated via the tz discussion list at IANA.

In summary - if your system time zone is set for Sri Lanka (Asia/Colombo on Linux/Mac or Sri Lanka Standard Time|(UTC+05:30) Sri Jayawardenepura on Windows) and the JavaScript Date object is showing IST in your particular browser, then it is doing everything correctly and you should not attempt to change it.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
0

If you have set local time in your computer this will generate local time

   var date = new Date().toLocaleDateString();  //  3/15/2017
   var dateTime =new Date().toLocaleString();   //  3/15/2017, 12:23:55 AM
   var time  =new Date().toLocaleTimeString();  //  12:23:55 AM

 
Zoe
  • 27,060
  • 21
  • 118
  • 148
Chanaka Fernando
  • 2,176
  • 19
  • 19
0

This will provide you the answer

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

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

link

Dushee
  • 255
  • 5
  • 17