Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
New Date() saves date 5 hours later
Message
 
 
Information générale
Forum:
Javascript
Catégorie:
Autre
Divers
Thread ID:
01618413
Message ID:
01618418
Vues:
47
I've looked at the data and I can see that the date is an object and displays

Tue Apr 14 2015 16:25:31 GMT-0500 (Central Daylight Time)

Here is some relevant code from our date time picker directive:
app.directive('smDateTimePicker', [function () {
        return {
            require: 'ngModel',
            restrict: 'E',

            scope: {
                ngModel: '=',//the date being saved to the database
                compareDate: '=',//either the start or end date of the range for validation (isDateRange attr must be set to true)
                form: '=',//the name of the form (if a subform, this will be parent.subform), used for setting the form to dirty and validation
                name: '@',//the input's name, used to manually outline invalid fields in red
                placeholder: '@',//text of placeholder on input field, 
                showBod: '=',//if true, we display the Beginning of Day button (pickTime must be true)
                showEod: '=',//if true, we display the End of Day button (pickTime must be true)
                showNow: '=',//if true, we display the Now button
                setBod: '&',//function defined in the directive that is called when clicking on the BOD button
                setEod: '&',//function defined in the directive that is called when clicking on the EOD button
                setNow: '&',//function defined in the directive that is called when clicking on the Now button
                validStartDateRange: '&',//function defined in the directive that determines whether to show validation message
                validEndDateRange: '&',
                validRequired: '&',
                validFutureDate: '&',
                validPastDate: '&'
            },
            templateUrl: "/app/templates/smDateTimePicker",
            link:  function (scope, element, attrs, controller) {
                    if (!controller) return undefined;

                    var updateModel, onblur, datetimepicker, options;
                    var programmaticChange = false;
                    var pickTime = attrs.pickTime === "true" ? true : false;
                    var isDateRange = attrs.isDateRange === "true" ? true : false; //if true, we do range validation on start and end dates
                    var isRequired = attrs.isRequired === "true" ? true : false; //if true, we do required validation
                    scope.showBod = (scope.showBod && pickTime); //only display showBod button if pickTime is true
                    scope.showEod = (scope.showEod && pickTime); //only display showEod button if pickTime is true
                    var onlyFutureDates = attrs.onlyFutureDates === "true" ? true : false;
                    var onlyPastDates = attrs.onlyPastDates === "true" ? true : false;
                    var isEndDate = attrs.isEndDate === "true" ? true : false;
                    var noDirtyCheck = attrs.noDirtyCheck === "true" ? true : false;

                    var format = function (date) {
                        if (date) {
                            if (!pickTime) {
                                if (isEndDate && !onlyFutureDates) {
                                    date = date.hours(23).minutes(59).seconds(59).milliseconds(997);
                                    date = date.format("YYYY-MM-DDTHH:mm:ss.SSS").toString();
                                } else {
                                    date = date.hours(0).minutes(0).seconds(0).milliseconds(0);
                                    date = date.format("YYYY-MM-DDTHH:mm:ss").toString();
                                }
                            }                            
                        }
                        return date;
                    };

                    var setStartDateLimit = function () {
                        if (onlyFutureDates) {
                            return moment().add('days', 1);
                        }
                        return moment("1753 01 01", "YYYY MM DD");
                    };

                    var setEndDateLimit = function () {
                        if (onlyPastDates) {
                            return moment().subtract('days', 1);
                        }
                        return moment("9999 12 31", "YYYY MM DD");
                    };

                    var setCalendarPickerStartDate = function () {
                        if (onlyFutureDates) {
                            return moment().add('days', 1);
                        } else if (onlyPastDates) {
                            return moment().subtract('days', 1);
                        } else {
                            return moment();
                        }
                    };
var init = function () {
                        options = {
                            icons: {
                                time: "fa fa-clock-o icon-1x",
                                date: "fa fa-calendar icon-1x",
                                up: "fa fa-arrow-up",
                                down: "fa fa-arrow-down"
                            },
                            //pickDate: true,
                            //pickTime: pickTime,
                            format: pickTime ? 'MM/DD/YYYY hh:mm:ss a' : 'MM/DD/YYYY',
                            //language: 'en',
                            locale: 'en',
                            //startDate: format(setStartDateLimit()),
                            //endDate: format(setEndDateLimit()),
                            minDate: setStartDateLimit(),
                            maxDate: setEndDateLimit(),
                            useStrict: true
                        };
                        datetimepicker = element.find('.input-group').datetimepicker(options);
                    };

                    init();
So, somehow using moment function results as date being saved in UTC. Do you know what should we change in the above in order to save the date as entered (and as shown on the form)?

Thanks in advance.


>Hi everybody,
>
>I have the following object
>
>
> $scope.transactionObject = {
>                    accountNameHash: '',
>                    operatorCode: userService.opCode.trim(),
>                    salespoint: userService.salespoint,
>                    message: '',
>                    paymentType: 0,
>                    transactionDate: new Date(),
>                    invoicesToPay: []
>                };
>
>I can see (if I don't apply the date filter to display) that it shows GMT - 5 hours.
>
>Anyway, when I try to save my data, the date is actually 5 hours later than my current date. I am not doing anything special except that I am using a directive with the following template:
>
>
>@using Siriusware.Resources;
>
><h3 style="text-align:center;">{{metaData.title}}</h3>
>
><label class="control-label col-md-2" title="@Labels.operatorLabel">@Labels.operatorLabel:</label>
>
><div class="col-md-3">
>    <select class="form-control" ng-model="transactionData.operatorCode"
>            required
>            name="operator" id="operator" data-no:dirty-check
>            ng-options="t.opCode as t.opCode for t in metaData.operators"></select>
></div>
>
><label class="control-label col-md-2" title="@Labels.message">@Labels.message:</label>
><div class="col-md-5">
>    <input type="text" name="message" id="message"
>           ng-model="transactionData.message"
>           class="form-control"
>           data-no:dirty-check
>           ng-maxlength="20" />
>    <div class="field-validation-error">
>        <span ng-show="formObject.message.$error.maxlength">@String.Format(Messages.cannotExceed, Labels.message, 20)</span>
>    </div>
></div>
>
><label class="control-label col-md-2" title="@Labels.salespoint">@Labels.salespoint:</label>
><div class="col-md-3">
>    <select class="form-control" ng-model="transactionData.salespoint" name="salespoint"
>            id="salespoint" data-no:dirty-check required
>            ng-options="t.salespoint as t.salespoint for t in metaData.salespoints"></select>
></div>
>
><label class="control-label col-md-2">@Labels.transactionDate:</label>
><div class="col-md-5">
>    <data-sm:date-time-picker ng-model="transactionData.transactionDate"
>                              is-date-range="false"
>                              form="formObject"
>                              placeholder="mm/dd/yy hh:mm:ss"
>                              pick-time="true"
>                              show-bod="true"
>                              show-eod="true"
>                              show-now="true"
>                              name="transactionDate"
>                              data-no:dirty-check
>                              required>
>    </data-sm:date-time-picker>
>    <div class="field-validation-error">
>        <span ng-show="formObject.transactionDate.$error.required">@String.Format(Messages.isRequired, Labels.transactionDate)</span>
>    </div>
></div>
><div class="clearfix"></div>
>
>I am going to check how other dates are saved in our application but do you see why would it add 5 extra hours to the date?
>
>UPDATE. Tested in other places where we use the date time picker without extra directive and the date is saved 5 hours later as well. I think most likely the problem is in our date time picker directive.
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform