1

I whould like to get 2 variables. 1 with the current date (yyyy-MM-DD), and 1 with the date of tomorrow (yyyy-MM-DD).

I need to place these 2 variables in my HTTP request. Can someone show me how I can make the 2 variables and use them in my HTTP request. The current code behind is this:

export class BookingService {

    private config: Object;
    public today = new Date();
    public tomorrow = new Date();
    public domainSettings: Object = {};

    constructor(
        private http: Http,
        private kioskservice: KioskService
    ) { }

    public getAllBookings() {
        return new Promise((resolve, reject) => {
            this.http
                .get(
                    `${this.kioskservice.getAPIUrl()}search/dashboard/${this.kioskservice.LocationGUID()}/?apikey=${this.kioskservice.getAPIKey()}&format=json&from=2018-04-17&until=2018-04-18&full=true`
                )
                .toPromise()
                .then(
                    res => {
                        this.config = res.json()
                        console.log(res.json());
                        resolve();
                    },
                    msg => {
                        throw new Error("Couldn't get all Bookings: " + msg);
                    }
                );
        });
    }
}
  • 1
    Both of your questions are very basic, please use google or search on SO before. Date: https://stackoverflow.com/questions/30397156/how-to-get-todays-tomorrows-and-yesterdays-dates-on-javascript?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa Promise example: https://codecraft.tv/courses/angular/http/http-with-promises/ or the official: https://angular.io/tutorial/toh-pt6 – Doomenik Apr 18 '18 at 12:02
  • Thanks @Doomenik –  Apr 18 '18 at 12:28

1 Answers1

4

The dates you can obtain by simply playing around with JavaScript's Date interface.

To use them into the HTTP call is just add them using template strings.

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}`;
  }

Using something like moment.js can be really useful if you're going to work a lot with dates, timestamps, etc.

SrAxi
  • 19,787
  • 11
  • 46
  • 65
  • Thanks it works! Could you also make one for the date of yesterday? –  Apr 18 '18 at 12:25
  • Hey, no problem. But the 'yesterday' date was already made. See `const tomorrow = new Date(today.setDate(today.getDate() + 1));`. Cheers mate! ;) – SrAxi Apr 18 '18 at 12:35
  • oh 1 more question tho. I just heard that I need to make it adapteble so that some people can set it up with 3 hours difference, instead of 1 full day. Do you know how to make something like that? –  Apr 18 '18 at 12:45
  • 1
    @MexLataster Then you'll need to work with timestamps. Anyway, check the Date API here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date. Adapt the code to also take into account the hours. If you cannot manage, create a new question. But first you'll have to show some written code, if you don't try stuff people will downvote you. ;) – SrAxi Apr 18 '18 at 12:55
  • 1
    Thanks for your help I will have a look at it! –  Apr 18 '18 at 12:57