Time conversion Not working properly?

Tags: , , ,



I am using the ionic time picker in my project. When I select the time picker it passes a value to the controller. For example when I select 09:00pm, the console shows 79200. If I select 07:00pm the console shows 68400. I want to convert the value to 12 hrs format. I have followed some steps, but it’s not working for me.

My code:

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');

I tried the code above, but nothing is working. Below i have added my origional code:

$scope.timePickerObject12Hour.inputEpochTime = val;
    console.log(val);
    console.log('Selected epoch is : ', val, 'and the time is ', selectedTime.getUTCHours(), ':', selectedTime.getUTCMinutes(), 'in UTC');
  1. on the first console.log i am getting 68400,
  2. for second console log I am getting 68400 and the time is 19:00 in UTC. How to convert 12 hr format for the selected time?

Answer

I assume you want the result as a string. Here is a simple implementation with moment.js:

var secs = 68400;
console.log(moment().startOf('day').add(secs, 'seconds').format("h:mm a"));

Will output “7:00 pm”

See in plunker http://plnkr.co/edit/D0ai2PpEhnuJkTYblW29?p=preview



Source: stackoverflow