[Python] How can I get the current time (now) in UTC?

Discussão em 'Python' iniciado por Stack, Novembro 27, 2024.

  1. Stack

    Stack Membro Participativo

    I have a python datetime object (representing five minutes from now) which I would like to convert to UTC. I am planning to output it in RFC 2822 format to put in an HTTP header, but I am not sure if that matters for this question. I found some information on this site about converting time objects, and it looks simpler that way, but this time I really want to use datetime objects, because I am using timedeltas to adjust them:

    I tried something like this:

    from datetime import datetime, timedelta

    now = datetime.now()
    fiveMinutesLater = datetime.now() + timedelta(minutes=5)
    fiveMinutesLaterUtc = ???


    Nothing in the time or datetime module looks like it would help me. It seems like I may be able to do it by passing the datetime object through 3 or 4 functions, but I am wondering if there is a simpler way.

    I would prefer not to use third-party modules, but I may if it is the only reasonable choice.

    Continue reading...

Compartilhe esta Página