我在项目中使用离子时间选择器。当我选择时间选择器时,它会将值传递给控制器。例如,当我选择09:00 pm时,控制台显示79200.如果我选择07:00 pm,控制台显示68400.我想将值转换为12小时格式。我已经遵循了一些步骤,但它不适合我。
我的代码:
var a = new Date($scope.timePickerObject12Hour.inputEpochTime*1000);
console.log(a);
var b = moment.utc(a).format("HH:mm");
console.log(b)
$scope.timePickerObject12Hour.inputEpochTime = val;
console.log(val);
//var yourDateObject = new Date();
var selectedTime = new Date();
var amPmHour = $filter('date')(selectedTime, 'hh');
console.log(amPmHour);
$scope.time = $filter('date')(new Date(val*1000), 'hh:mma');
console.log($scope.time);
console.log('Selected epoch is : ', val, 'and the time is ', selectedTime.getUTCHours(), ':', selectedTime.getUTCMinutes(), 'in UTC');
我尝试了上面的代码,但没有任何效果。下面我添加了我的原始代码:
$scope.timePickerObject12Hour.inputEpochTime = val;
console.log(val);
console.log('Selected epoch is : ', val, 'and the time is ', selectedTime.getUTCHours(), ':', selectedTime.getUTCMinutes(), 'in UTC');
答案 0 :(得分:0)
您可以使用modulo operator:
接收12小时格式var hours = new Date(68400000).getHours(); // 20 o'clock in milliseconds
hours = hours % 12; // Division by 12
hours = hours ? hours : 12; // the hour '0' should be '12'
console.log(hours) // 8
答案 1 :(得分:0)
我假设您希望将结果作为字符串。这是一个使用moment.js的简单实现:
var secs = 68400;
console.log(moment().startOf('day').add(secs, 'seconds').format("h:mm a"));
将输出"晚上7点"
参见plunker http://plnkr.co/edit/D0ai2PpEhnuJkTYblW29?p=preview
答案 2 :(得分:0)
var secs = 68400;
console.log(moment().startOf('day').add(secs, 'seconds').format("hh:mma"));
看到我输出为 07:00pm!所以 07:00pm 被称为 12 小时制