首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以给定分钟为输入的用户开始时间和结束时间之间的分隔时间

以给定分钟为输入的用户开始时间和结束时间之间的分隔时间
EN

Stack Overflow用户
提问于 2016-11-16 15:14:09
回答 3查看 3K关注 0票数 1

在我的应用程序中,我有一个预约与精神病学家预约的顺势。在这里,用户给出的输入分钟,与医生交谈,所以在下拉它应该看起来像一个正常的时间,如上午9:10,10-11下午等到下午5:00,这样客户就可以看到可用的时间预定预约。我在努力争取这次分手。我只完成了从mins到几个小时的转换,在此之后,我使用了time split.Hoping在这里提供一些帮助,或者使用任何有效的指导来获得时间分割。

主计长:

代码语言:javascript
复制
myApp.controller('ctrl', function ($scope) {
       $scope.calc = function () {
         var time = $scope.timeSelect;
                if (time < 60) {
               var a = (time) + 'm';
                } else if (time % 60 == 0) {
                    var a =  (time - time % 60) / 60 + 'h';
                } else {
                    var a = ((time - time % 60) / 60 + 'h' + ' ' + time % 60 + 'm');
                }
                $scope.result =a;
            }        
            $scope.result='';
});
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-11-16 16:04:12

每当您处理日期和时间时,我强烈建议您使用Momentj.js。下面是一个非常精心设计的示例,演示如何使用Moment.js设置时间段。

代码语言:javascript
复制
angular.module("app", ["angularMoment"])
  .controller("ctrl", function($scope, moment) {
    var startingTime = moment().hours(9).minutes(0);
    var endingTime = moment().hours(17).minutes(0);
    $scope.selectedTimeslot = "";
    $scope.intervals = [15, 30, 45, 60, 90, 120];
    $scope.interval = 60;
    $scope.timeslots = [];

    $scope.setTimeSlots = function() {
      $scope.timeslots = [];
      var currentStartTime = angular.copy(startingTime); // use copy to avoid reference issues since we're dealing with objects and not primitives
      while (currentStartTime < endingTime) {
        $scope.timeslots.push(currentStartTime.format("h:mm") + " - " + currentStartTime.add($scope.interval, "minute").format("h:mm"));
      }
    }

    $scope.setTimeSlots();

  });
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.16.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-moment/1.0.0/angular-moment.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  Interval in minutes: <select ng-model="interval" ng-change="setTimeSlots()" ng-options="i for i in intervals"></select>
  <br/>Timeslots:
  <select ng-model="selectedTimeslot" ng-options="slot for slot in timeslots">
    <option value="">Please select</option>
  </select><br/><br/>
  Selected timeslot: {{selectedTimeslot}}
</div>

票数 4
EN

Stack Overflow用户

发布于 2016-11-16 15:47:57

这可能对你有用:

代码语言:javascript
复制
function createTimeSlots(startHour, endHour, interval) {
    if (!startHour) {
        endHour = 8;
    }
    if (!endHour) {
        endHour = 20;
    }
    var timeSlots = [],
        dateTime = new Date(),
        timeStr = '';
    dateTime.setHours(startHour, 0, 0, 0);
    while (new Date(dateTime.getTime() + interval * 60000).getHours() < endHour) {
        timeStr = dateTime.getHours() + ':' + dateTime.getMinutes();
        timeStr += '-';
        dateTime = new Date(dateTime.getTime() + interval * 60000);
        timeStr += dateTime.getHours() + ':' + dateTime.getMinutes();
        timeSlots.push(timeStr);
    }
    return timeSlots;
}

console.log(createTimeSlots(9, 18, 45));

"9:0-9:45“、"9:45-10:30”、"10:30-11:15“、"11:15-12:0”、"12:0-12:45“、"12:45-13:30”、“13:30-14:15:15”、"14:15-15:0“、"15:0-15:45”、"15:45-16:30“、”16:30-17:15:15“

您可能需要在getHours()和getMinutes()上添加一些0填充,以便将1:0打印为01:00。

票数 1
EN

Stack Overflow用户

发布于 2016-11-16 15:58:34

从这个问题中,我大致了解了你需要什么,所以我希望这能帮助你在路上。首先,我将创建一个单独的函数,将时间(从午夜到午夜以分钟为单位)转换为字符串,如下所示:

代码语言:javascript
复制
function timeToString(time) {
    if (time < 12 * 60) 
        return (time - time % 60) / 60 + ":" + time % 60 + "am";
    else return (time - time % 60 - 12*60) / 60 + ":" + time % 60 + "pm";
}

然后可以创建用于填充下拉列表的数组,如下所示:

代码语言:javascript
复制
var times = [];
for (t = startHour * 60; t < endHour * 60; t += duration) {
    times[times.length] = timeToString(t) + ' - ' + timeToString(t + duration);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40635665

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档