Skip to content

Commit

Permalink
Fix Date Format (#841)
Browse files Browse the repository at this point in the history
  • Loading branch information
caleballdrin authored Aug 31, 2023
1 parent f376cc2 commit eb384da
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
13 changes: 10 additions & 3 deletions app/scripts/directives/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ angular
},
controller: function ($timeout, $scope) {
$scope.updateTimeStamp = function (timestamp) {
//For Graduation date question, set the day to 1. The API needs the day but that could change in the future.
timestamp = $scope.monthYearOnly
? moment(new Date(timestamp)).set('date', 1)
: timestamp;
$scope.$apply(function () {
let dateSaveFormat = $scope.monthYearOnly
? 'YYYY-MM-DD'
: 'YYYY-MM-DD HH:mm:ss';
$scope.localModel = moment(new Date(timestamp)).format(
'YYYY-MM-DD HH:mm:ss',
dateSaveFormat,
);
});
};
Expand All @@ -29,7 +36,7 @@ angular
: scope.localModel
? moment(new Date(scope.localModel)).format('MM/DD/YYYY hh:mm A')
: null;
let dateOptions = scope.monthYearOnly
scope.dateOptions = scope.monthYearOnly
? {
viewMode: 'years',
format: 'MMMM YYYY',
Expand All @@ -50,7 +57,7 @@ angular
defaultDate: initialDate,
};
datePickerElement
.datetimepicker(dateOptions)
.datetimepicker(scope.dateOptions)
.on('dp.change', function (ev) {
scope.updateTimeStamp(ev.date);
});
Expand Down
4 changes: 4 additions & 0 deletions app/views/components/answerDisplay.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@
- {{answer.amount |
localizedCurrency:conference.currency.currencyCode}}</span
>
<!--Remove slice when API can handle receiving YYYY-MM format-->
<span ng-switch-when="graduationDateQuestion">
{{answer.value.slice(0,-3)}}</span
>
</ng-switch>
36 changes: 36 additions & 0 deletions test/spec/directives/datepicker.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'angular-mocks';

describe('Directive: datepicker', function () {
beforeEach(angular.mock.module('confRegistrationWebApp'));

var element, scope, $compile, $rootScope;
beforeEach(inject(function (_$compile_, _$rootScope_, $templateCache) {
$compile = _$compile_;
$rootScope = _$rootScope_;

scope = $rootScope.$new();
$templateCache.put('views/components/graduationDateQuestion.html', '');
scope.monthYearOnly = true;
element = $compile(
'<crs-datetimepicker model="answer.value" month-year-only="true"></crs-datetimepicker>',
)(scope);
scope.$digest();
scope = element.isolateScope() || element.scope();
}));

it('Sets the date to the correct format based on the type of date question', function () {
scope.updateTimeStamp(new Date('02/05/1994'));

expect(scope.localModel).toBe('1994-02-01');

scope.monthYearOnly = false;

scope.updateTimeStamp(new Date('02/05/1994'));

expect(scope.localModel).toBe('1994-02-05 00:00:00');
});

it('Sets date options correctly based on type of date', function () {
expect(scope.dateOptions.viewMode).toBe('years');
});
});

0 comments on commit eb384da

Please sign in to comment.