如何使用 jQuery 从时间选择器中获取时间并将其转换为 ms-sql time(7) 格式?

How to get time from time picker using jQuery and convert into ms-sql time(7) format?

我想使用 java-script 从时间选择器中获取时间并转换为 ms-sql time(7) 格式。

这是我的时间选择器代码:

            <div class="form-group">
            <label class="col-md-4 control-label">
                In Time
            </label>
            <div class="col-md-5">
                <div class="input-group">
                    <input type="text" id="tpInTime" class="form-control timepicker timepicker-no-seconds">
                    <span class="input-group-btn">
                        <button class="btn default" type="button"><i class="fa fa-clock-o"></i></button>
                    </span>
                </div>
            </div>
        </div>

我刚刚意识到你只需要获取时间 7 的准确时间格式(我找到了格式文档 here)。所以我决定做这个棘手的把戏。

假设

  • 秒和毫秒为0
  • 使用时间选择器获取时间(不使用 Date())

逻辑

  • 获取时间选择器的值(查看查询)
  • 值应该是hh:mm
  • 按照Time 7的格式,应该是hh:mm:ss.ms
  • 所以我只是将 hh:mm:ss.ms 合并(查看查询)
  • 然后我将它存入一个变量time7,并显示它

提醒

Timezone is set to default (GMT+0)

试试吧!

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>

</body>
</html>
<div class="form-group">
  <label class="col-md-4 control-label">
    In Time
  </label>
  <div class="col-md-5">
    <div class="input-group">
      <input type="time" id="tpInTime" value="00:00">
      <span class="input-group-btn">
        <button class="btn default" type="button" id="btn"><i class="fa fa-clock-o">TEST</i></button>
        <p id="output"></p>
      </span>
    </div>
  </div>
</div>
<script>

  $("#btn").click(function(){
    var x = $("#tpInTime").val();
    var time7 = x+':00.0000000'
    $("#output").text(time7);
  });

</script>