如何将显示持续时间的时间值转换为另一个时间值到另一个时区 我正在做一个拍卖网站,我会显示每次的持续时间,以及如何确保如果有人在另一个国家/地区,则可以在他自己的时区持续时间内显示持续时间
Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs)
'--this method will fetch the end date from repeater item label and update the duration
UpdateDuration()
End Sub
Private Sub UpdateDuration()
For Each item As DataListItem In DataList1.Items
'--calculate the duration
Dim duration As TimeSpan = Convert.ToDateTime(TryCast(item.FindControl("lblEndDate"), Label).Text) - DateTime.Now
Dim label2 = TryCast(item.FindControl("lblDuration"), Label)
'--Here is the important part: if any duration is expired then bind the grid again
If (duration.Ticks < 0) Then
'Grab your DateTime object (checking that it exists)'
'Now you can output your time as you please (excluding any decimal points)'
BindItems()
Exit For
Else
'-- showing end date as last days has not arrived
TryCast(item.FindControl("lblDuration"), Label).Text = New DateTime(duration.Ticks).ToString("dd:HH:mm:ss")
label2.Text = String.Format("{0:dd\:hh\:mm\:ss}", duration)
label2.Text = Replace(label2.Text, ":", " Days ", , 1)
label2.Text = Replace(label2.Text, ":", " Hours ", , 1)
label2.Text = Replace(label2.Text, ":", " Mins ", , 1)
End If
Next
End Sub
答案 0 :(得分:0)
javascript中的以下行可以为您提供相对于UTC的客户端计算机时区的timezoneoffset(有关详细信息,请访问此link):
var offset = new Date().getTimezoneOffset();
一旦知道偏移量,就可以使用以下C#代码获取UTC time:
DateTime UtcNow = DateTime.UtcNow;
您可以将偏移量保存在客户端的隐藏字段中,并在服务器上将其保存在每个用户的会话中。根据偏移的符号,您需要决定是否在UTC时间内添加或减去分钟数。如果值为负,则说-330然后将330分钟添加到UTC日期时间,如果值为正,则说20然后从UTC日期时间减去20分钟。 e.g。
DateTime clientTime=UtcNow.AddMinutes(20)
以上说明将帮助您实现您的目标。但我还想给你一些关于安全的建议。 不应根据您从客户端获得的数据做出决定,因为这些数据可能已被调整。您从客户端获取的时区偏移量应仅用于在浏览器上显示时间,但是它不应该影响你的拍卖会从中部标准时间上午11点开始运行。希望你了解这里的风险。
希望这会有所帮助。如果需要,请要求澄清,特别是如果您不清楚可能存在的风险。