How to detect user's timezone?
To summarize Matt Johnson's answer in terms of code:
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js">
</script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.4/jstz.min.js">
</script>
<script type="text/javascript">
$(document).ready(function(){
var tz = jstz.determine(); // Determines the time zone of the browser client
var timezone = tz.name(); //For e.g.:"Asia/Kolkata" for the Indian Time.
$.post("url-to-function-that-handles-time-zone", {tz: timezone}, function(data) {
//Preocess the timezone in the controller function and get
//the confirmation value here. On success, refresh the page.
});
});
</script>
How to automatically detect user's timezone?
Use javascript solution
http://www.onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/
Demo should show your timezone in select box.
http://onlineaspect.com/examples/timezone/index.html (Dead link)
get client time zone from browser
Look at this repository pageloom it is helpful
download jstz.min.js and add a function to your html page
<script language="javascript">
function getTimezoneName() {
timezone = jstz.determine()
return timezone.name();
}
</script>
and call this function from your display tag
Getting the client's time zone (and offset) in JavaScript
Using getTimezoneOffset()
You can get the time zone offset in minutes like this:
var offset = new Date().getTimezoneOffset();
console.log(offset);
// if offset equals -60 then the time zone offset is UTC+01
How to get user's local timezone other than server timezone(UTC) in python?
Got it.
from datetime import datetime
from pytz import timezone
fmt = "%Y-%m-%d %H:%M:%S"
# Current time in UTC
now_utc = datetime.now(timezone('UTC'))
print now_utc.strftime(fmt)
# Convert to US/Pacific time zone
now_pacific = now_utc.astimezone(timezone('US/Pacific'))
print now_pacific.strftime(fmt)
# Convert to Europe/Berlin time zone
now_berlin = now_pacific.astimezone(timezone('Europe/Berlin'))
print now_berlin.strftime(fmt)
Courtesy: http://www.saltycrane.com/blog/2009/05/converting-time-zones-datetime-objects-python/
get user timezone
This will get you the timezone as a PHP variable.
I wrote a function using jQuery and PHP. This is tested, and does work!
On the PHP page where you are want to have the timezone as a variable, have this snippet of code somewhere near the top of the page:
<?php
session_start();
$timezone = $_SESSION['time'];
?>
This will read the session variable "time", which we are now about to create.
On the same page, in the <head>
section, first of all you need to include jQuery:
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
Also in the <head>
section, paste this jQuery:
<script type="text/javascript">
$(document).ready(function() {
if("<?php echo $timezone; ?>".length==0){
var visitortime = new Date();
var visitortimezone = "GMT " + -visitortime.getTimezoneOffset()/60;
$.ajax({
type: "GET",
url: "http://example.com/timezone.php",
data: 'time='+ visitortimezone,
success: function(){
location.reload();
}
});
}
});
</script>
You may or may not have noticed, but you need to change the url to your actual domain.
One last thing. You are probably wondering what the heck timezone.php is. Well, it is simply this:
(create a new file called timezone.php and point to it with the above url)
<?php
session_start();
$_SESSION['time'] = $_GET['time'];
?>
If this works correctly, it will first load the page, execute the JavaScript, and reload the page. You will then be able to read the $timezone variable and use it to your pleasure! It returns the current UTC/GMT time zone offset (GMT -7) or whatever timezone you are in.
You can read more about this on my blog
Get timezone from users browser using moment(timezone).js
var timedifference = new Date().getTimezoneOffset();
This returns the difference from the clients timezone from UTC time.
You can then play around with it as you like.
Related Topics
Html - How to Get My Text Next to My Icon
Css Height 100% Is Not Always Equal to the Direct Parent
How to Prevent Desktop Browser (Chrome, Safari) from Zooming a Webpage
Angular, Image Not Found (Get 404)
How to Make All Images of Different Height and Width the Same Via CSS
How to Clear Input Field After Submitting Data in Angular
Limit the Size of a File Upload (Html Input Element)
Div Elements Overlapping Each Other
Selecting an Option in a Dropdown List Using Request Parameters
Inserting an Image from Local Directory in Thymeleaf Spring Framework (With Maven)
How to Position Elements Next to Each Other in a Div
How to Get Angular to Reload the Same Page But With a Different Argument
Table With Fixed Header and Fixed Column on Pure CSS
Item Moving Around When Window Resized
How to Force Position Absolute With 100% Width to Fit into Parent Div With Padding
File Input 'Accept' Attribute - Is It Useful