问题
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 ?
回答1:
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.
回答2:
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.
回答3:
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 JS Date object is showing IST in your particular browser, then it is doing everything correctly and you should not attempt to change it.
回答4:
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
回答5:
This will provide you the answer
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Date();
</script>
link
来源:https://stackoverflow.com/questions/36475804/how-to-get-sri-lanka-time-abbreviation-from-date-function-in-javascript