SharePoint DateTime默认值遵守时区 [英] SharePoint DateTime default value respect timezone

查看:333
本文介绍了SharePoint DateTime默认值遵守时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DateTime字段(仅限日期),该字段使用今天的日期"作为默认值. 但是,当用户将项目添加到列表中时,所选日期与GMT时区相对应.

I have a DateTime field (Date Only) that uses the "Today's Date" as a default value. However, when a user adds an item to the list the date that is selected is respective to the GMT timezone.

因此,如果用户在2012年2月22日晚上7点(美国东部标准时间)添加商品,则默认值将实际显示为2012年2月23日.

Therfore if a user adds an item on 2/22/2012 at 7pm (EST) the default value will actually read 2/23/2012.

我的第一个想法是检查Web应用程序设置,并将它们设置为-5 EST.然后,经过一些搜索,我发现时区可能会根据用户的区域设置而有所不同.测试这种方法后,我手动将用户设置为具有EST时区,并进行了测试,得出了相同的结果.

My first thought was to check the web application settings, and they were set to -5 EST. Then after some searching I found that time zones can differ based on a users regional settings. Testing this approach, I manually set a user to have a EST time zone, and performed the test that yielded the same result.

最后,经过更多搜索,我发现有一篇文章说使用DateTime字段的计算值"部分并指定"= NOW()"

Finally, after more searching I found one article that said to use the Calculated Value portion of the DateTime field and specify "=NOW()"

这将返回公式包含语法错误或不受支持."

This however returned "The formula contains a syntax error or is not supported."

有人知道让DateTime字段的默认值遵守Web应用程序或用户所在时区的方法吗?

谢谢.

推荐答案

Sharepoint将所有日期字段存储在UTC中.也就是说,如果您将日期时间字段保存在列表中,Sharepoint实际上会将您选择的时间转换为UTC,并将其转换回该人在检索时所在的任何时区.

Sharepoint stores all date field in UTC. That is, if you are saving a datetime field in a list, Sharepoint actually converts the time that you selected into UTC, and converts it back to whatever time zone the person is in when retrieving.

首先检查服务器时区和客户端之间是否存在差异.使用任何API时,SharePoint都会始终返回UTC时间,并由您决定在界面或应用程序中进行转换.

Begin by checking if there's a difference between your server timezone and your client. When using any API SharePoint will always return the UTC time and leave it up to you to make the conversion in your interface or application.

如果您想以其他方式解决此问题,则不能使用datetime数据类型,而是将日期存储在文本字段中.

If you like to solve this differently you can't use the datetime datatype, instead store dates in text fields.

==更新==

如果您从SharePoint(总是UTC)获取日期,并且只想将其转换为本地时间,这是一个好习惯

If you got a date from SharePoint (always in UTC) and simply want to convert it to your local time this is a good practice

DateTime localDateTime = sharePointDate.ToLocalTime();

如果日期是字符串,或者不确定格式,那可能是个好方法

If you got the date as a string, or are uncertain of the formatting and whatnot this can be a good approach

string dateStr = ("2/18/2012 9:49:51 PM").ToString(CultureInfo.InvariantCulture);
DateTime convertedDate = DateTime.SpecifyKind(DateTime.Parse(dateStr), DateTimeKind.Utc);
DateTime localDateTime = convertedDate.ToLocalTime();
// Outputs 2/18/2012 10:49:51 PM since I'm +1 in sweden

这篇关于SharePoint DateTime默认值遵守时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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