ddsbro
ddsbro

Reputation: 217

DateTime not working in angular.js

I've got this line of code

<h3> <script>Date.now</script>{{$flow.files[0].name}} </h3>

Basically All i'm wanting to do is put the date infront of the filename thats generated from the {{}}.

It currently shows nothing, The unix timestamp might be better but i can only get it to show text that i type infront.

I have tried {{Date.now(); $flow.files[0].name}} Which hasn't worked.

Upvotes: 0

Views: 314

Answers (2)

manzapanza
manzapanza

Reputation: 6205

Try something like this:

Controller:

$scope.myDate = new Date();

HTML:

{{myDate | date: 'MMM d, y h:mm:ss a' }} {{$flow.files[0].name}}

I used the filter date to format the date.

Upvotes: 0

Yar
Yar

Reputation: 7447

If you want to do it with JavaScript like your first attempt, you should have something like this:

<div ng-app="app">
    <span id="dateHere"></span> - {{fileName}}
    <script>
    document.getElementById("dateHere").innerHTML = Date();
    </script>
</div>

and if you want to have more control on your date format:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js"></script>
<div ng-app='app' ng-controller='Ctrl'>
        <span id="dateHere"></span> - {{fileName}}
        <script>
        document.getElementById("dateHere").innerHTML = Date();
        var date = new Date();
        var currentYear = date.getFullYear();
        var currentMonth = date.getMonth();
        var currentDate = date.getDate();
        var customDate = currentMonth + '-' + currentDate + '-' + currentYear;
        document.getElementById("dateHere").innerHTML = customDate;
        </script>
</div>

<script>
  angular.module('app', []).controller('Ctrl', function($scope){
    $scope.fileName = 'your file name';
  })
</script>

-> on Plunker

Upvotes: 1

Related Questions