3

I am using Angular Material's md-dialog feature but having some issues getting the date filter working. Maybe someone can spot it and let me know if they have any idea on how to make this work. I can see that {{item.presentation.end_time}} and {{confSessionObj.session_nr}} are working but when I put the angular date filter, it doesn't recognize it.

Here's my code.

JS

$scope.showAdvanced = function(ev, confSession) {
    var sessionID = confSession.id;
    $.ajax({
        type: "GET",
        url: "/session-detail.php",
        data: {id: sessionID},
        success: function(data, response){
            data = data.replace(/\\n/g, "\\n")
                .replace(/\\'/g, "\\'")
                .replace(/\\"/g, '\\"')
                .replace(/\\&/g, "\\&")
                .replace(/\\r/g, "\\r")
                .replace(/\\t/g, "\\t")
                .replace(/\\b/g, "\\b")
                .replace(/\\f/g, "\\f");
            $scope.returnedObj = JSON.parse(data);

            $mdDialog.show({
                locals: { confSessionObj: confSession, returnedObj: $scope.returnedObj },
                controller: DialogController,
                targetEvent: ev,
                template:
                    '<div class="md-dialog-container">' +
                    '<md-dialog aria-label="Session Detail">' +
                    '<form ng-cloak>' +
                    '<md-toolbar md-scroll-shrink>'+
                    '<div class="md-toolbar-tools">'+
                    '<h4 style="color: #fff;">Session Details</h4>'+
                    '</div>'+
                    '</md-toolbar>'+
                    '<md-dialog-content>'+
                    '<md-list-item>'+
                    '<div class="md-dialog-content"  id="dialog">'+
                    '<p class="md-body-2"><span class="md-custom-title">Session Title:</span>  {{confSessionObj.session_nr}} - {{confSessionObj.session_name}}</p>'+
                    '<table class="table table-bordered table-striped table-hover table-responsive" id="dialogtable">'+
                    '<tr id="theader">'+
                    '<thead>'+
                    '<th>Talk Title</th>'+
                    '<th>Start Time</th>'+
                    '<th>End Time</th>'+
                    '<th>Speaker Name</th>'+
                    '</thead>'+
                    '</tr>'+
                    '<tr ng-repeat="item in returnedObj">'+
                    '<td>{{item.presentation.title}}</td>'+
                    '<td>{{item.presentation.start_time | date: "MM/dd/yyyy  h:mma"}}</td>'+
                    '<td>{{item.presentation.end_time | date: "MM/dd/yyyy h:mma"}}</td>'+
                    '<td>{{item.speakers.firstname}}&nbsp;&nbsp;{{item.speakers.lastname}}</td>'+
                    '</tr>'+
                    '</table>'+
                    '</div>'+
                    '</md-list-item>'+
                    '</md-dialog-content>'+
                    '<md-dialog-actions layout="row">'+    
                    '<md-button class="md-raised" ng-click="cancel()">Close</md-button>'    +
                    '</md-dialog-actions>'+
                    '</form>'+
                    '</md-dialog>'+
                    '</div>',
                parent: angular.element(document.body),
                preserveScope: true,
                clickOutsideToClose:true,
                fullscreen: $scope.customFullscreen
            });
        },
        error: function(e){
            console.log(e.message);
        }
    });
};
function DialogController($scope, $mdDialog, confSessionObj, returnedObj) {
    $scope.confSessionObj = confSessionObj;
    $scope.returnedObj = returnedObj;

    $scope.cancel = function() {
        $mdDialog.cancel();
    };

}
Splaktar
  • 5,506
  • 5
  • 43
  • 74
missgg
  • 93
  • 2
  • 12

1 Answers1

1

Based on the information you provided, the issue you are experiencing with date filter is the format of the date object/string is in the incorrect format as other filters are successfully functioning. From the angular date filter documentation:

Date to format either as Date object, milliseconds (string or number) or various ISO 8601 datetime string formats (e.g. yyyy-MM-ddTHH:mm:ss.sssZ and its shorter versions like yyyy-MM-ddTHH:mmZ, yyyy-MM-dd or yyyyMMddTHHmmssZ). If no timezone is specified in the string input, the time is considered to be in the local timezone.

You'll need to have the date in one of those formats to ensure it can be effectively processed. I'd recommend using milliseconds as the date filter can process it in either come in as a string or number, providing a bit more flexibility.

You could take that date being received and use Date.Parse() to get the milliseconds, which then can then be passed into interpolation with the date filter.

JS:

$scope.parseDate = function(date) {
    return Date.parse(date);
}

HTML:

<td>{{ parseDate(item.presentation.end_time) | date: "MM/dd/yyyy h:mma" }}</td>

Here is a plunker of the functionality working.

Hopefully that helps!

Update:

If you need to display hours/minutes in one interpolation while showing month/day/year in another spot, you would just need to change the date format string argument after still parsing the date string you are receiving from the server to a propery Date millisecond amount. The plunker linked in this answer has been updated with all possible variations for date formatting including the date format you specified in your comment as well as time in both military and 12 hour format.

<td>{{ parseDate('2017-05-24 13:10') | date: 'MM/dd/yyyy h:mma' }}</td> // 05/24/2017 1:10PM
<td>{{ parseDate('2017-05-24 13:10') | date: 'MM/dd/yyyy H:mma' }}</td> // 05/24/2017 13:10PM
<td>{{ parseDate('2017-05-24 13:10') | date: 'H:mma' }}</td> // 13:10PM
<td>{{ parseDate('2017-05-24 13:10') | date: 'HH:mma' }}</td> // 13:10PM
<td>{{ parseDate('2017-05-24 13:10') | date: 'h:mma' }}</td> // 1:10PM
<td>{{ parseDate('2017-05-24 13:10') | date: 'hh:mma' }}</td> // 01:10PM
<td>{{ parseDate('2017-05-24 13:10') | date: 'MM/dd/yyyy' }}</td> // 05/24/2017
Alexander Staroselsky
  • 37,209
  • 15
  • 79
  • 91
  • thank you for your answer. Your solution makes sense but the problem is that my date and time are in a different fields. So I have my date without the time and if I apply the ` | date: "MM-dd-yyyy` filter, it works but my {{item.presentation.start_time}} and {{item.presentation.end_time}} are showing up in a military time format like 13:10 without the date in the front. How do I convert just the time part without the date? I don't think I can do | date: " h:mma" if I am just converting the hours. – missgg 7 mins ago – missgg Feb 02 '17 at 16:57
  • You absolutely can just show the time without the date. I've update my answer and the associated plunker. I've provided seven (7) different formats for display date + time (in both standard and mililary) as well as just time and just date. – Alexander Staroselsky Feb 02 '17 at 17:19
  • Alex, it makes sense but I think my time format is a string that can't be converted into a right JS date format. I did some debugging and I am still scratching my head over this. If I do `console.log(item.presentation.start_time) and console.log(typeof item.presentation.start_time ); - I get 13:10 and string`. For `console.log(Date.parse(item.presentation.start_time)); and console out the typeof - I will get NaN and number`. If I do `console.log(JSON.stringify(item.presentation.start_time)); and typeof - it will give me "13:10" (with double quotes) and string `. – missgg Feb 03 '17 at 03:52
  • `Date.parse(item.presentation.start_time)` looks like a string b/c it gives me HH:MM format like 15:20 or 13:10 and when I do `typeof` it even says "string". Maybe it's a different type of string that I get from a php array which then got converted into a JSON array using `json_encode` from the php file so I am not sure why `Date.pase()` wouldn't work. Do you think it's `:` symbol btwn the hour and min that's breaking the format? – missgg Feb 03 '17 at 03:59
  • Is there any way you can return milliseconds or full UTC format date from the server? This may make it easier to parse. As long as you can get the date string to something that Date.parse() can effectively process, you'll be able to use the date filter in the template. – Alexander Staroselsky Feb 03 '17 at 04:07
  • Yeah, I will try a few things to get it work. I will let you know if I figure out a solution to my issue. Thanks so much for your help! Your suggestion would've def. worked in a normal string. – missgg Feb 06 '17 at 14:25