SURYA VISWANATHAN SURYA VISWANATHAN - 2 months ago 6
HTML Question

Display today's date along with a string

I want to display today's date along with a string. Eg., I want to display R_20160310 in a field. I used the following code, I'm not able to get the date format in YYYYMMDD when I store a string value in scope. Below are the codes in my html and controller.

html:

<tr>
<th><label>Review Title/Purpose*</label></th>
<td><input type="text" class="col-md-10" ng-model="today"></td>
</tr>


Controller:

$scope.today= 'Review__' + new Date();


Right now I'm getting as, "Review__Mon Oct 03 2016 10:47:47 GMT+0800 (Malay Peninsula Standard Time)" but I need to display "Review_20160310". Any suggestions?

Answer

Try this.

function x() {
  var y = new Date();
  return 'Review_' + ((y.getFullYear() + ' ' + (y.getMonth() + 1) + ' ' + y.getDate()).replace(/ /g, ''));
}

console.log(x());

Comments