Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MichaelGrossklos's avatar

Date Problem with AngularJS and Laravel

Hi,

I try to filtering a date that I fetch from laravel. But {{date | date:'shortDate'}} wont work. As I found out there is a problem between the Db date format and the format that angular expects. Somewhere I read from

Carbon\Carbon::setToStringFormat('c');

But I don't know how to use this properly.

0 likes
4 replies
MichaelGrossklos's avatar

Found another solution on angular side.

(function () {
    'use strict';

    angular
        .module('mcFilters', [])
        .filter('mcDbDateFormatter', function() {
            return function(dateSTR) {
                var o = dateSTR.replace(/-/g, "/"); // Replaces hyphens with slashes
                return Date.parse(o + " -0000"); // No TZ subtraction on this sample
            }
        });
}());

Created a filter that I use as follows:

date | mcDbDateFormatter | date: 'shortDate'

So I have the need to use this filter on every date in my ng templates. A laravel site solution would be much better.

bobbybouwmann's avatar

What do you want to do? What is the date format you have and what is the date format you want?

codehero's avatar

Hey... you could override the getDateDateFormat() to return 'U'.

willvincent's avatar

Rather than struggling with the built in date handling of javascript, I suggest adding moment.js and using it to do date formatting in angular. Ideally with custom directives, or if you don't want to write custom directives -- use moment along with these angular-moment directives.

Please or to participate in this conversation.