如何在没有时区的情况下发送 AngularStrap datepicker 值? [英] How to send AngularStrap datepicker value without timezone?

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

问题描述

我想知道是否可以使用 AngularStrap 的日期选择器而不保留用户的区域设置的时区信息.在我们的应用程序中,我们希望处理具有到期日期的 Contract 对象.

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.

在添加或编辑合同对象时,有一个用于选择日期的日期选择器字段.发生以下事情:

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. Angular 将 javascript 日期对象绑定到 ng-model 字段
  3. 绑定日期对象位于用户所在的时区(例如 GMT+3)
  4. 用户提交表单
  5. 使用 Angular 的 $http 服务将日期发送到服务器

在第 5 步中,日期被转换为 UTC 格式.所选日期是 GMT+3 2013-10-24 午夜,但 UTC 转换将日期更改为 2013-10-23 晚上 9 点.

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.

我们如何防止转换,或在整个过程中使用 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.

感谢所有想法!

推荐答案

问题不在于 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.

例如,我在犹他州,现在是 2013-10-24 的 07:41.如果我创建一个新的 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(date),我得到:

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

这将为您提供一个类似于 '2013-10-24' 的基于 UTC 的字符串,然后您可以将其发送到服务器,而不是包含时间信息的 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.

更新:正如@Matt Johnson 所说,有两种方法可以做到.您说:我们如何防止转换,或在整个过程中使用 UTC 日期?.如果要使用UTC,请使用我上面的解释.如果您只想防止转换",您可以使用以下内容:

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 datepicker 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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