0

I have the following code:

function Ctrl($scope)
{
    $scope.dt = new Date('2012-07-16T00:00:00');
    var format = 
    {
    	day: '2-digit',
      month: '2-digit',
      year: 'numeric'
    };
    $scope.dateTimeFormatter = Intl.DateTimeFormat('es-es', format);
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="Ctrl">
In: {{dt}} <br/> 
Angular: {{dt | date:'yyyy-MM-dd HH:mm:ss Z'}}
  <div style="background-color:lightgreen;">
    This works: {{dateTimeFormatter.format(dt)}}
  </div>
  <div style="background-color:rgb(251, 123, 89);">
    This doesn't: {{dateTimeFormatter.format(Date.parse('2012-07-16T00:00:00'))}}
  </div>
</div>

Does anybody knows what I'm doing wrong?

Or why the green line works as expected, but the red one doesn't?

2 Answers 2

3

It tries to find Date in scope ($scope.Date), and ofc found nothing.

Add

$scope.Date = Date;

and it'll work:

function Ctrl($scope)
{
    $scope.dt = new Date('2012-07-16T00:00:00');
    var format = 
    {
    	day: '2-digit',
      month: '2-digit',
      year: 'numeric'
    };
    $scope.dateTimeFormatter = Intl.DateTimeFormat('es-es', format);
    $scope.Date = Date;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="Ctrl">
In: {{dt}} <br/> 
Angular: {{dt | date:'yyyy-MM-dd HH:mm:ss Z'}}
  <div style="background-color:lightgreen;">
    This works: {{dateTimeFormatter.format(dt)}}
  </div>
  <div style="background-color:rgb(251, 123, 89);">
    This doesn't?: {{dateTimeFormatter.format(Date.parse('2012-07-16T00:00:00'))}}
  </div>
</div>

Sign up to request clarification or add additional context in comments.

11 Comments

Why my code then shows the current date? How I don't even get an error trying to find Date.parse method?
If Angular cannot evaluate expression, it replaces it with empty string. Without any errors. So, {{dateTimeFormatter.format(Date.parse('2012-07-16T00:00:00'))}} rendering in {{dateTimeFormatter.format()}}, which returns current date.
{{dateTimeFormatter.format('')}} returns "01/01/1970" but {{dateTimeFormatter.format()}} returns current date; does it returns an empty string or just undefined?
Sorry, it returns undefined, not empty string. dateTimeFormatter.format(undefined) returns current date, checked in console.
@DavidDiez, You already mentioned that above, Angular is "forgiving": expression evaluation is forgiving to undefined and null and not throws errors.
|
1

Your dateTimeFormatter.format expects a Date object - and you call it with number, since "The Date.parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC" (mdn).

2 Comments

var dateTimeFormatter = Intl.DateTimeFormat('es-es', { day: '2-digit', month: '2-digit', year: 'numeric' }); dateTimeFormatter.format(Date.parse('2012-07-16T00:00:00')); // returns on Firefox console "16/07/2016", why??
Date.parse returns number, not a Date object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.