Skip to main content
improved formatting
Source Link
palacsint
  • 29.9k
  • 9
  • 81
  • 156

Need tips over improving this javascript code Age in days, months and years

var AgeConvertor = {

    Age: function (formattedDate) {
      var now = new Date();
      var yearNow = now.getFullYear();
      var monthNow = now.getMonth() + 1;
      var dayNow = now.getDate();

      // Calculating in days
      var ONE_DAY = 1000 * 60 * 60 * 24;
      var ONE_MONTH = 1000 * 60 * 60 * 24 * 30;
      var date1_ms = new Date().getTime()
      var date2_ms = formattedDate.getTime()
      var difference_ms = Math.abs(date1_ms - date2_ms)
      var yearAge =  Math.round(difference_ms/ONE_DAY);

      if(yearAge < 30) {
            return yearAge =  Math.round(difference_ms/ONE_DAY) + 'D';
      // This condition checks if the number of days is more than 30
      } else if(yearAge > 30 && yearAge < 365){
            return yearAge =  Math.round(difference_ms/ONE_MONTH) + 'M';
      } else {

      // Calculating in years
      var today = new Date(yearNow,monthNow,dayNow);

      if (yearNow < 100) {
        yearNow=yearNow+1900;
      }

      yearAge = yearNow - formattedDate.getFullYear();

      if (monthNow <= formattedDate.getMonth()) {
        if (monthNow < formattedDate.getMonth()) {
          yearAge--;    
        } else {
          if (dayNow < formattedDate.getDay()) {
            yearAge--;
          }
        }
      }
      return yearAge + 'Y';
     }
   }

}
  1. How well can this code be improved?
  2. Is there any fault in this code?
  3. Does this code give desired results?

This code returns your age in days,months months and years.

Need tips over improving this javascript code

var AgeConvertor = {

    Age: function (formattedDate) {
      var now = new Date();
      var yearNow = now.getFullYear();
      var monthNow = now.getMonth() + 1;
      var dayNow = now.getDate();

      // Calculating in days
      var ONE_DAY = 1000 * 60 * 60 * 24;
      var ONE_MONTH = 1000 * 60 * 60 * 24 * 30;
      var date1_ms = new Date().getTime()
      var date2_ms = formattedDate.getTime()
      var difference_ms = Math.abs(date1_ms - date2_ms)
      var yearAge =  Math.round(difference_ms/ONE_DAY);

      if(yearAge < 30) {
            return yearAge =  Math.round(difference_ms/ONE_DAY) + 'D';
      // This condition checks if the number of days is more than 30
      } else if(yearAge > 30 && yearAge < 365){
            return yearAge =  Math.round(difference_ms/ONE_MONTH) + 'M';
      } else {

      // Calculating in years
      var today = new Date(yearNow,monthNow,dayNow);

      if (yearNow < 100) {
        yearNow=yearNow+1900;
      }

      yearAge = yearNow - formattedDate.getFullYear();

      if (monthNow <= formattedDate.getMonth()) {
        if (monthNow < formattedDate.getMonth()) {
          yearAge--;    
        } else {
          if (dayNow < formattedDate.getDay()) {
            yearAge--;
          }
        }
      }
      return yearAge + 'Y';
     }
   }

}
  1. How well can this code be improved?
  2. Is there any fault in this code?
  3. Does this code give desired results?

This code returns your age in days,months and years.

Age in days, months and years

var AgeConvertor = {

    Age: function (formattedDate) {
      var now = new Date();
      var yearNow = now.getFullYear();
      var monthNow = now.getMonth() + 1;
      var dayNow = now.getDate();

      // Calculating in days
      var ONE_DAY = 1000 * 60 * 60 * 24;
      var ONE_MONTH = 1000 * 60 * 60 * 24 * 30;
      var date1_ms = new Date().getTime()
      var date2_ms = formattedDate.getTime()
      var difference_ms = Math.abs(date1_ms - date2_ms)
      var yearAge =  Math.round(difference_ms/ONE_DAY);

      if(yearAge < 30) {
            return yearAge =  Math.round(difference_ms/ONE_DAY) + 'D';
      // This condition checks if the number of days is more than 30
      } else if(yearAge > 30 && yearAge < 365){
            return yearAge =  Math.round(difference_ms/ONE_MONTH) + 'M';
      } else {

      // Calculating in years
      var today = new Date(yearNow,monthNow,dayNow);

      if (yearNow < 100) {
        yearNow=yearNow+1900;
      }

      yearAge = yearNow - formattedDate.getFullYear();

      if (monthNow <= formattedDate.getMonth()) {
        if (monthNow < formattedDate.getMonth()) {
          yearAge--;    
        } else {
          if (dayNow < formattedDate.getDay()) {
            yearAge--;
          }
        }
      }
      return yearAge + 'Y';
     }
   }

}
  1. How well can this code be improved?
  2. Is there any fault in this code?
  3. Does this code give desired results?

This code returns your age in days, months and years.

Tweeted twitter.com/#!/StackCodeReview/status/85320445552885760
Source Link

Need tips over improving this javascript code

var AgeConvertor = {

    Age: function (formattedDate) {
      var now = new Date();
      var yearNow = now.getFullYear();
      var monthNow = now.getMonth() + 1;
      var dayNow = now.getDate();

      // Calculating in days
      var ONE_DAY = 1000 * 60 * 60 * 24;
      var ONE_MONTH = 1000 * 60 * 60 * 24 * 30;
      var date1_ms = new Date().getTime()
      var date2_ms = formattedDate.getTime()
      var difference_ms = Math.abs(date1_ms - date2_ms)
      var yearAge =  Math.round(difference_ms/ONE_DAY);

      if(yearAge < 30) {
            return yearAge =  Math.round(difference_ms/ONE_DAY) + 'D';
      // This condition checks if the number of days is more than 30
      } else if(yearAge > 30 && yearAge < 365){
            return yearAge =  Math.round(difference_ms/ONE_MONTH) + 'M';
      } else {

      // Calculating in years
      var today = new Date(yearNow,monthNow,dayNow);

      if (yearNow < 100) {
        yearNow=yearNow+1900;
      }

      yearAge = yearNow - formattedDate.getFullYear();

      if (monthNow <= formattedDate.getMonth()) {
        if (monthNow < formattedDate.getMonth()) {
          yearAge--;    
        } else {
          if (dayNow < formattedDate.getDay()) {
            yearAge--;
          }
        }
      }
      return yearAge + 'Y';
     }
   }

}
  1. How well can this code be improved?
  2. Is there any fault in this code?
  3. Does this code give desired results?

This code returns your age in days,months and years.