Unable to format default MySQL datetime

Viewed 21936

My dates come out of the database looking like this: 2013-11-21 17:43:20

I'm trying to user Angular's date filter to turn them into something prettier, but...

{{Objected.created | date:'shortDate'}}

or

{{Objected.created | date:'YYYY'}}

...just spits out the original datetime string: 2013-11-21 17:43:20. There are no errors. What am I doing wrong?

Update I see that MySQL's default datetime is incompatible with what Angular's data filter expects. I'm attempting to convert it on the fly like this but it's throwing errors:

<li ng-repeat="result in data">{{ new Date(result.Job.created).toISOString() | date:'shortDate'}}</li>

I suspect I can't instantiate the Date class in the way I'm trying. The error is a $parse:syntax error.

Update

Thanks to @m59's help, I got it working with a few minor adjustments...

HTML:

<html ng-app="myApp">
...
{{Object.created | dateToISO | date:'shortDate'}}

JS:

var myApp = angular.module('myApp',[]);

myApp.filter('dateToISO', function() {
  return function(input) {
    input = new Date(input).toISOString();
    return input;
  };
});

This custom filter converts the default MySQL datetime into the format that the date filter expects, so I send it throw one then another and "voila".

5 Answers

Angular 2 Pipe

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'toIso'
})
export class ToIsoPipe implements PipeTransform {
  transform(value: string): unknown {
    return value.replace(' ', 'T');
  }
}

// INPUT : "2020-08-20 09:00:20"
// OUTPUT : "2020-08-20T09:00:20"

Example

{{response.created | toIso | date:'MMM d, h:mm a'}}
Related