开发者

REQUEST_TIME in C#

Is there a similar function in C# that is 开发者_开发技巧akin to the PHP functionality of ("$_SERVER['REQUEST_TIME']")?


The following property returns the initial timestamp of the current HTTP request:

HttpRequest.RequestContext.HttpContext.Timestamp

In an ASP.Net-Page it can be accessed through the Request object like this:

Request.RequestContext.HttpContext.Timestamp

Hope I could help.


You can use the following extension method to convert a DateTime to a Unix timestamp (which is what I understand to be held in REQUEST_TIME):

public static class TimeTools
{
    private static readonly DateTime StartOfEpoch = 
        new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

    public static long ToUnixTime(this DateTime dt)
    {
        return Convert.ToInt64(
            (dt.ToUniversalTime() - StartOfEpoch).TotalSeconds);
    }
}

so (borrowing from @Dennis` answer)

Request.RequestContext.HttpContext.Timestamp.ToUnixTime()


I think you're looking for DateTime.Now or DateTime.UtcNow.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜