使用时间跨度限制时间内登录到网站C#ASP.NET [英] C# ASP.NET using TimeSpan to restrict hours to log into a website

查看:203
本文介绍了使用时间跨度限制时间内登录到网站C#ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有,

当客户登录我的网站,我想显示多少时间剩余,直到日志中的链接超时。

When a customer logs into my website, I want to show how much time is remaining until the log in link times out.

使用C#中的时间跨度结构,由于某种原因,我无法弄清楚如何做到这一点的任务:

Using the TimeSpan structure in C#, for some reason, I cannot figure out how to do this task:

TimeSpan diff = (DateTime.Now - orderDate   );

当在客户登录,我要拿出多少时间,他/她剩余可登录到其客户......你有3个小时33分,直到这个链接倍。 ......你有2个小时25分钟,直到此链接超时等。

When the customer logs in, I want to show the customer how much time he/she has remaining to log in... "you have 3 hours 33 minutes until this link times out." ... "You have 2 hours 25 minutes until this link times out" etc.

在4个小时命中,当客户登录时,我要他/她重定向到一个对不起...链接超时页。

Once 4 hours is hit, when the customer logs in, I am going to redirect him/her to a sorry... the link has timed out page.

因此​​,一些Psudo code可能是...

So some Psudo code might be...

if (diff.Hours >= 4)
{
response.redirect("log_in_timed_out.aspx");
}
else
{
lblTimeRemaining.Text = "You have " + diff.Hours + " hours and " + diff.Minutes + " minutes remaining until the link times out.";
}

这psudo code,使小时和分钟计数,而不是下降到0。所以我登录,它说你有1个小时33分,2个小时12分钟等,因为时间跨度是给我的两个日期之间的差异。我希望它倒计时到0,3个小时23分钟,2小时15分钟,1小时5分钟等。

This psudo code makes the hours and minutes count up instead of down to 0. So I log in and it says you have 1 hours 33 minutes, 2 hours 12 minutes, etc. because the TimeSpan is giving me the difference between the two dates. And I want it to count down to 0 such as 3 hours 23 minutes, 2 hours 15 minutes, 1 hour 5 minutes, etc.

我怎么能显示多少时间剩下给客户每次他/她登录使用的时间跨度结构,并将它倒计时为0时?

How can I show how much time is remaining to the customer each time he/she logs in using the TimeSpan structure and have it count down to 0?

感谢任何反馈。

推荐答案

我建议保持所有内部的DateTime S IN UTC ,只有转换为本地时间显示。所以我用 DateTime.UtcNow 而不是 DateTime.Now ,并假设 orderTime 是UTC为好。

I recommend keeping all internal DateTimes in UTC, and only convert to local times for display. So I use DateTime.UtcNow instead of DateTime.Now and assume orderTime is in UTC as well.

我第一次在计算该链接到期时间,然后还剩余多少时间再说吧。

I first compute the time at which the link expires, and then how much time remains until then.

DateTime expirationTime = orderTime.AddHours(4);
TimeSpan timeRemaining = expirationTime - DateTime.UtcNow;
if(timeRemaining<TimeSpan.Zero)
  Error("Expired");
else
  Write("Remaining {0} hours {1} minutes",timeRemaining.Hours, timeRemaining.Minutes);

为了更好的可测试性它可以不叫 DateTime.UtcNow / 。现在内联,但传递有用中的值。但因为我不知道你的周围code和建筑,我就用简单的方法。

For better testability it can be useful not to call DateTime.UtcNow/.Now inline, but to pass in the value. But since I don't know your surrounding code and architecture, I went with the simple approach.

这篇关于使用时间跨度限制时间内登录到网站C#ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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