代码改变世界

sharepoint中的时间问题

2008-12-24 15:09  Virus-BeautyCode  阅读(899)  评论(2编辑  收藏  举报


在页面的时间栏中填写的时间如果是
2008-8-8 0:00
然后保存页面,用程序访问刚才的列表项,然后显示刚才的时间,却显示为2008-8-8 8:00.
刚开始我也很是奇怪呢,后来才发现,估计是时区在作怪,sharepoint内部存储时间都是用那个什么格陵时间吧,可能是为了统一。然后在页面显示的时候才显示为本地时间。

用下面的函数可以转化本地时间为标准时间
DateTime.Now.ToUniversalTime().ToString()

还有就是容易搞混的
8月24号的24:00就是8月25号的0:00
这个问题把我弄混好多次了,几乎每次都中招。

下面的代码是处理过期的新闻测试代码,在sharepoint中的过期新闻,被我从“页面(Pages)”列表库移动到了“过期新闻(DocLib)”列表库


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace ConsoleApplication1
{
    
class Program
    {


        
static void Main(string[] args)
        {
            SPSecurity.RunWithElevatedPrivileges(
                                               
delegate()
                                               {
                                                   
using (SPSite site = new SPSite(@"http://virus/sites/intranet"))
                                                   {
                                                       
using (SPWeb web = site.AllWebs["team"])
                                                       {
                                                           web.AllowUnsafeUpdates 
= true;

                                                           SPList list 
= web.Lists["页面"];
                                                           
foreach (SPListItem item in list.Items)
                                                           {
                                                               Console.WriteLine(
"名称:" + item.GetFormattedValue("名称"+ "结束日期:" + item.GetFormattedValue("结束日期"));
                                                               Console.WriteLine(DateTime.Now.ToUniversalTime().ToString());
                                                           }

                                                          
// SPList list = web.Lists["页面"];
                                                           SPListItemCollection items = null;
                                                           SPQuery query 
= new SPQuery();
                                                             query.Query 
= @"<Where>
                                                              <Leq>
                                                                 <FieldRef Name='
" + list.Fields["结束日期"].InternalName + @"' />
                                                                 <Value Type='DateTime'>
" + DateTime.Now.ToString() + @"Z</Value>
                                                              </Leq>
                                                           </Where>
";
                                                           items 
= list.GetItems(query);
                                                           
foreach (SPListItem item in items)
                                                           {
                                                               item.CopyTo(web.Url 
+ "/DocLib/" + item.Name);
                                                               
//item.Delete();
                                                           }

                                                           
//#region 从Pages(中文名字:页面)列表库拷贝/移动文件到DocLib(中文名字:过期新闻)列表库
                                                           
//SPList list = web.Lists["页面"];
                                                           
//foreach (SPListItem item in list.Items)
                                                           
//{
                                                           
//    //列表项从一个列表库拷贝到另外一个列表库,列表项不支持移动(MoveTto)
                                                           
//    item.CopyTo(web.Url + "/DocLib/" + item.Name);
                                                           
//}
                                                           
//#endregion

                                                           
//#region 从Pages(中文名字:页面)文档库拷贝/移动文件到DocLib(中文名字:过期新闻)文档库
                                                    
//       SPFolder oFolder = web.GetFolder("Pages");
                                                    
//       SPFileCollection collFiles = oFolder.Files;

                                                    
//       for (int intIndex = collFiles.Count - 1; intIndex > -1; intIndex--)
                                                    
//       {
                                                    
//           //文档从一个文档库拷贝到另一个文档库
                                                    
//               collFiles[intIndex].CopyTo(web.Url+"/DocLib/" + collFiles[intIndex].Name);
                                                    
//               //文档从一个文档库移动到另一个文档库
                                                    
//           collFiles[intIndex].MoveTo(web.Url+"/DocLib/" + collFiles[intIndex].Name);

                                                    
//       }

                                                    
//#endregion
                                                           web.AllowUnsafeUpdates = false;
                                                       }
                                                   }
                                               });

            Console.ReadLine();
        }
    }
}