如何将格式为“%d/%m/%Y”的字符串转换为时间戳?

"01/12/2011" -> 1322697600

>>> int(datetime.datetime.strptime('01/12/2011', '%d/%m/%Y').strftime("%s"))
1322683200

>>> import time
>>> import datetime
>>> s = "01/12/2011"
>>> time.mktime(datetime.datetime.strptime(s, "%d/%m/%Y").timetuple())
1322697600.0

首先,必须使用strptime类将字符串转换为struct_time格式。

然后使用mktime从那里得到你的浮点数。


答案也取决于你输入的日期和时区。如果你的日期是本地日期,那么你可以像katrielalex说的那样使用mktime() -只是我不明白为什么他使用datetime而不是这个更短的版本:

>>> time.mktime(time.strptime('01/12/2011', "%d/%m/%Y"))
1322694000.0

但是请注意,我的结果与他的结果不同,因为我可能在不同的TZ中(结果是无时区的UNIX时间戳)

现在如果输入日期已经是UTC,那么我认为正确的解决方案是:

>>> calendar.timegm(time.strptime('01/12/2011', '%d/%m/%Y'))
1322697600

将字符串转换为日期对象:

from datetime import date, datetime

date_string = "01/12/2011"
date_object = date(*map(int, reversed(date_string.split("/"))))
assert date_object == datetime.strptime(date_string, "%d/%m/%Y").date()

将date对象转换为POSIX时间戳的方法取决于时区。从转换datetime。在Python中,date到UTC时间戳:

date对象表示UTC的午夜 导入日历 Timestamp1 = calendar.timegm(utc_date.timetuple()) Timestamp2 = (utc_date.toordinal() - date(1970, 1,1).toordinal()) * 24*60*60 断言timestamp1 == timestamp2 Date对象表示本地时间的午夜 导入的时间 timestamp = time.mktime(local_date.timetuple()) 断言timestamp3 != timestamp1或(time.gmtime() == time.localtime())

时间戳是不同的,除非UTC的午夜和本地时间是相同的时间实例。


我使用ciso8601,它比datetime的strptime快62倍。

t = "01/12/2011"
ts = ciso8601.parse_datetime(t)
# to get time in seconds:
time.mktime(ts.timetuple())

你可以在这里了解更多。


我建议约会日期:

import dateutil.parser
dateutil.parser.parse("01/12/2011", dayfirst=True).timestamp()

使用datetime即可。时间戳(您的datetime实例),datetime实例包含时区信息,因此时间戳将是标准utc时间戳。如果您将datetime转换为timetuple,它将失去它的时区,因此结果将是错误的。 如果你想提供一个接口,你应该这样写: Int (datetime.timestamp(time_instance)) * 1000


很多答案都没有考虑到日期一开始就很天真

为了正确,您需要首先将初始日期设置为具有时区意识的datetime

import datetime
import pytz
# naive datetime
d = datetime.datetime.strptime('01/12/2011', '%d/%m/%Y')
>>> datetime.datetime(2011, 12, 1, 0, 0)

# add proper timezone
pst = pytz.timezone('America/Los_Angeles')
d = pst.localize(d)
>>> datetime.datetime(2011, 12, 1, 0, 0,
tzinfo=<DstTzInfo 'America/Los_Angeles' PST-1 day, 16:00:00 STD>)

# convert to UTC timezone
utc = pytz.UTC
d = d.astimezone(utc)
>>> datetime.datetime(2011, 12, 1, 8, 0, tzinfo=<UTC>)

# epoch is the beginning of time in the UTC timestamp world
epoch = datetime.datetime(1970,1,1,0,0,0,tzinfo=pytz.UTC)
>>> datetime.datetime(1970, 1, 1, 0, 0, tzinfo=<UTC>)

# get the total second difference
ts = (d - epoch).total_seconds()
>>> 1322726400.0

另外:

小心,在datetime中使用pytz for tzinfo。datetime不适用于许多时区。参见datetime with pytz timezone。不同的偏移量取决于tzinfo的设置方式

# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0, 
tzinfo=<DstTzInfo 'America/Los_Angeles' LMT-1 day, 16:07:00 STD>)
# tzinfo in not PST but LMT here, with a 7min offset !!!

# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=<UTC>)
# you end up with an offset

https://en.wikipedia.org/wiki/Local_mean_time


似乎相当有效:

import datetime
day, month, year = '01/12/2011'.split('/')
datetime.datetime(int(year), int(month), int(day)).timestamp()

1.61µs±120 ns /循环(7次运行的平均值±标准值,每次100000次循环)


只需使用datetime.datetime.strptime:

import datetime
stime = "01/12/2011"
print(datetime.datetime.strptime(stime, "%d/%m/%Y").timestamp())

结果:

1322697600

要使用UTC而不是本地时区,请使用.replace:

datetime.datetime.strptime(stime, "%d/%m/%Y").replace(tzinfo=datetime.timezone.utc).timestamp()

您可以从datetime引用下面的链接来使用strptime函数。Datetime,从任何格式转换日期和时区。

https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior


一个获取UNIX纪元时间的简单函数。

注意:此函数假设输入日期时间为UTC格式(请参阅此处的注释)。

def utctimestamp(ts: str, DATETIME_FORMAT: str = "%d/%m/%Y"):
    import datetime, calendar
    ts = datetime.datetime.utcnow() if ts is None else datetime.datetime.strptime(ts, DATETIME_FORMAT)
    return calendar.timegm(ts.utctimetuple())

用法:

>>> utctimestamp("01/12/2011")
1322697600
>>> utctimestamp("2011-12-01", "%Y-%m-%d")
1322697600

你可以转换成等值格式

my_date = '2020/08/08'
my_date = my_date.replace('/','-') # just to adapte to your question
date_timestamp = datetime.datetime.fromisoformat(my_date).timestamp()

我想给初学者(像我一样)一个答案:

你有日期字符串“01/12/2011”。然后可以按照“%d/%m/%Y”的格式编写。如果你想格式化成另一种格式,比如“2015年7月9日”,这里有一个很好的备忘单。

导入datetime库。 使用datetime。Datetime类来处理日期和时间组合。 使用strptime方法将字符串datetime转换为对象datetime。 最后,使用timestamp方法以浮点形式获取Unix纪元时间。所以,

import datetime
print( int( datetime.datetime.strptime( "01/12/2011","%d/%m/%Y" ).timestamp() ) )

# prints 1322712000

你可以从两个方向,unix epoch <==> datetime:

import datetime
import time


the_date = datetime.datetime.fromtimestamp( 1639763585 )



unix_time = time.mktime(the_date.timetuple())

assert  ( the_date == datetime.datetime.fromtimestamp(unix_time) ) & \
        ( time.mktime(the_date.timetuple()) == unix_time         )