timeformatted= str(datetime.utcnow())
semiformatted= timeformatted.replace("-","")
almostformatted= semiformatted.replace(":","")
formatted=almostformatted.replace(".","")
withspacegoaway=formatted.replace(" ","")
formattedstripped=withspacegoaway.strip()
print formattedstripped
缩短它之后
OP要求一个带有毫秒(小数点后三位小数)的字符串。要获取毫秒,请改用:
from datetime import datetime
print datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
>>>> OUTPUT >>>>
2013-08-23 10:18:32.926
将time格式化为具有毫秒的字符串
import time
def get_time_stamp():
ct = time.time()
local_time = time.localtime(ct)
data_head = time.strftime("%Y-%m-%d %H:%M:%S", local_time)
data_secs = (ct - int(ct)) * 1000
time_stamp = "%s.%03d" % (data_head, data_secs)
print(time_stamp)
stamp = ("".join(time_stamp.split()[0].split("-"))+"".join(time_stamp.split()[1].split(":"))).replace('.', '')
print(stamp)
if __name__ == '__main__':
get_time_stamp()
输出结果如下:
2018-04-17 15:28:28.434
20180417152828434
第一行为:年-月-日 时:分:秒 . 毫秒
第二行为拼接后时间戳
格式化日期
我们可以使用 time 模块的 strftime 方法来格式化日期,:
time.strftime(format[, t])
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import time
# 格式化成2016-03-20 11:45:39形式
print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
# 格式化成Sat Mar 28 22:24:24 2016形式
print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())
# 将格式字符串转换为时间戳
a = "Sat Mar 28 22:24:24 2016"
print time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))
实例输出结果:
2016-04-07 10:25:09
Thu Apr 07 10:25:09 2016
1459175064.0
python中时间日期格式化符号:
- %y 两位数的年份表示(00-99)
- %Y 四位数的年份表示(000-9999)
- %m 月份(01-12)
- %d 月内中的一天(0-31)
- %H 24小时制小时数(0-23)
- %I 12小时制小时数(01-12)
- %M 分钟数(00=59)
- %S 秒(00-59)
- %a 本地简化星期名称
- %A 本地完整星期名称
- %b 本地简化的月份名称
- %B 本地完整的月份名称
- %c 本地相应的日期表示和时间表示
- %j 年内的一天(001-366)
- %p 本地A.M.或P.M.的等价符
- %U 一年中的星期数(00-53)星期天为星期的开始
- %w 星期(0-6),星期天为星期的开始
- %W 一年中的星期数(00-53)星期一为星期的开始
- %x 本地相应的日期表示
- %X 本地相应的时间表示
- %Z 当前时区的名称
- %% %号本身
# 2020-10-15 12:02:45.676482
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'))
# 2020-10-15 12:02:45.676516
print(datetime.datetime.now()) # 2019-01-28 11:09:01.529864
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')) # 2019-01-28 11:09:01.529864
print(datetime.datetime.now...
print (t) #原始时间数据
print (int(t)) #秒级时间戳
print (int(round(t * 1000))) #毫秒级时间戳
print (in...
ct = time.time()
local_time = time.localtime(ct)
data_head = time.strftime("%Y%m%d%H%M%S", local_time)
data_secs = (ct - int(ct)) * 1000
now = datetime.datetime.now()
# 格式化输出当前时间,包括毫秒print(now.strftime('%Y-%m-%d %H:%M:%S.%f'))
这样就可以得到当前时间,包括毫秒的格式化输出。
注意,如果只想输出毫秒,可以使用 '%f',如果想要输出微秒,...
ct = time.time()
local_time = time.localtime(ct)
data_head = str(time.strftime("%Y%m
Unix 时间戳根据精度的不同,有 10 位(秒级),13 位(毫秒级),16 位(微妙级)和 19 位(纳秒级)
python 毫秒级时间,时间戳转换如下:
时间转时间戳:
import time
from datetime import datetime
timestr = '2019-01-14 15:22:18.123'
datetime_obj = datetime.strptime(timestr, "%Y-%m-%d %H:%M:%S.%f")
obj_stamp = int(time.mkt
Ip2region是什么?
ip2region - 准确率99.9%的离线IP地址定位库,0.0x毫秒级查询,ip2region.db数据库只有数MB,提供了java,php,c,python,nodejs,golang,c#等查询绑定和Binary,B树,内存三种查询算法。
Ip2region特性
99.9%准确率
数据聚合了一些知名ip到地名查询提供商的数据,这些是他们官方的的准确率,经测试着实比经典的纯真IP定位准确一些。
ip2region的数据聚合自以下服务商的开放API或者数据(升级程序每秒请求次数2到4次):
01, >80%, 淘宝IP地址库,
02, ≈10%, GeoIP,
03, ≈2%, 纯真IP库,
备注:如果上述开放API或者数据都不给开放数据时ip2region将停止数据的更新服务。
标准化的数据格式
每条ip数据段都固定了格式:
_城市Id|国
虽然秒已经够短的了,但是人类操作键盘鼠标的时间间隔却是在毫秒级(millisecond)。有的时候为了应用的需要,需要使用毫秒级的时间。本文介绍如何在Python中获取毫秒时间,顺带一并介绍如何获取微秒(microsecond)时间。获取毫秒在Python中获取毫秒时间,基本思路就是转换time.time()函数返回的时间浮点数,来获取当前毫秒时间。代码如下:import timedef getM...