Let the following snippet:
var dateJS = new Date("1950-09-09T23:00:00.000Z");
var dateMoment = moment("1950-09-09T23:00:00.000Z");
console.log("Javascript Epoch Time:", dateJS.valueOf());
console.log("Moment Epoch Time:", dateMoment.valueOf());
console.log("Is DST?", dateMoment.isDST());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>
If you run it with some Firefox versions (such as Firefox 61.0.1) or with Microsoft Edge you get Is DST? true
. Instead, if you run it with some other Firefox versions (such as 64.0) or any Chrome you get Is DST? false
.
What could be the reason?
NEW SNIPPET:
var mar1947 = moment("1947-03-20T00:00:00.000Z");
var sep1947 = moment("1947-09-10T00:00:00.000Z");
var mar1950 = moment("1950-03-20T00:00:00.000Z");
var sep1950 = moment("1950-09-10T00:00:00.000Z");
var mar2019 = moment("2019-03-20T00:00:00.000Z");
var sep2019 = moment("2019-09-10T00:00:00.000Z");
console.log("March 1947: Epoch Time / DST:", mar1947.valueOf(), mar1947.isDST());
console.log("September 1947: Epoch Time / DST:", sep1947.valueOf(), sep1947.isDST());
console.log("March 1950: Epoch Time / DST:", mar1950.valueOf(), mar1950.isDST());
console.log("September 1950: Epoch Time / DST:", sep1950.valueOf(), sep1950.isDST());
console.log("March 2019: Epoch Time / DST:", mar2019.valueOf(), mar2019.isDST());
console.log("September 2019: Epoch Time / DST:", sep2019.valueOf(), sep2019.isDST());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>
Please run the snippet with Chrome and then with Firefox 61.0.1 or Microsoft Edge and see if the result is the same.
It will not.
(Full playground here https://codepen.io/anon/pen/yZMqrV)
The answer to the question "Is MomentJS browser independent?" could be "No".
Getting the Long from a Moment date could depends on the browser you are using.
The same affects Date
object of Javascript.
The issue is related to an historical DST application in every country in the world and then with historical timezone offsets, which weren't observed until recent versions of ECMAScript. So old browser, such as Firefox 61.0.1 and Microsoft Edge haven't the right coverage of the DST rules in effect in all the various different locations around the world.
To solve this problem you have to use the .utc()
method of MomentJS (as VincenzoC's comment in my answer).
So, the following code works in every browser: it always returns the same time (Epoch Time) and it "normalizes" DST for every time (it is always false):
var mar1947 = moment("1947-03-20T00:00:00.000Z").utc().startOf('day');
var sep1947 = moment("1947-09-10T00:00:00.000Z").utc().startOf('day');
var mar1950 = moment("1950-03-20T00:00:00.000Z").utc().startOf('day');
var sep1950 = moment("1950-09-10T00:00:00.000Z").utc().startOf('day');
var mar2019 = moment("2019-03-20T00:00:00.000Z").utc().startOf('day');
var sep2019 = moment("2019-09-10T00:00:00.000Z").utc().startOf('day');
console.log("March 1947:", mar1947.valueOf(), mar1947.isDST());
console.log("September 1947:", sep1947.valueOf(), sep1947.isDST());
console.log("March 1950:", mar1950.valueOf(), mar1950.isDST());
console.log("September 1950:", sep1950.valueOf(), sep1950.isDST());
console.log("March 2019:", mar2019.valueOf(), mar2019.isDST());
console.log("September 2019:", sep2019.valueOf(), sep2019.isDST());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>
Please be aware that this method doesn't work propertly if you use it after (instead of before) other methods, like startOf()
(see related issue on github)