0

I am using the below code to get the current time in angularJS:

    $scope.getDatetime = function() {
        return (new Date()) + "abc.txt" ;
    };

What is the correct code to the current time in YYYY-MM-DD-Hours-Minutes-Seconds?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
meallhour
  • 13,921
  • 21
  • 60
  • 117

2 Answers2

1

Maybe you need this.

  $scope.getDatetime = function() {
        var date = new Date();
        var day =  date.getDate();
        var month = ((date.getMonth() + 1) > 9) ? date.getMonth() + 1 :  "0" + (date.getMonth() + 1)
        var year = date.getFullYear();
        var hours = date.getHours();
        var minutes = date.getMinutes();
        var seconds =  date.getSeconds();
        return year+"/"+month+"/"+day+" "+hours+":"+minutes+":"+seconds;
    };
0

var s = new Date().toISOString();
console.log(s);
s = s.slice(0,19);
s = s.replace('T','-');
s = s.replace(':','-');
s = s.replace(':','-');
console.log(s);
georgeawg
  • 48,608
  • 13
  • 72
  • 95