Yaroslav
Yaroslav

Reputation: 674

how to pass properly to DatePicker string representation of Date in format M/d/yy

I am new at angular and I'm trying to solve the next issue: I have this code

<div class="col-xs-6">
                    <label for="from">Date from*</label>
                    <div ng-controller="DatePickerCtrl" class="form-group"
                         ng-class="{'has-error': formErrors.dateFrom}">
                        <p class="input-group">
                            <input type="text" class="form-control" id="from" name="from"
                                   placeholder="{{dateOptions.format}}"
                                   datepicker-options="dateOptions"
                                   datepicker-popup="{{dateOptions.format}}"
                                   datepicker-append-to-body="true"
                                   on-open-focus="false"
                                   ng-model="formData.dateFrom"
                                   ng-change="formErrors.dateFrom = isDateAfter(formData.dateFrom, formData.dateTo)"
                                   required
                                   is-open="opened" close-text="Close" aria-describedby="from"
                            />
                            <span class="input-group-btn">
                    <button class="btn btn-default" ng-click="toggle($event)">
                        <i class="glyphicon glyphicon-calendar"></i>
                    </button>
                </span>
                        </p>
                        <div class="help-block" ng-class="{'has-error': formErrors.dateFrom.length}"
                             ng-bind="formErrors.dateFrom"></div>
                    </div>
                </div>

enter image description here

I need the next functionality - when I push the button "Generate" I need to pass to datapicker a new Date(). So my report should create some reports within today (current date). It's like some automatization, instead of choosing the date manually I just want to click the button "Generate" and current date pass to datapicker and displays on the screen. Suppose the current date is 3/11/2020. Then I should have result on the screen 3/11/20. But I have always minus 1 day... enter image description here

My data_picker:

app.controller('DatePickerCtrl', ['$scope', '$filter', '$log',
function($scope, $filter, $log) {

    init();

    function init() {
        $scope.opened = false;

        $scope.toggle = function($event) {
            $event.preventDefault();
            $event.stopPropagation();
            $scope.opened = !$scope.opened;
        };

        $scope.dateOptions = {
            format: 'M/d/yy',
            startingDay: 1
        };
    }

    $scope.isDateAfter = function(minDate, selectedDate) {
        if (minDate && selectedDate) {
            var fromDate = new Date(minDate);
            var toDate = new Date(selectedDate);
            if (dateAfter(fromDate, toDate)) {
                return 'Invalid range. Date from is after date to';
            }
        }
        return '';
    };

    function dateAfter(from, to) {
        return from.getTime() > to.getTime();
    }

}
]);

This is how I create the Date for datepicker:

 function getDate() {
        let today = new Date;
        let mm = today.getUTCMonth() + 1;
        let dd = today.getUTCDate();
        let yy = today.getUTCFullYear().toString().slice(2, 4);
        return mm + '/' + dd + '/' + yy;
    }

The main problem is that the datepicker takes one day away from current day that I have passed to it! I just hardcode the date to "3/11/20" and instead I see on the screen the date "3/10/20". Cannot solve this issue(( Maybe someone can help me with it? Thanks in advance)

SOLVED: datepicker works correctly only when I pass to it full format date like -> new Date();

Upvotes: 0

Views: 84

Answers (1)

Arunkumar Ramasamy
Arunkumar Ramasamy

Reputation: 771

You can use default DatePipe in Angular.

https://angular.io/api/common/DatePipe

or you can assign your date to one variable

`today = new Date()`

`<p>Today date is {{today | date: 'MM/dd/yy'}}</p>`

Upvotes: 1

Related Questions