代码之家  ›  专栏  ›  技术社区  ›  SoftwareDveloper

Moment.js:将日期时间字符串转换为UTC

  •  0
  • SoftwareDveloper  · 技术社区  · 3 年前

    我正在尝试使用moment.js库将日期时间字符串转换为本地UTC。我该怎么做?

    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>
    
    0 回复  |  直到 3 年前
        1
  •  0
  •   SoftwareDveloper    3 年前

    这就是我所做的,而且似乎还可以。

        tStart = moment(new Date(tStartDatetime)).valueOf();
    
    推荐文章