1

I am trying to write a function to format and assign a past or future date on some date fields in my request. However, I got an error says assignedDate is not a function.

function changeDate(numberOfDays) {
var assignedDate = new Date().toISOString();
assignedDate.setDate(assignedDate.getDate() - numberOfDays);
assignedDate.substr(0, assignedDate.length-5);
return assignedDate; } 

Then I change it as I did at first place;

function changeDate(numberOfDays) {
var assignedDate = new Date().toISOString();
var assignedDate.setDate(assignedDate.getDate() - numberOfDays);
var assignedDate.substr(0, assignedDate.length-5);
return assignedDate; } 

Then it starts to show warning which says "assignedDate is already defined".

I am also a kind of newbie, please let me know your solutions.

Thanks.

Fatih_Ozgen
  • 11
  • 1
  • 2

3 Answers3

2

You need to take care with the types that you are creating:

var assignedDate = new Date().toISOString();

new Date creates a Date object, but toISOString returns a string, so assignedDate is a string. When you do:

assignedDate.setDate(assignedDate.getDate() - numberOfDays);

you'll get an error as strings don't have a setDate method. Just don't do toISOString.

Since assignedDate is now a Date, you can't do assignedDate.substr. If you want to format a date, see Where can I find documentation on formatting a date in JavaScript?

The following uses toISOString to get a string, but note that this will be the UTC date, not local so probably not what you want (since the date will be different for the duration of the host timezone offset).

function changeDate(numberOfDays) {
  // Start with a Date
  var assignedDate = new Date();
  // Adjust by the number of days
  assignedDate.setDate(assignedDate.getDate() - numberOfDays);
  // Get a formatted string (for UTC)
  assignedDate = assignedDate.toISOString();
  // Return the string
  return assignedDate.substr(0, assignedDate.length-5); 
}

console.log(changeDate(5));
RobG
  • 142,382
  • 31
  • 172
  • 209
0

Hear you are trying to subtract/add number of days to respective date.

you can directly subtract like

 var d = new Date();
 d.setDate(d.getDate()-5);

reference: Subtract days from a date in JavaScript

-1

You should use the interal library moment.

The import is done like this:

var moment = require('moment')

moment provides a very easy to use and readable API.

var fiveDaysAgo = moment().subtract(5, 'days');

You can lookup more supported libs in test scripts in Postman here: Postman PM API Reference

The documentation of moment JS can be found here: Moment JS Docs

Sergej Lopatkin
  • 1,071
  • 9
  • 11
  • This is actually not a bad idea... Moment is extremely powerful, and they do a lot of heavy lifting for you. The fact that it's a supported library should speak very highly to it's utility. edit: I accidentally hit the down-vote arrow and can't change it. I know this solution goes away from the OP's plain-javascript approach, but because it's supported by Postman, it should be an acceptable answer/solution. – Ed Meacham Dec 14 '17 at 20:03
  • 1
    Thanks for your comment @EdMeacham. I don't see a problem using moment JS since it is recommended in the Postman documentation. We used the plain old JS Date class in my team and it quickly became hard to read and maintain. – Sergej Lopatkin Dec 15 '17 at 09:50
  • 1
    For sure! I use Moment on A LOT of projects--client and server side. If you edit your response, I can fix my vote. – Ed Meacham Dec 15 '17 at 15:36
  • 1
    Love using momentjs in Postman. It’s awesome! As a alternative, another time based module called [Sugarjs](https://sugarjs.com/dates/) is also supported in the application. It basically does the same thing but it’s a useful thing to know. – Danny Dainton Dec 17 '17 at 09:43