-2

I have an application where I need to display the User profile. Now where ever the user is from India or may be Brazil. I have to display the local time of that particular location. But the user who is viewing the user profile, can brows the profile from any where in World. How can I achieve it using java script. Help will be appreciated much.

Marc Audet
  • 46,011
  • 11
  • 63
  • 83
  • The only way to do that is to store the timezone offset for the user in their profile, then apply it to the current time of the viewer. There will be issues with daylight saving though. – RobG Jul 19 '13 at 03:18

2 Answers2

0

You can use the Date object

function getTime () {
    var today=new Date();
    var hours = today.getHours() > 12 ? today.getHours() - 12 : today.getHours();
    var amORpm = hours > 12 ? "pm" : "am";
    var minutes =today.getMinutes();
    return hours + ":" + minutes + amORpm;
}

I don't have time to test this but it should work.

Edit:

You can't exactly set the time zone with JavaScript. However, there are ways around it with some more JavaScript. I suggest looking into the moment.js library. I believe it allows you to do what I want. You can also just use a Server Based Language such as PHP or ASP.NET.

Community
  • 1
  • 1
Shawn31313
  • 5,978
  • 4
  • 38
  • 80
  • Thank you for the response. But i need to know the local time based on the searched user Home address City (like Mumbai). This method will take my system local time and will display that. – user2597764 Jul 19 '13 at 02:04
  • I think you missed the point. The OP wants to display the time at the location of the user whose profile is being viewed, not the current user's location. – RobG Jul 19 '13 at 03:17
0

You can get the current date and time of the viewer by creating a new Date object:

var userDate = new Date();

You can adjust that to show the date at time in some other location by either adding the viewer's timezone offset then subtracting the profile timezone offset, or adjusting the internal timevalue by subtracting the profile timezone offset.

However, you will need to account for daylight saving adjustments for profiles in those places that have them.

// Return date object for provided offset.
// Offset is the ECMAScript Date object offset 
// in minutes for a timezone, e.g. UTC+1000 is -600
function getTime(offsetInMinutes) {
    var d = new Date();
    d.setMinutes(d.getMinutes() + d.getTimezoneOffset() - offsetInMinutes);
    return d;
}

// Show time in Riyadh: UTC+0300 (offset -180)
alert(getTime(-180));
RobG
  • 142,382
  • 31
  • 172
  • 209
  • Hi Rob. Thanks for the response. but I do not know the Offset time of the viewed User. I just have the country code of the user. And this is for a SharePoint application, where I can only use JavaScript or J query. – user2597764 Jul 24 '13 at 13:43
  • Then you are in trouble. :-( Even the country code is not sufficient as most have multiple timezones. You need the geographical location, then access to a service that will tell you the timezone offset for that location (or an equivalent). You need the location reasonably precisely, since some places are in the same winter timezone but while some observe daylight saving, others don't, or start or stop daylight saving on different dates. – RobG Jul 24 '13 at 23:04
  • @user2597764—try [How to get a time zone from a location?](http://stackoverflow.com/questions/16086962/how-to-get-a-time-zone-from-a-location). – RobG Jul 24 '13 at 23:05