How to capture Date (with pickadate) to use in a http get request

I am using Angular 2, and the idea is for it to send request to a REST service I've done in api.

I'm currently checking the results in Plunker. I am having a lot of trouble capturing the date selected, so I can then use it to make the http request.

So I would like to know how this can be done, or if there is another, easier way of doing this.

I have tried everything seen here and a few other things but nothing works (and with that I mean, I don't manage to get the value in a variable that I can then use for the operations in the .get() and .subscribe() to make the request to the REST service.

This is the closer I've got:

Calendar.js:

function myCalendar(){
  $('.datepicker').pickadate({
        selectMonths: true, // Creates a dropdown to control month
        selectYears: 15, // Creates a dropdown of 15 years to control year
         onSet: function(context) {
        console.log(new Date(context.select));
 }
  });
}

calendar.ts

import {Component} from 'angular2/core';
@Component({
  selector: 'cal-inside',
  template: `<input value="Input date" type="text" class="datepicker" onclick ="myCalendar()" >
`,
  directives: []
})
export class CalendarioDentro {
  constructor() {
      }    
}

And that selector is called here:

src/basic_usage.html

[...]
 <form>
          <cal-inside></cal-inside>
          <check-inside></check-inside>
          <boton></boton>

        </form>
     [...]

This all does work, I get a calendar inside a sidenav component, and now I would like to know how to keep going, since I can't get the syntax for the pickadate() function right, no matter what.

What I currently have only shows on console that I have indeed clicked on a date. I can't manage to get anything working other than that, and when I look arround for info it's like it's not done how the API says it's done (from what I understand, something like this var picker = $input.pickadate('picker') picker.get('select', 'yyyy/mm/dd') ).

So is there some key concept I'm missing to not being able to use .get() right? Because no matter what syntax I use I get errors everywhere, and when I don't (using something similar to what I have managed with the onset: syntax, I get nothing displayed).

For example I see jquery being used, sometimes, like here How can I consolidate pickadate initializer for multiple inputs on one page? and I can translate that into what I need.

Answers:

Answer

Doesn't seem like others have much trouble with this but since I went crazy trying to properly get it, I'll put it here just in case:

This currently works and displays the date in console. Pending work would be to do the http get itself.

 var foo;
function CalendarMaterializeCSS(){
$('.datepicker').pickadate({
    onSet: function(context) {
        foo = new Date(context.select);
        var curr_date = foo.getDate();

        var curr_month = foo.getMonth();

        var curr_year = foo.getFullYear();
        var dateFormated=(curr_date + "-" + curr_month + "-" + curr_year);
        console.log(dateFormated);

    }
});

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.