-2

I am scraping web series data from JSON and lists using python. the problem is with date and time. I got a function to convert the duration format

def get_duration(secs):
    hour = int(secs/3600)
    secs = secs - hour*3600
    minute = int(secs/60)
    if hour == 0:
        return "00:" + str(minute)
    elif len(str(hour)) == 1:
        return "0" + str(hour) + ":" + str(minute)
    return str(hour) + ":" + str(minute)

The output is 00:29 which is correct.

But how can I convert broadCastDate to DD-MM-YYYY format

Here is the Data

"items": [
   {
     "duration": 1778,
     "startDate": 1555893000,
     "endDate": 4102424940,
     "broadCastDate": 1555939800,
   }
]

The Broadcast Date on the website is 22 Apr 2019, so need a function to get this type of output.

Vardhan
  • 13
  • 4

2 Answers2

2

That value looks like a timestamp so you could use datetime.fromtimestamp to convert to a datetime object and then strftime to output in the format you want:

from datetime import datetime

d = datetime.fromtimestamp(1555939800)
print(d.strftime('%d-%m-%Y'))

Output:

22-04-2019
Nick
  • 138,499
  • 22
  • 57
  • 95
1

The original question is slightly unclear about the final desired format. If you would like "22 Apr 2019" then the required format string is "%d %b %Y"

from datetime import datetime

d = datetime.fromtimestamp(1555939800)
print(d.strftime('%d %b %Y'))

Output:

22 Apr 2019
Russel
  • 41
  • 3