SPApps如何保存为UTC,然后根据用户访问时区应用convertion [英] SPApps how to save as UTC and then apply convertion based on user access timezone

查看:91
本文介绍了SPApps如何保存为UTC,然后根据用户访问时区应用convertion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库字段类型DateTime 名为 FromDate
ToDate  ,它可以节省当地时间。但是我想以UTC格式保存。在保存到数据库之前,我应该如何转换?看起来它可以正确保存但是如何在从数据库
检索值到用户本地时间时应用转换当人们跨时区访问时?


  foreach(在mvctest.Attributions VAR项)

  {


          &NBSP ;         item.From_Date = item.From_Date.ToUniversalTime();

                     item.To_Date = item.To_Date.ToUniversalTime();


 使用(SqlConnection的康恩=新的SqlConnection(ConfigurationManager.ConnectionStrings [" CONNSTRING"]。的ConnectionString))

  {

      串customQuery = @ [更新TESTDB SET


              &NBSP ;                    FROM_DATE = @From_Date,


                &NBSP ;                  To_Date = @ To_Date"

    

              var count = conn.Execute(customQuery,item);

 }

}


我尝试了各种方法来检索本地时间动态基于用户访问应用程序但没有正常工作?请你帮忙添加函数来动态转换基于UTC的本地时间?请帮助



    TestDB.From_Date = DateTime.SpecifyKind(TestDB.From_Date,DateTimeKind.Utc);

  var localTime = TestDB.From_Date.ToLocalTime();

  TestDB.Eff_Date = localTime;

  TestDB.To_Date = DateTime.SpecifyKind(TestDB.To_Date,DateTimeKind.Utc);

  var localTime2 = TestDB.To_Date.ToLocalTime();

  TestDB.To_Date = localTime2;


解决方案

您好


您发布到了错误的电路板。


您可以将数据存储为UTC,并在必要时将其转换为本地。


https://docs.microsoft.com/ EN-US / DOTNET / API / system.timezoneinfo.converttime redirectedfrom = MSDN&安培;视图= netframework-4.7.2#System_TimeZoneInfo_ConvertTime_System_DateTime_System_TimeZoneInfo_System_TimeZoneInfo_


I have a database field type DateTime called FromDate and ToDate  and it saves in the local time. However I want to save in UTC. Is this how should I convert before saving in the database? It looks like it saves correctly but how do I apply conversion while retrieving the value from database to users local time when people access across timezone ?

 foreach (var item in mvctest.Attributions)
 {

                    item.From_Date = item.From_Date.ToUniversalTime();
                    item.To_Date = item.To_Date.ToUniversalTime();

 using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString))
 {
       string customQuery = @"UPDATE TestDB SET
                                   From_Date = @From_Date,
                                   To_Date = @To_Date"
    
              var count = conn.Execute(customQuery, item);
 }
}

I tried various ways to retrieve local time dynamically based on user access the application but none works correctly? Will you please help adding function to dynamically convert the local time based on UTC? Please help

   TestDB.From_Date = DateTime.SpecifyKind(TestDB.From_Date, DateTimeKind.Utc);
  var localTime = TestDB.From_Date.ToLocalTime();
  TestDB.Eff_Date = localTime;
  TestDB.To_Date = DateTime.SpecifyKind(TestDB.To_Date, DateTimeKind.Utc);
  var localTime2 = TestDB.To_Date.ToLocalTime();
  TestDB.To_Date = localTime2;

解决方案

Hi

You post to the wrong board.

You can store data as UTC, and convert it to local when necessary.

https://docs.microsoft.com/en-us/dotnet/api/system.timezoneinfo.converttime?redirectedfrom=MSDN&view=netframework-4.7.2#System_TimeZoneInfo_ConvertTime_System_DateTime_System_TimeZoneInfo_System_TimeZoneInfo_


这篇关于SPApps如何保存为UTC,然后根据用户访问时区应用convertion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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