JavaScript的简化版,角UI日期选择正确的日期转换为UTC

编程入门 行业动态 更新时间:2024-10-25 04:23:12
本文介绍了JavaScript的简化版,角UI日期选择正确的日期转换为UTC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我采用了棱角分明的UI日期选择在我的项目(​​asp网页API + angularjs)。一切工作正常,但是当我试图保存日期分贝它不它正确转换为UTC格式和substructs 1天(实际上是几个小时,但它影响了一天太)。

I'm using angular ui datepicker in my project(asp 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).

例如,当我在日期选择器中选择2014年1月11日:

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 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:

我试过手动日期转换为UTC格式,但没有成功,以及

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; }

我应该指定时区或使用角度JS过滤器?我将AP preciate任何帮助!

Should I specify timezone or use angular js filter? I'll appreciate any help!

推荐答案

我有完全相同的问题,解决的办法是删除的日期部分的组件时区。为了解决一个更通用的方式,我写了一个指令,这个问题

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 }; });

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

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

我使用日期操作moment.js。但是你可以很容易地转换上述不使用moment.js

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

修改

使用它像下面

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

更多推荐

JavaScript的简化版,角UI日期选择正确的日期转换为UTC

本文发布于:2023-10-27 00:15:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531761.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   转换为   正确   简化版   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!