Skip to content Skip to sidebar Skip to footer

How To Get Sri Lanka Time Abbreviation From Date() Function In Javascript

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

Solution 1:

I think this is what you expect as the answer

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

Solution 2:

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.


Solution 3:

This will provide you the answer

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

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

link


Solution 4:

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.


Solution 5:

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.


Post a Comment for "How To Get Sri Lanka Time Abbreviation From Date() Function In Javascript"