Angularjs获取当前时间

时间:2019-04-29 02:13:14

标签: javascript angularjs

我正在使用以下代码在angularJS中获取当前时间:

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

YYYY-MM-DD-Hours-Minutes-Seconds中当前时间的正确代码是什么?

2 个答案:

答案 0 :(得分:1)

也许您需要这个。

  $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;
    };

答案 1 :(得分: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);