0

I have an API call that receives a date string like 2016-2-12 //('YYY-MM-DD').

I am using moment-timezone, but not having any luck.

The end goal is to get the unix timestamp for midnight 2016-2-12 of the timezone of the applicable site (not the server)

I have tried a lot of combinations of something like:

moment.tz('2016-2-12', 'America/New York').utc();

But it seems to receive the input date as UTC (my server is set to UTC) then do the timezone conversion AFTER, giving me a date for the day before.

For reference I am generating a report for a date range. So if the input is:

&start=2016-4-12&stop=2016-4-15 //and the timezone is America/New York

I expect a start time of 2016-4-12T00:00:00 -04:00 but i keep ending up with dates from the day before.

I expect the function to be something like:

moment.tz('2016-2-12', 'YYYY-MM-DD', 'America/New York').startOf('day').utc();

user3624154
  • 196
  • 1
  • 2
  • Then I think your API should accept timezone as well. CMIIW –  Apr 14 '16 at 14:07
  • I guess "*the time zone of the applicable site*" means you want the string treated as local (which, since it has no time zone, is per ISO 8601 but not ECMAScript 2015) then this is a duplicate of many other questions like [*How to assume local time zone when parsing ISO 8601 date string*](http://stackoverflow.com/questions/15517024/how-to-assume-local-time-zone-when-parsing-iso-8601-date-string), without the moment.js tag. – RobG Apr 15 '16 at 03:21

1 Answers1

0

As soon as i asked the question, after hours of screwing around, i got it.

moment.tz('2016-4-14', 'YYYY-MM-DD', 'America/New York').format();

is yielding 2016-04-14T00:00:00-04:00

Sorry to waste time here... I hope someone else finds this helpful...

moment.tz('2016-4-14', 'YYYY-MM-DD', 'America/New York').endOf('day').format();

is yielding 2016-04-14T23:59:59-04:00 as well and i can easily get unix timestamp using utc()

user3624154
  • 196
  • 1
  • 2
  • That is only helpful if you know the [*IANA time zone*](https://www.iana.org/time-zones) of the host. Far simpler to just parse it as local (see [*How to assume local time zone when parsing ISO 8601 date string*](http://stackoverflow.com/questions/15517024/how-to-assume-local-time-zone-when-parsing-iso-8601-date-string)). – RobG Apr 15 '16 at 03:23