Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to get the month of a cell containing a value in this format (dd/mm/yyyy)

    var date = new Date(scadenziario.getRange(eRow,eCol).getValue());
    var month = date.getMonth();

I am not sure why, but it doesn't work. The cell cointaining the date display this in the sheet: 20/05/2021 I would like the month variable to be matching the month of the cell, in this case 5.

question from:https://stackoverflow.com/questions/65943513/google-sheets-macro-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
82 views
Welcome To Ask or Share your Answers For Others

1 Answer

The date in the cell is not a date object. You can verify that if you run ISDATE:

enter image description here

One way to solve your issue is to use getDisplayValue instead and slice the relevant part of the resulting string:

  const date = scadenziario.getRange(eRow,eCol).getDisplayValue();
  const month = date.slice(3,5).replace("0","");

Last but not least, you want to get 5 and not 05 so one way is to get 05 and remove the 0 part. If the date is 12 then you will get the full number 12.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...