0

i'm trying to use a jQuery Datepicker in my angular application. From the server I get a date object, format yy-mm-dd. When I set this Format as "dateFormat" option my date is displayed right. But I like to get the following Format only for the UI: dd.mm.yy. When I set this format in the options the date 1959-12-31 results in 05.03.2022. And this is also the Format that I get from the datepicker if I click on the Save button. So the only working format is yy-mm-dd, it would be great if anyone can help me out. I tried a lot of other options and read a lot of other similiar questions here but I could not get any solution to work. I also tried a hidden input field to store the alternate value.

Here is my Angular component witch uses the jQuery picker:

import {forwardRef, Input, Output, EventEmitter, ViewChild, ElementRef, AfterViewInit, Component} from '@angular/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from "@angular/forms";

const DATE_PICKER_VALUE_ACCESSOR = {
  provide: NG_VALUE_ACCESSOR,
  useExisting: forwardRef(() => DatePicker),
  multi: true
};
declare var jQuery: any;

@Component({
  selector: 'jQueryDate',
  template: `<input #datePickerInput type="text">
<input type="hidden" id="datePickerAlternate" #datePickerAlternate>`,
  providers: [DATE_PICKER_VALUE_ACCESSOR]
})
export class DatePicker implements AfterViewInit {
  private onTouched = () => {
  };
  private onChange: (value: string) => void = () => {
  };

  @Input() options: any = {
    closeText: "Schließen",
    prevText: "&#x3C;Zurück",
    nextText: "Vor&#x3E;",
    currentText: "Heute",
    dateFormat: 'dd.mm.yy',
    isRTL: false,
    showMonthAfterYear: false,
    altField: "#datePickerAlternate",
    altFormat: "yy-mm-dd",
    dayNames: ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"],
    monthNames: ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"],
    dayNamesMin: ["Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"],
    weekHeader: "KW",
    firstDay: 0

  };

  @Input() value: Date;

  writeValue(date: Date) {
    console.log("datepicker value " + date);

    jQuery(this.input.nativeElement).datepicker('setDate', date);
    this.alternate.nativeElement.value = date;
    console.log("value of alternate: " + this.alternate.nativeElement.value);
  }

  registerOnChange(fn: any) {
    this.onChange = fn;
  }

  registerOnTouched(fn: any) {
    this.onTouched = fn;
  }

  @Output() dateChange = new EventEmitter();

  @ViewChild('datePickerInput') input: ElementRef;
  @ViewChild('datePickerAlternate') alternate: ElementRef;

  ngAfterViewInit() {
    jQuery(this.input.nativeElement).datepicker(Object.assign({}, this.options, {
      onSelect: (value) => {
        this.value = value;
        this.onChange(value);
        this.onTouched();
        this.alternate.nativeElement.value = value;

        this.dateChange.next(value);
      }
    }))
      .datepicker('setDate', this.value);

  }
}

And here is the use of the component:

  <jQueryDate class="" name="txtBirthdate" #txtBirthdate="ngModel" type="date" id="txtBirthdate"
                      [(ngModel)]="this.person.txtBirthdate"  ngDefaultControl></jQueryDate>
mwim
  • 51
  • 1
  • 1
  • 6
  • `When I set this format in the options the date 1959-12-31 results in 05.03.2022` Can you show it here? https://plnkr.co/edit/q9qVaNIPwUTT91oXbYny?p=preview I don't completely understand what do you mean. – yurzui Dec 05 '16 at 12:32
  • That's strange, there must be something wrong with the date that I get from the server. If I set the birthdate manually everything works fine. – mwim Dec 05 '16 at 13:11
  • @yurzui I fixed the display problem with this: @Input() value: any; newDate: Date; writeValue(date: String) { console.log("datepicker value " + date); this.newDate = new Date(date); jQuery(this.input.nativeElement).datepicker('setDate', this.newDate); this.alternate.nativeElement.value = this.newDate; } – mwim Dec 05 '16 at 13:25
  • Glad to hear :) – yurzui Dec 05 '16 at 14:37
  • @yurzui maybe you can help me out again, im trying to include jquery in my webpack.ci.js, not as script in the index.html. Because when I put the scripts there, they are not build. Do you have any idea why jquery isnt found? I need these two files to get the datepicker work: > – mwim Dec 06 '16 at 14:44
  • http://stackoverflow.com/questions/39424340/include-jquery-in-angular2-with-webpack-and-access-it-from-component/39426399#39426399 – yurzui Dec 06 '16 at 14:45
  • @yurzui for some reason, this dosent work for me, I get Error: Uncaught (in promise): ReferenceError: jQuery is not defined. – mwim Dec 06 '16 at 14:48
  • Would be great if you share your project on github – yurzui Dec 06 '16 at 14:49
  • @yurzui I would love to, but this is not possible because of company secrets. In your linked answer you talk about an "entry file" which do you excactly mean? the app.module.ts? – mwim Dec 06 '16 at 14:52
  • You can also check this thread https://github.com/AngularClass/angular2-webpack-starter/issues/215 Here is information about entry file http://webpack.github.io/docs/configuration.html#entry – yurzui Dec 06 '16 at 14:56
  • `ReferenceError: jQuery is not defined` Is it runtime error? Or you can see it in your IDE? – yurzui Dec 06 '16 at 15:00
  • runtime error, it is thrown when I open my dialog that includes the datepicker – mwim Dec 06 '16 at 15:11

0 Answers0