Get client time with out javascript and posts back

I need to know the time of the client. My course of action is to keep the offset in the cookie, and then calculate. My problem is I need to insert the cookie yet, even while loading the first page. I know there are a lot of methods but no one answers the need. I need to use local time before the first page is loaded. So I can not use JavaScript.

I tried to send the user back to the client in the post, put the cookie and then return it to the server, but it is problematic for google, because they do not have cookie.

Here is the function:

public static DateTime? GetClientTime()
    {
        HttpRequest Request = HttpContext.Current.Request;
        if (Request.Cookies["DynoOffset"] != null)
        {
            string strOffset = Request.Cookies["DynoOffset"].Value;
            int offset = int.Parse(strOffset);
            TimeZone localZone = TimeZone.CurrentTimeZone;
            DateTime currentDate = DateTime.Now;
            DateTime CreationDate = localZone.ToUniversalTime(currentDate).AddMinutes(-offset);
            return CreationDate;
        }
        else
        {
            StoreClientTime();
            return null;
        }
    }

    public static DateTime? StoreClientTime()
    {
        var Context = HttpContext.Current;
        var Session = Context.Session;
        var Response = Context.Response;
        var Request = Context.Request;
        // if the local time is not saved yet in Session and the request has not posted the localTime
        if (Request.Cookies["DynoOffset"] == null && String.IsNullOrEmpty(Request.Params["localTime"]))
        {
            // then clear the content and write some html a javascript code which submit the local time
            Response.ClearContent();
            Response.Write("<form id='local' method='post' name='local'>" +
                "<script src=\"/Js/jquery-1.7.1.min.js\" type=\"text/javascript\"></script>" +
                "<script src=\"/Js/JqueryUI/jquery.cookie.js\" type=\"text/javascript\"></script>" +
                "<script type=\"text/javascript\">" +
                    "$.cookie(\"DynoOffset\", new Date().getTimezoneOffset(), { expires: 150 });" +
                    "$(\"#local\").submit()" +
                "</script>" +
                "</form>");
            // 
            Response.Flush();

            // end the response so PageLoad, PagePreRender etc won't be executed
            Response.End();
            return null;
        }
        else
        {
            return GetClientTime().Value;
        }
    }

I thought to find the offset based on the calture, but I do not know how to do it.

Answers


A few things:

  • You are doing too much work to get at the UTC time. Just use DateTime.UtcNow.
  • Scripting postbacks are a thing of the past. You've already shown you're using jquery, so just do an ajax post to send it to the server. That will solve your google issue as well.
  • If your first page needs that, then send the UTC time and convert it on the client - or do an ajax get to retrieve it.
  • Keep in mind that the user can set their clock to whatever timezone they want, and that the offsets can and will change for many users because of daylight savings time / summer time. If you keep their offset in a permanent cookie, you will have the wrong time when they come back after the change. Make sure it's in a temporary cookie, and it's probably something you will want to reset often.
  • You say that you are processing data using the client's local time? Can you elaborate on what for? That is a very dangerous thing to do since local times can be ambiguous. You should probably be processing based on UTC. If you need the client's offset while processing, you should use a DateTimeOffset on the server. Review here.

Need Your Help

In WPF setting e.Effects in DragOver doesn't work - why?

c# wpf xaml treeview drag-and-drop

I am new in WPF. I am implementing Drag&amp;Drop in a TreeView and my DragOver event handler does not work.

c# WPF and WCF mapping problem

c# wpf wcf wcfserviceclient

I'm actually trying to create a wpf project witch connected to the WCF via services references, I'm also using the MVVM pattern in my wpf project and I have a problem when I declare in my CTOR of my

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.