2013-05-06 35 views
0

我有我的數據庫服務器位於美國用戶從印度輸入日期必須存儲客戶端而不是服務器。但不幸的是,服務器日期存儲在數據庫中。我只向當天的用戶顯示僅限於當天的條目。 問題是當用戶輸入記錄直到12:30 PM(客戶機)顯示給用戶,但在12:30 PM(客戶機)之後,只有在12:30 PM(客戶機)之後才進行輸入時,顯示任何想法?如何從asp.net中的TimeZone獲取客戶端本地日期?

例如:在2013年6月5日(DD/MM/YYYY),直到12:30 PM做項顯示,當客戶機上的日期是2013年6月5日上午11時57 但是當客戶機日期是2013年6月5日下午12點57分,輸入的條目不會顯示,但顯示下午12點30分之後輸入的條目。在DB上面例如。創建日期存儲爲05/05/2013不知道爲什麼? 在cs文件:

public static DateTime GetIndianDate(DateTime dateTime) 
     { 
      return TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dateTime, TimeZoneInfo.Local.Id,"India Standard Time").Date; 

     } 

要僅顯示今天的條目:

shipments = BLL.GetShipmentsByUserIdNDate(userId, Classes.Common.GetIndianDate(DateTime.Now.Date)); 

public static List<Shipment> GetShipmentsByUserIdNDate(Guid userId, DateTime dateTime) 
     { 
      Entities db = new Entities(); 
      List<Shipment> shipments = (from s in db.Shipments 
             where s.UserId == userId && s.CreatedDate == dateTime 
             select s).ToList(); 
      return shipments; 
     } 

同時插入:

shipment.CreatedDate = Classes.Common.GetIndianDate(DateTime.Now.Date); 



public static DateTime GetIndianDate(DateTime dateTime) 
     { 
      return TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dateTime, TimeZoneInfo.Local.Id,"India Standard Time").Date; 

     } 

那麼,如何將存儲客戶機的日期?現在它仍然只存儲服務器日期。

幫助讚賞!

+0

[將UTC日期時間全局轉換爲用戶指定的本地日期時間]可能的副本(http://stackoverflow.com/questions/16345862/globally-convert-utc-datetimes-to-user-specified-local-datetimes) – 2013-05-06 13:47:43

回答

0

對於相同的情況,我建議您在保存記錄時每條記錄的日期時間。

當你取的記錄,請獲得時區,如果你想更新從C#代碼的日期請參見在這種情況下也需要UTC日期

這one.But操縱UTC日期

Select getutcdate() --Get UTC Date 

Select getdate()--Get Indain Standared Date 

Select dateadd(minute,30,dateadd(hour,5,getutcdate())) --Get Indian offset +05:30 

string utcDateString = System.DateTime.UtcNow.ToString(); 

     DateTime localDate = DateTime.Parse(utcDateString, 
            CultureInfo.CurrentCulture, 
            DateTimeStyles.AssumeUniversal); 

它幫助你。

+0

嗨@Neeraj Dubey我也有同樣的問題,你能告訴我如何設置utc用戶時區在c# – Alok 2013-12-06 14:46:59

相關問題