I'm writing Python and I'm not a Python person.

I have a need to get a Unix timestamp of a time that is an arbitrary number of days ago.

In PowerShell, you can do something like this:

PS > Get-Date -Date (Get-Date).AddDays(-30) -UFormat %s

In Python, this is the best I came up with and it is ugly:

import re
import datetime

`int(re.split('\\.',str((datetime.datetime.now(datetime.UTC) - datetime.timedelta(days=30)).timestamp()))[0])`

That was from me reading module documentation and hacking away at it for a bit.

Surely, there is a better way.

I'm interfacing with an API that only takes datetime in Unix Time format and only down to the second in precision.

#Python #DateTime #UnixTime #EpochTime

@jrdepriest the str/re.split is redundant and a bit silly; you can round off the non-integer part of the timestamp by simply calling int() on it directly if that’s what you’re trying to do

@glyph @jrdepriest yip, pretty much that:

int((datetime.datetime.now(datetime.UTC) - datetime.timedelta(days=30)).timestamp())