0

I'm trying to get always an specific time of a specific timezone

e.g: Sao Paulo(-3)

I've tried this code that I found searching here on stackoverflow:

// function to calculate local time
// in a different city
// given the city's UTC offset
function calcTime(city, offset) {

    // create Date object for current location
    d = new Date();

    // convert to msec
    // add local time zone offset 
    // get UTC time in msec
    utc = d.getTime() + (d.getTimezoneOffset() * 60000);

// create new Date object for different city
// using supplied offset
nd = new Date(utc + (3600000*offset));

// return time as a string
return "The local time in " + city + " is " + nd.toLocaleString();

}

// get Sao Paulo time
Ti.API.info(calcTime('Sao Paulo', '-3'));

on my Ti.API.info() I get this as response: The local time in Sao Paulo is August 6, 2013, 3:31:52 PM GMT-03:00... There's a way to get only 3:31:52 ? Besides... as this function says... it get always the local time and them show what's the time on that timezone... BUT someone knows how can I get always Sao Paulo's time whitout giving the local time ? So if the user change their time on phone it won't change the Sao Paulo's time.

Thanks

DHennrich
  • 45
  • 8

2 Answers2

1

use momentjs specifically moment timezone

http://momentjs.com/timezone/docs/

if you are using Alloy, momentjs is included by default, if not you will need to download the library.

Aaron Saunders
  • 33,180
  • 5
  • 60
  • 80
0

If you are just talking about JavaScript, then please see this answer. However, my guess is that Titanium has its own way of mapping the native iOS or Android time zone APIs into JavaScript. If not, it should. I don't know enough about Titanium to answer on this point further.

Do keep in mind that a time zone cannot be represented by just an offset. See "Time Zone != Offset" in the timezone tag wiki.

In particular, Sao Paulo is represented by the America/Sao_Paulo time zone in the IANA time zone database. This zone alternates between -3 and -2, as shown here. You cannot simply pass in -3 and expect it to know what to do.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • I read what you give me but I'm completely lost... What I'm trying to do is: The user set the time to 15min to make a function... them it start to counting. when the time reach this 15 min it fire a function but only after the 15min setted... but my problem is if the user change their phone time, he won't need to wait 15min to this function be fired. My problem is how can I prevent this way of circumventing from the user? I guess it can only be done if the app make a request to an website and check the time right? But there's another way to do that? A offline way Thanks for your help @Matt – DHennrich Aug 06 '13 at 19:33
  • If you can guarantee your app will be running the whole time, just use [`setTimeout()`](http://www.w3schools.com/jsref/met_win_settimeout.asp). Another way would be to get the current UTC time and add 15 minutes to that. There's no reason to involve time zones. – Matt Johnson-Pint Aug 06 '13 at 20:43
  • My app won't be running the whole time so I can't use setTimeout(). But if I get the current UTC and them start to reach current UTC + 15 mins, when the user leave the app, the current UTC that will counting to reach that 15 min will be saved so I can check the time after when the user enter in app again... BUT if the user change their phone's time, it will mess up with my storeged time right? Because I believe that the app will assume that the 15min already pass I'm right? – DHennrich Aug 06 '13 at 22:28
  • Right. You can't prevent the users from changing their clock. there's no way to stop that (AFAIK). But at least with UTC, you don't have to muck with time zones. And if they change their zone, it won't affect you. – Matt Johnson-Pint Aug 06 '13 at 22:33
  • Using moment (like Aaron suggested), get the expiration time with `var expiration = moment().add('minutes',15).toISOString();`. Then check for it with `moment().isAfter(expiration)`. – Matt Johnson-Pint Aug 06 '13 at 22:39
  • I tried here and get a big list with errors so I don't know what's wrong... but this code won't work too... because when I use `moment()` is the same thing that I do `new Date()`... so if the user change their time, on my checking it will get the new time as `new Date()` and will compare with the old... same problem =/ hahaha I guess is more right make a request to an php that will get the time from a website and them compare to the user's time. Do you know any website that allow this ? Thanks – DHennrich Aug 07 '13 at 01:15
  • When you call `.toISOString()`, the UTC representation is captured in the string. Also, we've deviated quite far from your original question, so if you need additional help, please ask in a new question and show what does or doesn't work. – Matt Johnson-Pint Aug 07 '13 at 01:19