Moment.js: Convert datetime string into UTC

Viewed 20

I'm trying to use moment.js library to convert a datetime string into a local UTC. How do I do that?

html:

<div style="float:left; margin-right: 5em;">
            <label id="startDateTimeLabel">Start DateTime</label>
            <div class="input-group input-append date" id="startDateTime" style="width:300px;">
                  <input type="text" class="form-control">
                  <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
            </div>
            
        </div>
        <input id="ButtonConnect" class="btn btn-primary" type="button" value="Connect" onclick="ButtonGetSelectedDate();" />


        <script>

        
        const startPicker = new tempusDominus.TempusDominus(document.getElementById('startDateTime'));
        var tStartDatetime = startPicker.dates.picked;

//the below line seems to be a javascript datetime;                 
        console.log('tStartDatetime : ' + tStartDatetime ); //this is the format: Sat May 15 2021 12:30:00 GMT-0400 (Eastern Daylight Time)
        var tStart = moment(tStartDatetime ).valueOf();
        console.log('tStart : ' + tStart ); //this prints Nan on the console
        </script>
1 Answers

this is what I did and it seems to work ok.

    tStart = moment(new Date(tStartDatetime)).valueOf();
Related