AngularJs filter sql timestamp "yyyy-MM-dd HH:mm:ss" to something else


I've tried the angular docs of {{someDate | date:'params'}}, however; This doesn't work if someDate is in this format yyyy-MM-dd HH:mm:ss.

How would I convert yyyy-MM-dd HH:mm:ss to say just yyyy using angularjs.

Problem courtesy of: Carlos Pliego


The date filter is designed to handle multiple input types including date objects and common date strings. If your date string is not recognized by the provided date filter, I would just write my own filter. It would look something like this...

myapp.filter('toYear', function () {
     return function (dateString) {
         var dateObject = new Date(dateString);
         return dateObject.getFullYear();

You can use it like this...

{{someDate | toYear}}
Solution courtesy of: Charlie Martin


Angular js date object doesn't support to convert from MySql date format to timestamp directly. So need to convert it to acceptable format from YYYY-mm-dd H:i:s to YYYY/mm/dd H:i:s and then convert it to a timestamp.

You may try the below snippet to convert Mysql date format YYYY-mm-dd H:i:s to timestamp using AngularJs filter

app.filter("mysqlDateFormatToTimestamp", function(){
    return function(date){
        var date1 =  date2 = date3 = timestamp = hours = minutes = seconds = '';               
        date1 = date.split(':'); 
        date2 = date1[0].split(' '); 
        date3 = date2[0].split('-'); // Change it based on your format
        if( date1.length == 1 && date2.length == 1 ){
            hours = '00';
            minutes = '00';
            seconds = '00';
            hours = parseInt(date2[1]);
            minutes = parseInt(date1[1]);
            seconds = parseInt(date1[2]);
        timestamp = new Date(parseInt(date3[0]), parseInt(date3[1])-1, parseInt(date3[2]), hours, minutes, seconds);
        return timestamp;

Now, You may use this filter wherever the date format you want, and you may change the params as you like.

   {{ '2016-07-07 05:27:30' | mysqlDateFormatToTimestamp | date:'LLLL dd, yyyy HH:mm:ss'}} 
   // results would comes like "July 07, 2016 05:27:30"

   {{ '2016-07-07 05:27:30' | mysqlDateFormatToTimestamp | date:'yyyy'}} 
   // results would comes like "2016"
Discussion courtesy of: Arul

You can define a custom format as follows

Date.prototype.customFormat = function(){
    return this.getMonth() + 
    "/" +  this.getDate() +
    "/" +  this.getFullYear();


> var d = new Date()
Fri Mar 14 2014 15:39:07 GMT+0000 (GMT Standard Time)
> d.customFormat()

More details

Discussion courtesy of: dopplesoldner

I actually fixed the solution here:

app.filter('timestampToISO', function() {
    return function(input) {
        input = new Date(input).toISOString();
        return input;


To implement you add this to the view:

{{someSQLTIMESTAMP | timestampToISO | date}}
Discussion courtesy of: Carlos Pliego

This recipe can be found in it's original form on Stack Over Flow.