Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS how to handle a Bootstrap datepicker date to send it out for a REST backend

I have a angular-strap bootstrap datepicker

<input id="dp5" class="span8" type="text" ng-model="obj.date" data-date-format="mm/dd/yyyy"  placeholder="Pick a Date" bs-datepicker>

The backend is a Spring MVC REST application which originally returns the date in milliseconds (java.util.Date). The date I receive from the above datepicker element is in the following format

2013-10-01T06:00:00.000Z

How can I convert it to milliseconds so that I can properly ship it to the backend?

like image 358
user6123723 Avatar asked Feb 15 '23 20:02

user6123723


2 Answers

As I know you can create directive to handle it:

Demo Plunker

app.directive('datetimez', function() {
    return {
        restrict: 'A',
        require : 'ngModel',
        link: function(scope, element, attrs, ngModelCtrl) {
          element.datetimepicker({
            dateFormat:'dd/MM/yyyy hh:mm:ss',
            language: 'pt-BR'
          }).on('changeDate', function(e) {

            var outputDate = new Date(e.date);

           var n = outputDate.getTime();


           ngModelCtrl.$setViewValue(n);
            scope.$apply();
          });
        }
    };
});

And HTML wrapper for date-picker should be like:

  <div id="date" class="input-append" datetimez ng-model="var1">

So after date change var1 gets milliseconds (see Demo)

Hope this direction will help

like image 156
Maxim Shoustin Avatar answered Feb 17 '23 12:02

Maxim Shoustin


You can call Date.parse function on the string:

Date.parse("2013-10-01T06:00:00.000Z") // 1380607200000
like image 37
Klaster_1 Avatar answered Feb 17 '23 10:02

Klaster_1