user5503464
user5503464

Reputation:

How can i convert value to time using angularjs?

I am using Ionic Timepicker. When I select a time it's passing values like 75600 (here I have selected 09:00pm). How to actually retrieve a human readable string instead of an "epoch time" value?

Here's my code:

    $scope.timePickerObject12Hour.inputEpochTime = val;
    console.log(val);
    // here i am getting 79200
    var selectedTime = new Date();
    var amPmHour = $filter('date')(selectedTime, 'hh');
    console.log(amPmHour);
    // here i am getting 07
    console.log('Selected epoch is : ', val, 'and the time is ', selectedTime.getUTCHours(), ':', selectedTime.getUTCMinutes(), 'in UTC');
    //here i am getting Selected epoch is :  75600 and the time is  16 : 5 in UTC

Upvotes: 0

Views: 100

Answers (1)

ccjmne
ccjmne

Reputation: 9618

Simply using the plain Javascript Date object:

new Date($scope.timePickerObject12Hour.inputEpochTime*1000);

Don't forget to multiply the "epoch time" value you get from Ionic Timepicker by 1000, since the Javascript Date constructor takes a number of milliseconds, not seconds.

in your case, the following code:

console.log($filter('date')(new Date(79200*1000), 'hh:mma'));

... would output:

09:00PM

Here's a working JSFiddle (link) that will feature a working example using the tools you're working with.

Upvotes: 1

Related Questions