Displaying timezone-formatted date as UTC time
<p>on my UI, I try to display a date based on a specific timezone. In this example, I will use Americas/New_York as the timezone. This is how I did it.</p>

<pre><code>    $scope.getStartTime = function(){
        var date = new Date();
        return moment(date).tz("Americas/New_York").format('YYYY-MM-DD HH:mm:ss');
    };
</code></pre>

<p>Afterwards, I want to send this data and send it to my server. In my server however, I want it so that it is always serialized into UTC time instead of in the New York Timezone (EST). </p>

<p>For example, if the time was 12:00 P.M. in New York, then the time would be serialized to 4:00 P.M. in UTC time before it was sent to the backend. This was my attempt:</p>

<pre><code>    var date = getStartTime();
    ....
    // Display the date in the UI
    ....
    $scope.revertStartTime(date);

    $scope.revertStartTime = function(startTime) {
        console.log("Start time: ", startTime);
        console.log("Moment: ", moment(startTime).format());
        console.log("Converted to utc time: ", moment().utc(startTime).format());
        return moment.utc(startTime).format("YYYY-MM-DD'T'HH:mm:ss.SSSZ");
    }
</code></pre>

<p>I tried to revert the start time by using the <code>moment().utc()</code> function and hoped that the date would change to a UTC based date but unfortunately it keeps turning my date into the localized date instead of UTC date and I'm not sure why. Any help would be appreciated. Thanks!</p>

<p>Edit:</p>

<p>Tried to follow the below method and here is what I did:</p>

<pre><code>    $scope.getStartTime = function(){
        var date = new Date();
        var startTime = new moment(date).tz($rootScope.userinfo.timeZone).format('YYYY-MM-DD HH:mm:ss');
        $rootScope.offset = moment().utcOffset(startTime);    
        console.log("offset: ", $rootScope.offset);
        return startTime;
    };

    $scope.revertStartTime = function(startTime) {
        console.log("User Selected Time: ", moment().utcOffset(startTime).format('YYYY-MM-DD HH:mm:ss'));
        return moment().utcOffset(startTime).format('YYYY-MM-DD HH:mm:ss');
    }
</code></pre>

<p>But all I get is an error saying that <code>revertStartTime</code> returns an <code>Invalid Date</code>.</p>