Search code examples
angularjsangulardateangularjs-directiveangular-moment

How to convert angular-moment time to date format like Thu Jan 01 1970 15:00:00 GMT+0000 (Country)


I am using angular-moment-picker to set time ( hours and minutes). Whenever i want to to save data I got time like HH:mm. My aimis to convert this time to date format since 1970 as follows: Thu Jan 01 1970 15:00:00 GMT+0000 (Country) where the 15:00 is time setted in angular-moment-picker here is my source code:

<div class="form-group">
  <p class="icon-group input-group ">
    <input readonly ng-model="workTime.startTime" class="form-control">
    <span class="input-group-btn ">
      <button moment-picker="workTime.startTime" format="HH:mm" type="button" class="btn btn-default" id="startTime">
        <i aria-hidden="true" class="glyphicon glyphicon-calendar"></i>
      </button>
    </span>
    <span class="show-date-picker"></span>
  </p>
</div>

Solution

  • In your angular controller you can instantiate the Date:

    var startDate=new Date();
    

    Then you can set the Day, Month, and the Year for this date as suggested. After that you split and set the time to your date as follow:

    var start=$scope.startTime.toString().trim().split(':');
    startDate.setHours(start[0]);
    startDate.setMinutes(start[1]);
    startDate.setSeconds(0);
    

    It works for me.

    Best Regards