Asp.net 将计时器转换为不同的时区

Asp.net 将计时器转换为不同的时区,asp.net,vb.net,Asp.net,Vb.net,如何将显示持续时间的时间值转换为另一个时间值和另一个时区 我正在做一个拍卖网站,我显示每一次的持续时间,以及如何确保如果有人在另一个国家,它可以显示持续时间,在他自己的时区的持续时间请 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 du

如何将显示持续时间的时间值转换为另一个时间值和另一个时区 我正在做一个拍卖网站,我显示每一次的持续时间,以及如何确保如果有人在另一个国家,它可以显示持续时间,在他自己的时区的持续时间请

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
Protected Sub Timer1\u Tick(ByVal sender作为对象,ByVal e作为事件args)
“--此方法将从repeater item label获取结束日期并更新持续时间
更新()
端接头
私有子更新()
对于DataList1.Items中作为DataListItem的每个项目
--计算持续时间
Dim duration As TimeSpan=Convert.ToDateTime(TryCast(item.FindControl(“lblEndDate”),Label.Text)-DateTime.Now
Dim label2=TryCast(item.FindControl(“lblDuration”),标签)
“--重要的一点是:如果任何持续时间过期,则再次绑定网格
如果(duration.Ticks<0),则
'获取DateTime对象(检查它是否存在)'
'现在您可以随心所欲地输出时间(不包括任何小数点)'
BindItems()
退出
其他的
“--显示结束日期,因为最后几天尚未到达
TryCast(item.FindControl(“lblDuration”),Label.Text=newdatetime(duration.Ticks).ToString(“dd:HH:mm:ss”)
label2.Text=String.Format(“{0:dd\:hh\:mm\:ss}”,持续时间)
label2.Text=Replace(label2.Text,“:”,“天”,1)
label2.Text=替换(label2.Text,“:”,“小时”,1)
label2.Text=Replace(label2.Text,“:”,“分钟”,1)
如果结束
下一个
端接头

javascript中的这一行可以提供客户机时区相对于UTC的时区偏移(有关详细信息,请访问此行):

知道偏移量后,可以使用以下C代码获得:

您可以将偏移保存在客户端的隐藏字段中,然后在服务器上存储一次,将其存储在每个用户的会话中。根据偏移的符号,您需要决定是从UTC时间中增加还是减少分钟。如果值为负数(如-330),则UTC日期时间加330分钟,如果值为正数(如20),则UTC日期时间减去20分钟。e、 g

DateTime clientTime=UtcNow.AddMinutes(20)
以上说明将帮助您实现您的愿望。但我也想给你一个关于安全的建议不应根据您从客户处获取的数据做出任何决定,因为这些数据可能已被修改。您从客户处获取的时区偏移量应仅用于在其浏览器上显示时间,但它不应影响您的拍卖将从CST上午11点开始运行1小时这一事实。希望你明白这里的风险


希望这有帮助。如果需要,请要求澄清,特别是如果您不清楚可能存在的风险。

我会给您一个场景,比如我登录您的网站,我在x时区。现在,为了获得更多的时间,我会改变我的时区。所以,你所需要做的就是设计你的功能,记住这种情况。只要问自己一个问题“如果有人改变时区怎么办?”。并且尽量让你的答案接近“即使有人改变了时区,也不会影响我的功能”。哦,但我想要的是,用户在注册时选择它,然后所有事情都在代码中完成,然后就可以了。如果用户更改时区不影响。时区将只设置为注册,然后在此之后,系统将为不同国家/地区的每个用户计算每个持续时间,我已经完成了持续时间,但如何让它为每个国家/地区计算每个持续时间,这会给我带来问题,如果您能提供帮助,将非常有用
DateTime UtcNow = DateTime.UtcNow;
DateTime clientTime=UtcNow.AddMinutes(20)