import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { DaysComponent } from './jquery/days.component'; @NgModule({ declarations: [ DaysComponent ], imports: [ BrowserModule, ], providers: [ ], bootstrap: [DaysComponent] }) export class AppModule { }
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core'; declare var $: any; // Declaring $ as a variable so that we can use it to access jQuery @Component({ selector: 'count-days', templateUrl: './days.component.html' }) export class DaysComponent implements OnInit { message : string; days : number; @ViewChild('sd') sdate : ElementRef; @ViewChild('ed') edate : ElementRef; constructor() { } ngOnInit(): void { $( function() { $("#startDate").datepicker( {dateFormat : "yy-mm-dd"}); $("#endDate").datepicker( {dateFormat : "yy-mm-dd"}); } ); } getNoDays() { this.message = ""; this.days = 0; let startDate = this.sdate.nativeElement.value; let endDate = this.edate.nativeElement.value; if (startDate && endDate) { var start = new Date(startDate) var end = new Date(endDate) let ms = (end.getTime() - start.getTime()); // get no. of milliseconds between dates // find out no. of days this.days = ms / (1000 * 60 * 60 * 24); // divide total milliseconds by no. of milliseconds per day } else{ this.message = "Please select dates first!" } } }
<form> Start Date : <input type="text" #sd name="startDate" id="startDate"/> End Date : <input type="text" #ed name="endDate" id="endDate" /> <p></p> <button (click)="getNoDays()">No. Of Days</button> </form> <p></p> <h3 *ngIf="message">{{message}}</h3> <h3 *ngIf="days > 0">No. of days = {{days}} </h3>
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Angular and jQuery</title> <base href="/"> <!-- include jquery --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script> <script src="assets/jquery-ui.js"></script> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="assets/jquery-ui.css"> </head> <body> <count-days></count-days> </body> </html>