Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8150653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:07:44+00:00 2026-06-06T15:07:44+00:00

I am storing Datetime in a session as mentioned below:- Session[LoggedInTime] = System.DateTime.Now; Then

  • 0

I am storing Datetime in a session as mentioned below:-

Session["LoggedInTime"] = System.DateTime.Now;

Then i m retrieving this value on a page load like this:-

DateTime _loggedInTime = Convert.ToDateTime(Session["LoggedInTime"]);

I debug the above code code and find that up to here the _loggedInTIme is showing the correct date which i m storing in it. After that i m calculating the time span like this:-

TimeSpan elapsedtimespan = System.DateTime.Now.Subtract(_loggedInTime);
int elapsedtime = Convert.ToInt32(elapsedtimespan.TotalSeconds);

I found while debugging the code that ,while subtraction the _loggedInTime = {1/1/0001 12:00:00 AM} and due to which i m not able to get exact elapsedtime .

Please help me to solve this issue as i m not getting why the _loggedInTime become {1/1/0001 12:00:00 AM} at calculating TimeSpan.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:07:45+00:00Added an answer on June 6, 2026 at 3:07 pm

    The following works fine for me. Since you prefix _loggedInTime with an underscore I’m assuming you declared it as an instance variable of the page itself.

    private DateTime _loggedInTime;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["LoggedInTime"] == null)
            Session["LoggedInTime"] = DateTime.Now;
    
        _loggedInTime = Convert.ToDateTime(Session["LoggedInTime"]);
    
        TimeSpan elapsedtimespan = DateTime.Now.Subtract(_loggedInTime);
        int elapsedtime = Convert.ToInt32(elapsedtimespan.TotalSeconds);
    }
    

    I’m guessing that you are calculating the elapsed time at another time and not in the Page_Load as in the above example.

    Make sure that on each post back you correctly load the elapsed time from the session before calculating the elapsed time. On the next post back the _loggedInTime is reset to the default value of a DateTime, being {1/1/0001 12:00:00 AM}.

    I think you have something to the following setup.

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["LoggedInTime"] == null)
                Session["LoggedInTime"] = DateTime.Now;
            _loggedInTime = Convert.ToDateTime(Session["LoggedInTime"]);
        }
    }
    
    private void ButtonClick(object sender, ImageClickEventArgs e)
    {
        TimeSpan elapsedtimespan = DateTime.Now.Subtract(_loggedInTime);
        int elapsedtime = Convert.ToInt32(elapsedtimespan.TotalSeconds);
    }
    

    Here I demonstrate it by handling a postback when a button is clicked. In that case the Page_Load does not load the LoggedInTime and the elapsed time is calculated incorrectly. To solve this, just remove the IsPostBack if statement in the Page_Load. Make sure you set the instance variable _loggedInTime each time you load the page, thus also on a postback.

    Remark: Also check if you are on a server farm. If you are using multiple servers to handle your requests but have configured the wrong session mode (e.g. in process) then server A will store the session variable in its memory, but the redirect can be handled by server B, which doesn’t know about server A’s in-memory session store.

    More information can be found on MSDN:

    Session-State Modes

    In process session state is the default, in a server farm scenario you can use the StateServer or SqlServer alternatives to share session state between the servers. Or you can write your own custom session state provider.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DateTime? field. Its currently storing UT. I would like it to
I want to add a date in session (date1) like this: Session[DateLesson] = date1.ToString(dd.MM.yyyy);
SQLite docs specifies that the preferred format for storing datetime values in the DB
My object, contains a queue which stores DateTime objects. Storing objects with just the
i want to convert this string into DateTime. Tue Aug 19 15:05:05 +0000 2008
I want to compare datetime value and timespan value for the reason of non-negative
I am trying to pass a vbscript session variable to my c# page. Upon
Here's an example of SetCulture attribute which inside does something like this: public void
I am using the crystal report, in that i am using code like below
I have a DB which looks like this: 1st: CommissionsV2 (Table = Entity_Product_Point) This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.