最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it possible to determine the users time from a web request? - Stack Overflow

programmeradmin1浏览0评论

Is it possible on a web server to determine the users local time and timezone when they sent the request?

Could it be done using javascript to capture it and post it back to the server?

My pany want to track how many users use our site outside of office hours (no I dont really know why either!).

Thanks.

Is it possible on a web server to determine the users local time and timezone when they sent the request?

Could it be done using javascript to capture it and post it back to the server?

My pany want to track how many users use our site outside of office hours (no I dont really know why either!).

Thanks.

Share Improve this question asked Feb 3, 2009 at 17:18 Simon KeepSimon Keep 10k11 gold badges64 silver badges79 bronze badges 1
  • Thanks everyone, I realise that its dependant on the user having their clock set correctly. – Simon Keep Commented Feb 3, 2009 at 17:27
Add a ment  | 

6 Answers 6

Reset to default 6

You need to put the client's JavaScript time into a hidden field, and assume their clock is set properly.

myTime = new Date() >> Tue Feb 03 2009 12:24:28 GMT-0500 (Eastern Standard Time)

You could load a date into a hidden field using JavaScript:

var newDate = new Date();
hiddenFieldName.value = newDate.toLocaleString();

Then on the postback to the sever grab that field.

You should consider reverse DNS resolution of your existing web server logs. You can then use geocoding to guess user's time zone (and their local time relative to your web server's time).

You would not need to write new JavaScript code, write custom server-side code to log user times, and then deploy an updated website.

This Perl script might be a helpful example: Perl Script that Does Bulk Reverse-DNS Lookups

JavaScript is indeed the way to go.

Fortunately the web has lots of sample code you can use. This page seems pretty prehensive.

It should be noted that knowing someone's current offset is not the same as knowing their time zone, but it's probably enough for your purposes :)

Certainly you could embed a JS derived client timestamp and send it back appended to some other request, but it's pletely unreliable since the user could set their client time to anything they want, and it requires that second request which could be slightly abusive, or technically awkward to ensure. A lot of implementations will do this by fire-and-forgetting an AJAX request for a text file, or adding a single blank pixel image to the page.

You could try and derive a similar metric based on first requests and geolocating an IP - location and GMT timestamp giving you a local time - but that might be expensive if you haven't got an IP/Geo database already :)

Well you could read the browser's local time with JavaScript:

var now = new Date();

Then you could post it to the server using XmlHttpRequest or a hidden form field...

发布评论

评论列表(0)

  1. 暂无评论