开发者

asp.net timestamp issue

开发者 https://www.devze.com 2023-03-31 18:37 出处:网络
This i my code for storing record information from asp.net 2.0 webform scmd.Connection = scon; //Connection string

This i my code for storing record information from asp.net 2.0 webform

         scmd.Connection = scon; //Connection string
        SqlParameter p = scmd.CreateParameter(); 
        recodName = txtrecordname.Text;  //form field
        todaysdate = DateTime.Parse(txtFrom.Text); 
        DateTime now = DateTime.UtcNow;
        AddParameters("@record", recodName);  //adding parameter to stored procedure
        AddParameters("@date", todaysdate);
        AddParameters("@timeinfo", now);
        scmd.CommandText = "sp_InsertRecord";
        scmd.CommandType = CommandType.StoredProcedure;
        scon.Open();
        int i=scmd.ExecuteNonQuery();
    if (i > 0)
        {
            result.Text = "Record Inserted开发者_StackOverflow中文版 RecordName : " + recodName; //Label displaying  recordinfo
            dateinfo.Text = "Record inserted on (TimeStamp Info) : " + now;  //label displaying time info when user inserted record
        }
        GridView1.DataBind();

This web application is hosted on server whose timezone is (UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi, Now user from another system access the application whose time zone is (UTC+01:00) West Central Africa, As you can see i have inserted 'datetime now as Utcnow' but when user view the record inserted it must be in its local datetimeformat

i.e dateinfo.Text = "TimeStamp Info : " + now; //This label should display local time info currently it displays server local time where the application is hosted

Thanxs for any help


When loading the DateTime from the Database (you said you store UTC there), you need to specifiy the Kind of this time explicitely:

DateTime dateTimeFromDatabase = LoadDateTimeValueFromDatabase();
DateTime utcDate = DateTime.SpecifyKind(dateTimeFromDatabase, DateTimeKind.Utc);

Then, as long as you have the Culture of the web site visitor set correctly, you can simply use .ToLocalTime() from the DateTime to display the Utc value as local time:

DateTime localTimeToDisplay = utcDate.ToLocalTime();

You then use the localTimeToDisplay variable for databinding and you're all set.

Update: To display the current time you can simply do:

 dateinfo.Text = "TimeStamp Info : " + DateTime.UtcNow.ToLocalTime();

As UtcNow already sets the Kind of DateTime to UTC.

You need to set the correct culture for the client, though. So you would need to do that either manually or set this in the web.config:

<system.web>
    <globalization culture="auto" />
<system.web>


Use javascript to get the current time in the client system and put that in a javascript variable. How to do it can be found here:
http://www.quackit.com/javascript/javascript_date_and_time_functions.cfm

Then assign the javascript variable value to a hidden field variable and access it in the server side. How to access javascript variable in code behind
http://codeasp.net/blogs/joydeep157/microsoft-net/81/accessing-javascript-variable-from-code-behind-and-accessing-code-behind-variable-from-javascript

Now you have the client time in your server variable.

0

精彩评论

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