1

I'm trying to get number of days passed. I'm storing epoch (milliseconds) for date. I'm reading startDate from database (date value of first record) in milliseconds and I want to find current epoch in specified timezone.

I tried this:

var startDate = rows[0]['MIN_DATE'];
var endDate = moment().tz("America/New_York");

Then to calculate difference, I used:

var oneDay = 24 * 60 * 60 * 1000;
var daysCount = Math.ceil((endDate - startDate) / (oneDay));

The value of startDate is:

1522821600000 which is: Wednesday, April 4, 2018 2:00:00 AM GMT-04:00 DST

The value of endDate is:

Moment_d: Wed Apr 04 2018 22:24:45 GMT-0400 (EDT)_isAMomentObject: true_isUTC: true_isValid: true_locale: Locale_offset: -240_pf: Object_z: Zone__proto__: Object

The value of daysCount is 2, how? How can I get milliseconds instead of object from:

moment().tz("America/New_York");
Ashutosh
  • 4,371
  • 10
  • 59
  • 105

3 Answers3

1

To directly answer your question, use .valueOf() to get the value of moment.tz("America/New_York")

var endDate = moment.tz("America/New_York").valueOf()

I'm having difficulty understanding your question, but I believe you're trying to get the difference between the days considering the correct timezone. The following gives an accurate result using .diff() (https://momentjs.com/docs/#/displaying/difference/)

var timeZone = "America/New_York"
var startDate = 1522821600000
var momentStartDate = moment.tz(startDate,timeZone)
var momentEndDate = moment.tz(timeZone)

alert(momentEndDate.diff(momentStartDate, 'days') );
JaanRaadik
  • 571
  • 3
  • 11
0

Use fromNow() function. It is very straight-forward.

Do like this :

moment(date).fromNow();

It will give you number of days passed if date is past as well as number of days to go if date is future.

Below are is example:

var date = 1522821600000; // your date

console.log(moment(date).fromNow());
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
  • @Ashutosh for that look this question: https://stackoverflow.com/questions/15347589/moment-js-format-date-in-a-specific-timezone – Vikasdeep Singh Apr 05 '18 at 07:17
0

Solved this with following statement:

var endDate = moment().tz("America/New_York").valueOf();
Ashutosh
  • 4,371
  • 10
  • 59
  • 105