1

I am trying to create a program that outputs the day of the week in another country using python. The furthest I get is to output the current day of the week in my own country. But for example, I wan to print what day it is in Tokyo. Lets say today is Thursday in my country and in Tokyo it is Friday.

My question is: is there a simple way to find out what day of the week it is in another country?

Thanks in advance

FpRoT
  • 63
  • 1
  • 2
  • 12
  • 4
    possible duplicate of [Change Timezone for Date object Python](http://stackoverflow.com/questions/27221132/change-timezone-for-date-object-python) – ILostMySpoon May 07 '15 at 17:05
  • 1
    In countries that have more than one time-zone, different parts of the country can be in different days. Probably there aren't any cities that cross time-zones. – cphlewis May 07 '15 at 17:23
  • Could you post what you have so far? – nivix zixer May 07 '15 at 17:29
  • @cphlewis: people living on the same street may have a different idea what date it is (e.g., due to religious shenanigans). – jfs May 09 '15 at 10:03
  • @ILostMySpoon: it is not a duplicate. I see nothing in the answers that help to find Tokyo's timezone. – jfs May 09 '15 at 10:16

1 Answers1

4

To find out the correct date, you need to know the correct timezone. There could be several timezones in the same country. There are databases that allows you to get the timezone from the name of the city e.g., see Get Timezone from City in Python/Django:

#!/usr/bin/env python
from geopy import geocoders # $ pip  install geopy

g = geocoders.GoogleV3()
place, (lat, lng) = g.geocode('Tokyo')
timezone = g.timezone((lat, lng)) # return pytz timezone object
print(timezone.zone)

Once you know the timezone, you could use a solution from Change Timezone for Date object Python mentioned by @ILostMySpoon:

#!/usr/bin/env python
import calendar
from datetime import datetime
import pytz # $ pip install pytz

now = datetime.now(pytz.timezone('Asia/Tokyo')) # you could pass `timezone` object here
weekday = now.weekday() 
print(calendar.day_name[weekday])
Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670