Rajiv Bhalla
Rajiv Bhalla

Reputation: 11

Get time in GMT in a specific format

I want a time in the below-mentioned format, using Python.

Tue, 08 Nov 2022 15:35:20 GMT
  1. I should be able to get the current time in above format;
  2. Then I should be able to add days in it and get the date and time in the same above-mentioned format (for example, I want date falling on after n number of days).

Any help would be highly appreciated.

I have tried the below code but not getting the desired results:

start_date = str(datetime.now()).split(".")[0]
due_date = str((datetime.now() + timedelta(days=2))).split(".")[0]

Output:

2022-11-08 23:45:15
2022-11-10 23:45:15 

Upvotes: 0

Views: 57

Answers (1)

Mateus Coelho
Mateus Coelho

Reputation: 50

You can try to use the below functions:

from datetime import datetime, timedelta

def get_now():
    return datetime.now().strftime('%a, %d %b %Y %H:%M:%S GMT')

def n_days_from_now(n):
    now = get_now()
    return (datetime.now() + timedelta(days = n)).strftime('%a, %d %b %Y %H:%M:%S GMT')

Upvotes: -1

Related Questions