How To: Logging with Javascript using Date class

Hello,

If you are trying to print a neat date format in Javascript using Date() class, then you can use below snippet.

Its a ES6 based function with template literals.


  
  const date = () => {
  const d = new Date();
  return `${d.getFullYear()}-${d.getMonth()}-${d.getDate()}_${d.getHours()}-${d.getMinutes()}-${d.getSeconds()}_${d.getMilliseconds()} => `;
};

 
  

If you are looking for ES5 syntax, you can use below one

 
  var getDate = function() {
    var d = new Date();
    return d.getFullYear()+"-"+d.getMonth()+"-"+d.getDate()+"_"+d.getHours()+"-"+d.getMinutes()+"-"+d.getSeconds()+"_"+d.getMilliseconds()+" =>";
  };
  
  

Hope it helps.

Thank you.

0 comments:

Post a Comment