1

I am getting Nan error when I am using parseInt with date strings. I am getting finalRelease and Original Release value in form of string. I am splitting the value and try to access using index. I am getting correct month in case of finalMonth but it is showing Nan in case of originalMonth.

finalRelease = "2017-07-20"
originalRelease = "2017-08-09"
if(finalRelease!=null && originalRelease!=null)  {
      var finalDate = finalRelease.split('-');
      var originalDate = originalRelease.split('-');
      var finalMonth = parseInt(finalDate[1])-1;
      var originalMonth = parseInt(originalDate[1])-1;
Saurabh Garg
  • 199
  • 7
  • 22

2 Answers2

3

Indeed, parseInt("08") returns NaN in Google Apps Script. This is because GAS, as old versions of JavaScript, consider the leading 0 as a sign of octal integer. The remedy is simple:

Always include the radix (usually 10) with parseInt.

Use:

parseInt("08", 10): 
parseInt(originalDate[1], 10) - 1;

Do not use:

parseInt("08"): 
parseInt(originalDate[1]) - 1;
0

Here is another shorter method using Number constructor:

var num = Number("08"); // 8.0
Kos
  • 4,890
  • 9
  • 38
  • 42