get.Year with google apps script

Viewed 2620

I've got this very simple code in Google apps script:

var today = new Date();
var date = today.getDate();
var month = today.getMonth() + 1;
var year = today.getYear();
var newEstimate = month + '/' + date + '/' + year;

For some reason, the 'year' is outputted as '121'. Any ideas?

2 Answers

Recommendation:

You can try using getFullYear() method

  var today = new Date();
  var date = today.getDate();
  var month = today.getMonth() + 1;
  var year = today.getFullYear();// use getFullYear() method
  var newEstimate = month + '/' + date + '/' + year;

  Logger.log(newEstimate);

Here's the result:

enter image description here

Additional info:

The getYear() method returns the current year minus 1900. Thus, it's the reason why you're getting 121 result for 'year'.

I think this is a recent change by Google. I was using the same function for years and only in the last few weeks did the result returned 121 for the year. It's always been the "FullYear" prior.

Related