我想从我的约会中删除秒和毫秒

时间:2017-01-13 08:14:32

标签: javascript angularjs

现在,我发布这样的日期:

17:34:14.965

我只有几小时和几分钟。 怎么办?

3 个答案:

答案 0 :(得分:3)

你可以这样做

var date="17:34:14.965";

var splittedString=date.split(":");

date=splittedString.slice(0,-1).join(':');

参考splitslicejoin

答案 1 :(得分:0)

将字符串转换为日期对象。然后提取你想要的任何格式

  $scope.time = '17:34:14.965';
  var timeTokens = $scope.time.split(':');
  var newDate= new Date(1970, 0, 1, timeTokens[0], timeTokens[1], timeTokens[2]);
  $scope.hourAndMin = $filter('date')(newDate, 'HH:mm');



var app = angular.module("app", []);
app.controller("ctrl", function($scope, $filter) {
  $scope.time = '17:34:14.965';
  var timeTokens = $scope.time.split(':');
  var newDate= new Date(1970, 0, 1, timeTokens[0], timeTokens[1], timeTokens[2]);
  $scope.hourAndMin = $filter('date')(newDate, 'HH:mm');

});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  {{hourAndMin}}
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

用正则表达式替换将完成这项工作:

&#13;
&#13;
var dateString = '17:34:14.965'
console.log( dateString.replace(/:[^:]*$/,'') );
&#13;
&#13;
&#13;