Get Time of Specific Timezone

Get time of specific timezone

If you know the UTC offset then you can pass it and get the time using the following function:

function calcTime(city, offset) {
// create Date object for current location
var d = new Date();

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

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

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

alert(calcTime('Bombay', '+5.5'));

Taken from: Convert Local Time to Another

Get date time for a specific time zone using JavaScript

var offset = -8;
new Date( new Date().getTime() + offset * 3600 * 1000).toUTCString().replace( / GMT$/, "" )

"Wed, 20 Jun 2012 08:55:20"

<script>  var offset = -8;
document.write( new Date( new Date().getTime() + offset * 3600 * 1000 ).toUTCString().replace( / GMT$/, "" ) );</script>

PHP - get current time in specific time zone

As @Karthik said, you can grab the timezone by using the DateTimezone object.

Here's a link to the docs.

Example:

$tz = 'America/New_York';
$tz_obj = new DateTimeZone($tz);
$today = new DateTime("now", $tz_obj);
$today_formatted = $today->format('Y-m-d');
$directory = "comics";

$query = "
SELECT *
FROM comics
WHERE story = ?
AND `date` <= ?
ORDER BY `date` DESC, id DESC
LIMIT 1"

$prepped = $conn->prepare($query);
$prepped->bind_param('ss', $directory, $today_formatted);

Convert Date/Time for given Timezone - java

For me, the simplest way to do that is:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

//Here you say to java the initial timezone. This is the secret
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
//Will print in UTC
System.out.println(sdf.format(calendar.getTime()));

//Here you set to your timezone
sdf.setTimeZone(TimeZone.getDefault());
//Will print on your default Timezone
System.out.println(sdf.format(calendar.getTime()));

Momentjs timezone - getting date at time in specific timezone

Yes, 1448841600000 is the date you said:

moment(1448841600000).utc().format()
// "2015-11-30T00:00:00+00:00"

But that is a day earlier in Pacific time

moment(1448841600000).tz('America/Los_Angeles').format()
// "2015-11-29T16:00:00-08:00"

When you adjust it to 9:30 pacific, it's on the 29th, not the 30th.

moment(1448841600000).tz('America/Los_Angeles').hour(9).minute(30).format()
// "2015-11-29T09:30:00-08:00"

When you call valueOf, the result is:

moment(1448841600000).tz('America/Los_Angeles').hour(9).minute(30).valueOf()
// 1448818200000

This is the correct value, however it's different than the one you provided. However, it is indeed what I get when I run your code as well.

Screenshot from Chrome debug window, with your exact code:

screenshot

Also, in comments you wrote:

//moment("2015-11-30"); //monday 11/30 in UTC

Actually, that would be in local time, not UTC. If you wanted UTC, you'd use:

moment.utc("2015-11-30")

Though it's unclear to me whether you are using this string input or the numeric timestamp.

If what you are asking is that you want the UTC date to be treated as if it were local and then have an arbitrary local time applied - that is a somewhat strange operation, but it would go something like this:

var tempDate = moment.utc(1448841600000);
var adjustedStart = moment.tz([tempDate.year(), tempDate.month(), tempDate.date(), 9, 30],
"America/Los_Angeles");
console.log("adjustedStart in milliseconds:" + adjustedStart.valueOf());
// adjustedStart in milliseconds:1448904600000

This gives the value you asked for, but to me - this is a smell that something is wrong with the expectation. I'd look a lot closer at the requirement and the other parts of the system.

Retrieve date and convert it to specific time zone according to user time zone

$date = new DateTime($result->s_start, new DateTimeZone($result->s_timezone));
$date->setTimezone(new DateTimeZone('Africa/Cairo'));
echo $date->format('Y-m-d H:i:sP') ;

Resources

  • DateTime class
  • DateTimeZone class

Edit A function in the controller to convert timezones.

public function _convert_time($result){
$date = new DateTime($result->s_start, new DateTimeZone($result->s_timezone));
$date->setTimezone(new DateTimeZone('Africa/Cairo'));
return $date->format('Y-m-d H:i:sP') ;
}

Now you can echo the result

foreach($results as $result){
echo $this->_convert_time($result);
}

How to get current time in a specific timezone?

It might be tricky without an external library, so i suggest you use the pytz package

pip install pytz

And with help from here you could try something like below

from datetime import datetime
from pytz import timezone

now_time = datetime.now(timezone('America/Chicago'))
print(now_time.strftime('%I:%M:%S %p'))

I used America/Chicago because it's in the CDT timezone according to this.

But if you are interested in doing it natively you will have to read up some more here in the official documentation because it provide some examples on how to do it but it will leave kicking and screaming especially if you are a beginner.



Related Topics



Leave a reply



Submit