Home > Back-end >  web scraping convert unix timestamp to date format
web scraping convert unix timestamp to date format

Time:06-22

I'm trying to webscrape a flight data website using beautifulsoup in python but the timestamp is in unix timestamp how can i convert to regular datetime format. There are several such columns to be converted.

#scheduled_departure 
result_items[0]['flight']['time']['scheduled']['departure']

and the output is shown as 1655781000. how can I convert it to Tue, Jun 21, 2022 8:40 AM

CodePudding user response:

import time


print(time.strftime("%a, %b %d, %Y %H:%M %p", time.localtime(1655781000)))

CodePudding user response:

There is only one Unix time and it is created by using the UTC/GMT time zone. This means you might have convert time zones to calculate timestamps.

import datetime
from pytz import timezone

local_datetime = datetime.datetime.fromtimestamp(1655781000)
local_time_str = datetime.datetime.strftime(local_datetime, "%a, %d %b %Y %H:%M:%S %p")
print(f'Local time: {local_time_str}')

other_timezone = 'Asia/Kolkata'  # Replace your interest timezone here
remote_datetime = local_datetime.astimezone(timezone(other_timezone))
remote_time_str = datetime.datetime.strftime(remote_datetime, "%a, %d %b %Y %H:%M:%S %p")
print(f'Time at {other_timezone }: {remote_time_str}')
  • Related