1

I am having datetime available. All I need to extract date in dd/MM/yyyy and time in hh:mm a format.

I am try below code, but this is not working. What is wrong with the code and How can I achieve the said target?

$scope.date = new Date(dateTimeObj).getDate().toLocaleString("dd/MM/yyyy");
$scope.from = new Date(dateTimeObj).getTime().toLocaleString("hh:mm a");
$scope.to = new Date(dateTimeObj).getTime().toLocaleString("hh:mm a");

edit: I achieved the above functionality using momentjs.

$scope.to moment(dateTimeObj).format("HH:mm A");

Now I'm facing a different problem. This value is not getting binded to the view. Html is as below.

<input type="time" class="form-control" placeholder="To" ng-model="to" id="to" value="{{to}}">

How can I bind the value of time object with HTML5's time control?

sachin
  • 63
  • 1
  • 11
  • Please, this question has been solved dozens of time. Please search before asking. – lin Sep 18 '17 at 09:09
  • @lin: apologies.I'm on a deadline,Sir, I tried and didn't find any useful link and hence I asked this question. – sachin Sep 18 '17 at 09:35
  • Please close / delete this question and check the link I posted. Thanks – lin Sep 18 '17 at 09:37

2 Answers2

2

{{date | date:'dd-MM-yyyy'}} {{date | date:'hh:mm a'}}

In JS

$scope.date = new Date();

https://plnkr.co/edit/aXdMMPmZXtuzTLfX1mXu?p=preview

Alexa
  • 77
  • 11
1

Try this:

var myApp = angular.module('myApp',[]);
myApp.controller('pageCtrl', ['$scope','$filter', function ($scope,$filter) {
   var dateStr = '2015-09-21 18:30:00';
   $scope.dt = $filter('date')(new Date(dateStr.split('-').join('/')), "dd/MM/yyyy hh:mm a");
}]);
santho
  • 366
  • 2
  • 16