Skip to content

Instantly share code, notes, and snippets.

@vladwa
Created December 31, 2017 07:38
Show Gist options
  • Save vladwa/8cd97099e32c1088025dfaca5f1bfd33 to your computer and use it in GitHub Desktop.
Save vladwa/8cd97099e32c1088025dfaca5f1bfd33 to your computer and use it in GitHub Desktop.
Python snippet to get the PST time and date.
from datetime import datetime
from pytz import timezone
def get_pst_time():
date_format='%m_%d_%Y_%H_%M_%S_%Z'
date = datetime.now(tz=pytz.utc)
date = date.astimezone(timezone('US/Pacific'))
pstDateTime=date.strftime(date_format)
return pstDateTime
@matiasherranz
Copy link

Fails with missing pytz import. This fixes it:

from datetime import datetime
from pytz import timezone, utc

def get_pst_time():
    date_format='%m_%d_%Y_%H_%M_%S_%Z'
    date = datetime.now(tz=utc)
    date = date.astimezone(timezone('US/Pacific'))
    pstDateTime=date.strftime(date_format)
    return pstDateTime

Thanks for sharing the snippet! :-)

Copy link

ghost commented Sep 7, 2020

after import pytz not show the time so change return function print function then saw the time

from datetime import datetime
from pytz import timezone
import pytz

def get_pst_time():
date_format='%m_%d_%Y_%H_%M_%S_%Z'
date = datetime.now(tz=pytz.utc)
date = date.astimezone(timezone('US/Pacific'))
pstDateTime=date.strftime(date_format)
print(pstDateTime)

Thankyou

@abcjjy
Copy link

abcjjy commented Sep 16, 2021

Pacific time is different from PST. It switches to PST or PDT according to date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment