Date Before / After using Moment

Hi All,

Can someone help me out with what i am sure is going to be some obvious code?

I am trying to use ‘moment’ in a postman test to do the following logic

   const currentDate= require('moment');
   currentDate().format("DD-MMM-YYYY");

   If currentDate == 25th of month > {Need code here} 
          currentDate().add(8,'days').format("DD-MMM-YYYY");
   else
          currentDate().add(3,'days').format("DD-MMM-YYYY");
   endIf

How can I assess if the date returned is greater or equal to the 25th? is there a way to check the first 2 numeric in the date returned?

I was thinking of hardcoding it to something like:

const currentMonth = require('moment');
monthNow = currentMonth().format("MM"));
const currentYear = require('moment');
yearNow = currentYear().format("YYYY"));

then do some kind of compare on:

datecheck = "25/" + monthNow + yearNow

Any help is appreciated

1 Like

I can see Before or Same, but unsure how to chain:

https://momentjs.com/docs/#/query/is-before/

const moment= require('moment');
currentDate = moment()
console.log(currentDate.date())

this will print the date

Thanks @praveendvd, I am aware of this. T

he real issue is how I evaluate if the current date is the 25th of the month or later e.g. 26th 27th and so on.

if the date is between 1st - 24th I want to do x
if it is 25th - 31st (depending on month) then I want it to do y

console.log(1<currentDate.date()<25)

This prints true if its between 1 and 25

you get month using currentDate.month() , 0 means jan and 11 means dec (0-11)

https://momentjs.com/docs/

Hey @praveendvd here is a POC that can be copied and pasted:

currently on run the error returned is “POC | ReferenceError: If is not defined”

pm.test("POC", function () {
const moment= require('moment');
currentDate = moment()

   If (1<currentDate.date() <25||25) == true; {
          currentDate().add(8,'days').format("DD-MMM-YYYY");
          console.log(currentDate);
   } elseif (1<currentDate.date() <25||25) == false; {
          currentDate().add(3,'days').format("DD-MMM-YYYY");
          console.log(currentDate)}
   endIf
});
pm.test("POC", function () {

    const moment = require('moment');

    currentDate = moment()

    if (1 < currentDate.date <= 25) {

        console.log(currentDate.add(8, 'days').format("DD-MMM-YYYY"));

    }

    else {

        console.log(currentDate.add(3, 'days').format("DD-MMM-YYYY"))

    }

});

hey @praveendvd I’ve just put this to the test:

pm.test(“POC”, function () {

const moment = require('moment');
currentDate = moment()

if (1 < currentDate.date <= 17) {
    console.log(currentDate.add(2, 'days').format("DD-MMM-YYYY"));
}
else {
    console.log(currentDate.add(10, 'days').format("DD-MMM-YYYY"))
}

});

today is the 19th, but it will add 2 days, rather than add 10 days :S

This kind of works, I just need to construct the correct date time format as Id need to pass it in somehow rather than 16-01-2021

pm.test("POC", function () {

    const moment = require('moment');
    currentDate = moment()

    if (moment(16-01-2021).isBefore(17-01-2021)) {
        console.log(currentDate.add(2, 'days').format("DD-MMM-YYYY"));
    }
    else {
        console.log(currentDate.add(10, 'days').format("DD-MMM-YYYY"))
    }
});

pm.test(“POC”, function () {

const moment = require('moment');
currentDate = moment()
day = Number(21);

passedDate = day+01-2021;
result = (moment(passedDate).isBefore(17-01-2021));

if (result == true) {
    console.log("beforeDate")
    console.log(currentDate.add(2, 'days').format("DD-MMM-YYYY"));
}
else {
    console.log("afterDate")
    console.log(currentDate.add(10, 'days').format("DD-MMM-YYYY"))
}

});