如何在没有时区发送AngularStrap日期选择器值? [英] How to send AngularStrap datepicker value without timezone?

查看:179
本文介绍了如何在没有时区发送AngularStrap日期选择器值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有可能使用AngularStrap的日期选择器,没有它保持用户的语言环境的时区信息。在我们的应用中,我们要处理的有到期日合约的对象。

I'm wondering if it's possible to use AngularStrap's datepicker without it keeping the user's locale's timezone information. In our application we want to handle Contract objects that have an expiration date.

添加或编辑合同对象时,存在用于选择日期一个datepicker字段。下面的事情发生了:

When adding or editing the contract object, there is a datepicker field for selecting the date. The following thing happens:


  1. 用户选择的日期(例如2013年10月24日)
  2. 角结合的JavaScript日期对象到NG-模型字段

  3. 的绑定日期对象是在用户的时区(例如GMT + 3)

  4. 的用户提交表单

  5. 日期越来越采用了棱角分明的$ HTTP服务发送给服务器

在步骤5的时间被转换为UTC格式。选定的日期是GMT + 3 2013年10月24日午夜,但UTC转换晚上九时更改日期2013年10月23日。

In step 5 the date is converted to UTC format. The selected date was GMT+3 2013-10-24 at midnight, but the UTC conversion changes the date to 2013-10-23 at 9pm.

我们怎能prevent转换,或者使用全过程UTC日期?我们不希望根据用户的本地时区合同的日期改变。相反,我们希望日期为2013年10月24日总,不管是什么时区。

How could we prevent the conversion, or use UTC dates during the whole process? We don't want the contract's date to change based on the user's local timezone. Instead, we want the date to be always 2013-10-24, no matter what timezone.

我们的当前的解决方案是使小的变化对AngularStrap库,以便当发送到服务器的时间不会改变。

Our current solution was to make small changes to the AngularStrap library so that the date won't change when sent to the server.

如果我们能在服务器获取用户选择的时区,我们可以再拍转换存在,但该服务器没有这些信息。

If we could get the user's selected timezone in the server, we could make another conversion there, but the server doesn't have that information.

所有的想法都是AP preciated!

All ideas are appreciated!

推荐答案

这个问题不是AngularStrap。它是多么的JavaScript工作的日期和JSON如何格式化,以便传输。当你打开一个javascript日期对象转换成JSON字符串,它格式化字符串作为UTC。

The issue isn't AngularStrap. Its just how javascript dates work and how JSON formats them for transmission. When you turn a javascript date object into a JSON string, it formats the string as UTC.

例如,我在犹他,现在是07:41于2013年10月24日。如果我创建一个新的JavaScript日期并将其打印到控制台,它会说:

For example, I'm in Utah and it is now 07:41 on 2013-10-24. If I create a new javascript date and print it to the console it will say:

Thu Oct 24 2013 07:41:19 GMT-0600 (MDT)

如果我字符串化同一天(使用 JSON.stringify(日期),我得到:

If I stringify that same date (using JSON.stringify(date), I get:

"2013-10-24T13:41:47.656Z"

,你可以看看是不是在我目前的时区,但在UTC。所以在转换发生之前的形式被发送到时,它会从一个JavaScript对象转换为JSON字符串的服务器。

which you can see is not in my current timezone, but is in UTC. So the conversion is happening just before the form gets sent to the server when it gets converted from a javascript object to a JSON string.

做这将是之前刚刚更改日期为您自己选择的一个字符串,发送日期到服务器的最简单方法。因此,与其让JSON日期更改为UTC,(假设你不在乎一天的时间),你可以只是做这样的事情:

The easiest way to do it would be to just change the date to a string of your own choosing prior to sending the date to the server. So instead of letting JSON change the date to UTC, (assuming you don't care about the time of day) you could just do something like this:

var dateStrToSend = $scope.date.getUTCFullYear() + '-' + ($scope.date.getUTCMonth() + 1) +  '-' + $scope.date.getUTCDate();

这会给你一个基于UTC的字符串,看起来像2013年10月24日,然后你可以发送到服务器,而不是JSON格式它包括时间信息。希望帮助。

That will give you a UTC-based string that looks like '2013-10-24' and then you can send that to the server, instead of the JSON format which includes the time info. Hopefully that helps.

更新:由于@马特约翰逊说,有两种方法可以做到这一点。你说:我们怎么能prevent转换,或使用UTC全过程日期?。如果你想使用UTC,然后用我上面的解释。如果你想只是prevent转换中,你可以使用下列内容:

UPDATE: As @Matt Johnson said, there are two ways to do it. You said: How could we prevent the conversion, or use UTC dates during the whole process?. If you want to use UTC, then use my above explanation. If you want to just "prevent the conversion", you could use the following:

var dateStrToSend = $scope.date.getFullYear() + '-' + ($scope.date.getMonth() + 1) +  '-' + $scope.date.getDate();

这篇关于如何在没有时区发送AngularStrap日期选择器值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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