javascript 不会将 angular ui datepicker 日期正确转换为 UTC [英] javascript doesn't convert angular ui datepicker date to UTC correctly

查看:21
本文介绍了javascript 不会将 angular ui datepicker 日期正确转换为 UTC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目(asp.net web api + angularjs)中使用了 angular ui datepicker.一切正常,但是当我尝试将日期保存到 Db 时,它不会将其正确转换为 UTC 格式,而是将 1 天(实际上是几个小时,但它也会影响一天).

例如,当我在日期选择器中选择 01/11/2014 时:

Angularjs 对象:2014 年 11 月 1 日星期六 00:00:00 GMT+0200(FLE 标准时间)在请求中:2014-10-31T22:00:00.000Z在asp.net api控制器中:{31-Oct-14 10:00:00 PM}

日期选择器控制器:

app.controller('DatepickerCtrl', function ($scope) {$scope.today = 函数 () {$scope.dt = new Date();};$scope.today();$scope.clear = function () {$scope.dt = null;};$scope.open = 函数($event){$event.preventDefault();$event.stopPropagation();$scope.opened = true;};$scope.format = 'dd/MM/yyyy';$scope.dateOptions = {起始日":1};});

hml:

<label for="date">出生日期</label><p class="输入组"><input type="text" name="date1" ui-date="{dateFormat: 'yy-mm-dd'}" ui-date-format="yy-mm-dd" placeholder="DD/MM/YYYY" class="form-control" datepicker-popup="{{format}}" ng-model="user.Personal.BirthDate" max-date="currentDate" is-open="opened" close-text="关闭"/><span class="input-group-btn"><button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></按钮></span></p>

但似乎角度日期选择器突出显示了正确的日期:

我尝试手动将日期转换为 UTC 格式,但也没有成功

 toUTCDate: function (d) {var date = new Date(d);var _utc = new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate());//, date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds()返回_utc;}

我应该指定时区还是使用 angular js 过滤器?

解决方案

我遇到了完全相同的问题,解决方案是删除日期部分的时区组件.为了以更通用的方式解决问题,我编写了一个指令

csapp.directive("csDateToIso", function () {var linkFunction = 函数(范围、元素、属性、ngModelCtrl){ngModelCtrl.$parsers.push(function (datepickerValue) {return moment(datepickerValue).format("YYYY-MM-DD");});};返回 {限制:A",要求:ngModel",链接:linkFunction};});

上述指令删除了时区部分,只考虑了 ISO 日期格式输入的日期部分.

我确实使用 moment.js 进行日期操作.但是你可以很容易地转换为不使用moment.js

编辑

像下面那样使用

 

I'm using angular ui datepicker in my project(asp.net web api + angularjs). All works fine but when I'm trying to save date to Db it doesn't convert it to UTC format correctly and substructs 1 day(actually a few hours but it affects for a day too).

For example when I choose 01/11/2014 in the datepicker:

Angularjs object:
Sat Nov 01 2014 00:00:00 GMT+0200 (FLE Standard Time)

In request: 
2014-10-31T22:00:00.000Z

In asp.net api controller:
{31-Oct-14 10:00:00 PM}

Datepicker controller:

app.controller('DatepickerCtrl', function ($scope) {
    $scope.today = function () {
        $scope.dt = new Date();
    };
    $scope.today();
    $scope.clear = function () {
        $scope.dt = null;
    };
    $scope.open = function ($event) {
        $event.preventDefault();
        $event.stopPropagation();

        $scope.opened = true;
    };
    $scope.format = 'dd/MM/yyyy';
    $scope.dateOptions = {
        'starting-day': 1
    };
});

hnml:

<div class="form-group inputGroupContainer" ng-controller="DatepickerCtrl">
                    <label for="date">Date of Birth</label>
                    <p class="input-group">
                        <input type="text" name="date1" ui-date="{dateFormat: 'yy-mm-dd'}" ui-date-format="yy-mm-dd" placeholder="DD/MM/YYYY" class="form-control" datepicker-popup="{{format}}" ng-model="user.Personal.BirthDate" max-date="currentDate" is-open="opened" close-text="Close" />
                        <span class="input-group-btn">
                            <button type="button"   class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
                        </span>
                    </p>
                </div>

But seems like angular datepicker highlight the correct date:

I've tried to manually convert date to UTC format but unsuccessfully as well

   toUTCDate: function (d) {
            var date = new Date(d);
            var _utc = new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate());//, date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds()
            return _utc;
        }

Should I specify timezone or use angular js filter?

解决方案

I have had the exact same problem, the solution was to remove the timezone component of the date part. To solve the problem in a more generic way I had written a directive

csapp.directive("csDateToIso", function () {

    var linkFunction = function (scope, element, attrs, ngModelCtrl) {

        ngModelCtrl.$parsers.push(function (datepickerValue) {
            return moment(datepickerValue).format("YYYY-MM-DD");
        });
    };

    return {
        restrict: "A",
        require: "ngModel",
        link: linkFunction
    };
});

above directive removes the timezone part and only considers the date part of the ISO date format input.

I do use moment.js for date manipulations. But you can easily convert above to not use moment.js

EDIT

use it like below

  <input ng-model='abc' cs-date-to-iso ui-datepicker>

这篇关于javascript 不会将 angular ui datepicker 日期正确转换为 UTC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆