如何使用angular获得AM/PM的12小时格式

how to get 12 hrs format with AM/PM using angular?

本文关键字:PM 12小时 格式 AM 何使用 angular 获得      更新时间:2023-09-26

$scope.timePickerObject12Hour = {
      inputEpochTime: ((new Date()).getHours() * 60 * 60 + 3600),  //Optional
      step: 15,  //Optional
      format: 12,  //Optional
      //titleLabel: '12-hour Format',  //Optional
      closeLabel: 'Close',  //Optional
      setLabel: 'Set',  //Optional
      setButtonType: 'button-positive',  //Optional
      closeButtonType: 'button-stable',  //Optional
      callback: function (val) {    //Mandatory
        timePicker12Callback(val);
      }
    };
    
    function timePicker12Callback(val) {
      if (typeof (val) === 'undefined') {
		  
        console.log('Time not selected');
      } else {
		 
        $scope.timePickerObject12Hour.inputEpochTime = val;
		console.log(val);
        var selectedTime = new Date(val * 1000);
       
      }
	 
    }

我使用了angular和ionic应用程序。在这里我关注了这个gituphttps://github.com/rajeshwarpatlolla/ionic-timepicker对于时间选择器。当我选择时间(例如,我选择了下午5点)控制台时,我得到的值为61200,如下所示或选定的历元是:61200,时间是UTC的17:0。我如何将其转换为12小时格式的AM/PM格式?任何人都可以帮助我完成

$scope.timePickerObject12Hour.inputEpochTime = val;
        console.log(val);
        var selectedTime = new Date(val * 1000);
        console.log('Selected epoch is : ', val, 'and the time is ', selectedTime.getUTCHours(), ':', selectedTime.getUTCMinutes(), 'in UTC');

例如:

var yourDateObject = new Date();
var amPmHour = $filter('date')(yourDateObject, 'hh');

不要忘记为此调用$filter模块

来自Angularjs日期文档