-1

I have a WebService where I have to modify DATEFIN by today's date and DATEDEBUT by a date 1 year before.

Currently, I have this below

illustration

Here my dates are entered "manually", how I could create this correctly, please?

getNews(SVM, last) {
    var payload = {
        "HEADER": this.sh.getHeaderForRequest(),
        "SVM": SVM,
        "PERIODE": {
            "DATEDEBUT": "2018-01-01",
            "DATEFIN": "2021-01-01"
        },
        "LASTX": 0
    }
    return this.http.post < any[] > (this.getBaseUrl() + `/WLOLARTL`, payload);
}

Here is my method getNews()

getNews() {
    return this.api.getNews(this.svm, 20)
        .pipe(
            map((response: {}) => {
                // this.getDetails();
                this.prepareData(response);
            })
        );
}

Thank you so much.

2 Answers2

1

Somethig like that:

var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();

previous year:

var previousYear = today.getFullYear() - 1 +'-'+(today.getMonth()+1)+'-'+today.getDate();
Cristian18
  • 220
  • 3
  • 12
  • @Christian18, Thank you. Is it possible to add a zero on the day? I have this `2021-10-6` instead of `2021-10-06`. –  Oct 06 '21 at 11:58
  • Yes, a simple way will be `("0" + today.getDate()).slice(-2)`. You add "0" and the day "6". And you extract the last two characters "06". If you have a day "10", it will be "010", but you extract the last two "10" – Cristian18 Oct 06 '21 at 12:14
0

You can do it like this if i understood the question correctly.

const date = new Date();
const year =  date.getFullYear()
const month = date.getMonth()
const day =  date.getDate()
var payload = {
        "HEADER": this.sh.getHeaderForRequest(),
        "SVM": SVM,
        "PERIODE": {
            "DATEDEBUT": (year-1)+"-"+month+"-"+day ,
            "DATEFIN": year+"-"+month+"-"+day
        },
        "LASTX": 0
    }
Singh
  • 783
  • 1
  • 6
  • 24