0

Actually,I'm developing a Clinic Management System where in one screen,I need to show Today's Patients which are added in the Queue.
For this : I've called DateTime.Now;
Actually My host server is in Canada Location and I'm in Malaysia.So,there is a 12 hour gap between both and the records are not showing until 12 pm.
While googling for the solution,I came across a solution through JavaScript.

 <label id="lblTime" runat="server" style="font-weight: bold"></label> 

 <script type="text/javascript">
        $(document).ready(function () {
            ShowTime();
        });

        function ShowTime() {
            var dt = new Date();
            document.getElementById("lblTime").innerHTML = dt.toLocaleTimeString();
            window.setTimeout("ShowTime()", 1000); // Here 1000(milliseconds) means one 1 Sec  
        }
    </script>

Actually,It is showing time but when I get the text on the code behind,it is giving me an empty string.

string strrr = lblTime.InnerHtml.ToString();

Can anyone guide me through this? Am I doing any Mistake? Is there any other way to do this?

Actually,I need the Date&Time in !isPostback of Page_load

RealSteel
  • 1,871
  • 3
  • 37
  • 74
  • `new Date()` in JavaScript will use your *browser's* clock and local time zone. `DateTime.Now` in .NET will use your *server's* clock and local time zone. What are you actually asking here? – Matt Johnson-Pint Mar 21 '14 at 02:59
  • actually,I need the ClientTime on Server Side,So that i can show him today's records based on his time – RealSteel Mar 21 '14 at 03:02
  • 1
    How can you be certain that his clock is set correctly? What you probably want instead is the *server time* but converted to the client's time zone. – Matt Johnson-Pint Mar 21 '14 at 03:36
  • 1
    There are actually a lot of questions you'll have to answer to proceed. For example, you say "I need to show *today's* patients". Well, who's day do you mean? Also, you say "my host server is in Canada", but if you do things properly, you should not be calling `DateTime.Now`, but rather `DateTime.UtcNow` and therefore the host server time zone becomes irrelevant. There are many other concerns besides just the bit of javascript you've got here. – Matt Johnson-Pint Mar 21 '14 at 03:38
  • yes.you are correct.There is a chance of client setting wrong time also. actually,I need to get to get the server time and convert it based on ClientTimeZone. can you please help me? – RealSteel Mar 21 '14 at 03:43
  • There is an another post talk about this extensively http://stackoverflow.com/questions/274826/how-to-get-client-date-and-time-in-asp-net?rq=1 – Senthil Mar 21 '14 at 03:45
  • @MattJohnson : thank you very much for letting me think :) – RealSteel Mar 21 '14 at 07:44

3 Answers3

0

ShowTime() is going to execute AFTER Page_Load. Your current design isn't going to work.

You could use jQuery ajax to call a code behind method and pass the client date/time to it.

Look at this article: http://coderskey.blogspot.com/2013/04/how-to-call-code-behind-function-using.html

Here's a code snipit. You will want to "SetCurrentTime" instead.

function ShowCurrentTime() {
$.ajax({
    type: "POST",
    url: "CS.aspx/GetCurrentTime",
    data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: OnSuccess,
    failure: function(response) {
        alert(response.d);
    }
});

}

function OnSuccess(response) {
    alert(response.d);
}
Rick S
  • 6,476
  • 5
  • 29
  • 43
0

You probably want to pass a date and time string back to the server that is either UTC or the local time with the local timezone offset. For the former, use the toISOString method, which returns a UTC date and time in ISO 8601 format:

var now = new Date();

var UTCString = now.toISOString(); // 2014-03-21T03:28:52.531Z

for the latter, you will have to build the string yourself:

function toLocalISOString(d) {

  function pad(n){
    var sign = n < 0? '-' : '';
    n = Math.abs(n);
    return sign + (n<10? '0' : '') + n;
  }

  function minsToHHMM(n){
    var hh = pad(n/60 |0);
    var mm = pad(Math.abs(n%60));
    return  hh + '' + mm;
  }

  var offset = minsToHHMM(d.getTimezoneOffset() * -1);

  return d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate()) +
         'T' + pad(d.getHours()) + ':' + pad(d.getMinutes()) + ':' + pad(d.getSeconds()) +
         (offset<0?'-' : '+') + offset;
}

toLocalISOString(now); // 2014-03-21T14:28:52+1100

Note that timezone offsets in javascript are in minutes and have the opposite sign to usual offsets, e.g. UTC1000 is -600, UTC-0530 is 330. The above ignores milliseconds, they can be included if required as decimal seconds.

Other formats can be accommodated too.

RobG
  • 142,382
  • 31
  • 172
  • 209
0

I think I found the answer.
I followed the Link : Here
But Unable to get the Date&Time in Page_load.
something like Ajax Calls need to be Done for a PostBack.
I'm Planning to take a Timer and then postback the Page immediately after the !ispostback event and then I will make the Timer Enabled False.
Can anyone suggest a better solution than this?

RealSteel
  • 1,871
  • 3
  • 37
  • 74