Changing the Timezone on a JS Date without altering the time specified?

Viewed 736
var schedule = {
    start_at = Date ( '2017-10-10' ), // Not editable - this data is being handed off by an ORM
    ...
};

// Mon Oct 09 2017 20:00:00 GMT-0400 (Eastern Daylight Time)
console.log ( schedule.start_at );

Is it possible to alter the time of an ISO Date to match 00:00:00 of the current timezone, so the output would, in this case, be Tue Oct 10 2017 00:00:00 GMT-0400 (Eastern Daylight Time)?

I have experimented using moment.utc (), but that had only helped to format a date to a string. The output needs to be a Date object that which be passed along some further logic.

I have spent hours looking up the Moment.js documentation but had found nothing that I could use to solve this problem.

(Off note: I am adding this to the list of cons in using Node.js for API/Server development. ISO Date management has been such a nightmare in my experience.)

EDIT:

So I am mistaken. It turns out that schedule.start_at is a Date object set to Mon Oct 09 2017 20:00:00 GMT-0400 (Eastern Daylight Time). [question changed to reflect]. My question still remains, how may I alter a date object to change the timezone while maintaining the specified time?

2 Answers

Is this what you are looking for: moment ( schedule.start_at + ' 00-0400' ).toDate();

var schedule = {
    start_at : '2017-10-10'
};

var date = moment ( schedule.start_at ).toDate();
console.log(date.toString());

var date2 = moment ( schedule.start_at + ' 00-0400' ).toDate();
console.log(date2.toString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

I was able to accomplish this task by using a combination of moment.clone(), and moment.add().

var date = moment ( schedule.start_at );
var clone = date.clone ();
clone.add ( date.toDate ().getTimezoneOffset (), 'minutes' );
clone.add ( 0 - clone.utcOffset (), 'minutes' );

The output now is successfully set to Tue Oct 10 2017 00:00:00 GMT-0400 (Eastern Daylight Time) as desired.

Related