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:
01618490
Vues:
62
This message has been marked as a message which has helped to the initial question of the thread.
JavaScript dates are always UTC dates meaning they are at GMT 0. Your timezone puts you at a 5 hour offset from that.

If you're posting that data to the server, the server will see that date as UTC. If your backend is .NET then the date will be DateTimeKind.Utc. You can convert that to locale time.
var utcTime = DateTime.UtcNow;
var localTime = utcTime.ToLocalTime();
In JavaScript you can use .toLocaleTimeString() and .toLocaleDateString() to get the local time for display purposes.

But - in reality you should always store time values as UTC and convert them to the local time only for display purposes.

I wrote a blog post about Timezone's a few weeks back - might be a useful read for you:

http://weblog.west-wind.com/posts/2015/Feb/10/Back-to-Basics-UTC-and-TimeZones-in-NET-Web-Apps



>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.
+++ Rick ---

West Wind Technologies
Maui, Hawaii

west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web

Where do you want to surf today?
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform