Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Date and java.util.Date constructor argument

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).

like image 230
ike3 Avatar asked Jan 18 '12 15:01

ike3


People also ask

What does the JavaScript Date () function do?

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.

What is Date constructor?

Date() constructor. The Date() constructor can create a Date instance or return a string representing the current time.

Why does JavaScript month start at 0?

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.

How can you get the current Date using JS Date objects?

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.


2 Answers

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.

like image 77
Alexander Bird Avatar answered Oct 05 '22 20:10

Alexander Bird


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).`
like image 39
Ortomala Lokni Avatar answered Oct 05 '22 19:10

Ortomala Lokni