Yes, another question about Date in Java and Javascript.
The timezone is GMT+4 (Moscow) both in Java and Browser (Chrome).
<script language="javascript">
var d = new Date(170798400000);
document.write(d);
</script>
Gives: Sun Jun 01 1975 00:00:00 GMT+0400 (Russian Standard Time)
public class Test {
public static void main(String[] args) {
java.util.Date d = new java.util.Date(170798400000L); // the same epoch value!
System.out.println(d);
}
}
Gives: Sat May 31 23:00:00 MSK 1975
If I change the epoch value to something like 2011-2012 year (after daylight saving was canceled in Russia) the output is OK. Timezone updater tool ran OK.
Is this a bug or documented feature? Is there any way to handle this except formatting and re-parsing like YYYY-MM-dd HH:mm:SS or so?
from javadoc:
Date(long date)
Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.
from javascript reference:
new Date(milliseconds)
milliseconds - Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC (Unix Epoch).
Date methods allow you to get and set the year, month, day, hour, minute, second, and millisecond of date objects, using either local time or UTC (universal, or GMT) time.
Date() constructor. The Date() constructor can create a Date instance or return a string representing the current time.
because they might have an array of string (indexed from 0) of month names and these month numbers if they start from 0, it'll be lot easier to map to the month strings.. Save this answer.
In JavaScript, we can easily get the current date or time by using the new Date() object. By default, it uses our browser's time zone and displays the date as a full text string, such as "Fri Jun 17 2022 10:54:59 GMT+0100 (British Summer Time)" that contains the current date, time, and time zone.
Is this a bug or documented feature?
This is not a bug with Javascript. At least, I see no way I could claim that.
The browser's Javascript engine is returning the time converted to "GMT+4". What you are wanting, apparently, is MSK which is different from GMT+4 (as noted in your comment). Javascript not knowing about MSK doesn't count as a bug, but a lack of a feature. Perhaps js is "wrong" for not having that detailed knowledge of timezones, but it's not a bug.
Is there any way to handle this except formatting and re-parsing like YYYY-MM-dd HH:mm:SS or so?
Keeping track of all the arbitrary details of timezones requires a lot of work. I know of no such codebase which has all that work available for javascript. Therefore, I believe that, yes, you would have to manually code that conversion yourself if you want to use true MSK.
String representation of time (like "Sun Jun 01 1975 00:00:00 GMT+0400"
) are used for human. Time value (milliseconds since 1 January, 1970 UTC) are used for storage and calculation.
There is no bug there. In JavaScript, according to the specification, the contente of string representation is implementation-dependant. In Java, according to the documentation, the daylight saving time may be reflected.
From JavaScript specification:
15.9.5.2
Date.prototype.toString ( )
This function returns a String value. The contents of the String are implementation->dependent, but are intended to represent the Date in the current time zone in a convenient, human-readable form.
From Java documentation:
java.util.Date, public String toString()
Converts this Date object to a String of the form: dow mon dd hh:mm:ss zzz yyyy
where:
...
zzz is the time zone (and may reflect daylight saving time).`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With