0

i have a response from frontend as below:

const response = {  Lakban: "Merah",  Plant: "K103",  Kategori: "M2",  Jumlah: 103 }

and i have lookup Value from MongoDB with example result as below:


 const monthReturnDB = "November"

 const shelfLifeDB = 6

and I have function below to get month number:

 const getMonth=(monthStr)=>{

    return new Date(monthStr+'-1-01').getMonth()+1

}

so what i try to explain is i try to get the format of date based on the monthReturnDB and the "Kategori" type with function below:

let value;

if(response.Kategori ==="M1"){

  value = "1" + "/" getMonth(monthReturnDB)+Dates.getFullYear()

}else{

  value = "1" + "/" getMonth(monthReturnDB)+(Dates.getFullYear()-1)

}

i expected it to sum the value from the months and year with the shelfLifeDB number, with function below:

let end = new Date(valueDB);

end.setMonth(end.getMonth() + 11);

let curr = new Date()

console.log(curr)

console.log(end.toLocaleString('en-US'),valueDB)

but its not returning the expected result as i want:

the expected result i want is as pic below: enter image description here

Any explanation where i did wrong here?

shrys
  • 5,860
  • 2
  • 21
  • 36
JS24
  • 1
  • 2
  • 14
  • could you please provide a runnable example showing the error/misbehaviour you experience? https://stackoverflow.blog/2014/09/16/introducing-runnable-javascript-css-and-html-code-snippets/ generally speaking, the adding months logic (`end.setMonth(end.getMonth() + 11);`) works, see: https://jsbin.com/nawecoziki/edit?js,console,output – IDMatt Jan 24 '23 at 08:52
  • Is `value` the same as `valueDB`? – FiddlingAway Jan 24 '23 at 23:06
  • Re `new Date(monthStr+'-1-01')`, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results). If you want to convert a month name to number, just create an array of month names and use [*Array.prototype.indexOf*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf). – RobG Jan 25 '23 at 05:43

0 Answers0