How to format pubDate with Python

I am new to Python and I am trying to do the following.

In PHP, if we want to convert a date, we use something like this:

$item_date = date("Ymj G:i:s", strtotime($RSSitem->pubDate)); 

Now I'm trying to do the same with Python, but I cannot figure out the exact method to execute it.

Can someone help me by writing this line in python?

+4
source share
2 answers

There are two parts:

  • convert pubDate from string to datetime / time object. If the pubDate format is fixed, you can use the datetime.strptime() function, otherwise you could use dateutil.parser.parse() or feeparser._parse_date() (later it might be easier to set).
  • Convert a datetime / time object to a string using the .strftime() / time.strftime() method.

See the behavior of strftime () and strptime () .

+3
source

According to the RSS specification pubDate must follow RFC822 :

 mytime.strftime("%a, %d %b %Y %H:%M:%S %z") 
+10
source

Source: https://habr.com/ru/post/1432355/


All Articles