Change Date.now() to readable date format (eg: sep 8,2018 or 2 hrs ago)


Change Date.now() to readable date format (eg: sep 8,2018)

var dateInDateNowFormat = date.now();
moment(dateInDateNowFormat).format('MMM DD, YYYY');


Change date To sec , Minutes, hrs , weeks and months. eg(3 min ago, 2 weeks ago)


    calculate_time_difference(a){
    var dt = new Date(a);
   var millis =    new Date().getTime() - dt.getTime() ;
      var minutes = Math.floor(millis / 60000);
  var seconds = ((millis % 60000) / 1000).toFixed(0);

   var hours = (millis / (1000 * 60 * 60)).toFixed(1);

 var days = (millis / (1000 * 60 * 60 * 24)).toFixed(1);

   if(minutes<1 && seconds<10){
    return 'now';
  }else if(minutes <1 && seconds<59 ){
    return seconds + 's';
   } else if(minutes>= 1 && minutes<=59) {
    return minutes + 'm';
  }else if(minutes>=60 && hours<24){
        if(Math.floor(hours)==1 || minutes==60){
        return Math.floor(hours) + 'h';
        }else{
        return Math.floor(hours) + 'h';
        }
  }else if(hours>24){
    if(Math.floor(days) == 1){
    return Math.floor(days) +"d";
    }else{
    return Math.floor(days) +"d";
    }
  }
  else{ 
        return minutes + ":" + (seconds < 10 ? '0' : '') + seconds  + "  [" + a.toString('YYYY-MM-dd').slice(0,25) + "] ";
      }
  },

Comments

Popular posts from this blog

Setting up Orthanc server on Ubuntu 16

How to install COMODO SSL Certificates for NGINX

HTML