Answers for "date medium angularjs in ts file"

0

how to use datepipe in ts file

// Since CommonModule does not export it as a provider you'll have to do it yourself. This is not very complicated.

//1) Import DatePipe:

import { DatePipe } from '@angular/common';
// 2) Include DatePipe in your module's providers:

NgModule({
  providers: [DatePipe]
})
export class AppModule {
}
// or component's providers:

@Component({
  selector: 'home',
  styleUrls: ['./home.component.css'],
  templateUrl: './home.component.html',
  providers: [DatePipe]
})
export class HomeComponent {
...
// 3) Inject it into your component's constructor like any other service:

constructor(private datePipe: DatePipe) {
}
// 4) Use it:

ngOnInit() {
    this.time = this.datePipe.transform(new Date());
}
Posted by: Guest on August-24-2021
0

Date format in angular ts

public getAllBookings() {
    // Get the dates
    const today =  new Date();
    const tomorrow =  new Date(today.setDate(today.getDate() + 1));

    return new Promise((resolve, reject) => {
      this.http
        .get(
          `${this.kioskservice.getAPIUrl()}search/dashboard/${this.kioskservice.LocationGUID()}/?apikey=${this.kioskservice.getAPIKey()}&format=json&from=${this.dateFormat(today)}&until=${this.dateFormat(tomorrow)}&full=true`
        )
        .toPromise()
        .then(
          res => {
            this.config = res.json()
            console.log(res.json());
            resolve();
          },
          msg => {
            throw new Error("Couldn't get all Bookings: " + msg);
          }
        );
    });
  }

  // Helper function to format if you don't use moment.js or something alike
  private dateFormat(date: Date) {
    const day = date && date.getDate() || -1;
    const dayWithZero = day.toString().length > 1 ? day : '0' + day;
    const month = date && date.getMonth() + 1 || -1;
    const monthWithZero = month.toString().length > 1 ? month : '0' + month;
    const year = date && date.getFullYear() || -1;

    return `${year}-${monthWithZero}-${dayWithZero}`;
  }
Posted by: Guest on May-11-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language