網站首頁 編程語言 正文
緒論
java默認精度是毫秒級別的,生成的時間戳是13位,而python默認是10位的,精度是秒。那么python是如何生成13位時間戳,以及時間戳如何轉換為日期(年-月-日 時-分-秒)
- 13位是毫秒時間戳(難點:?輸入毫秒級的時間,轉出正常格式的時間)
- 10位是秒時間戳。
Python實現【時間戳】與【日期格式】之間相互轉化的應用函數匯總表:
Python函數 | 功能 | 示例 |
---|---|---|
time.time() | 獲取當前時間 | 1655179674.911647 |
int(time.time() ) |
獲取精確到秒時間戳,10位 | 1655179674 |
int(round(time.time() * 1000) ) |
獲取精確毫秒時間戳,13位 | 1655179674912 |
time.localtime (k1) |
將10位時間戳k1轉為日期格式 | time.struct_time(tm_year=2022, tm_mon=6, tm_mday=11, tm_hour=18, tm_min=19, tm_sec=48, tm_wday=5, tm_yday=162, tm_isdst=0) |
time.strftime (“%Y-%m-%d %H:%M:%S”, time.localtime (k1)) |
將10位時間戳k1轉為【年-月-日 時-分-秒】日期格式 | 2019-09-02 16:19:35 |
time.localtime(k1/1000) |
將13位時間戳k1轉為日期格式 | time.struct_time(tm_year=2022, tm_mon=6, tm_mday=11, tm_hour=18, tm_min=19, tm_sec=48, tm_wday=5, tm_yday=162, tm_isdst=0) |
time.strftime (“%Y-%m-%d %H:%M:%S”, time.localtime (k1/1000)) |
將13位時間戳k1轉為【年-月-日 時-分-秒】日期格式 | 2019-09-02 16:19:35 |
一、獲取當前日期,轉為10位或13位時間戳
- 自定義函數1 get_second():python獲取精確到秒時間戳,10位
- 自定義函數2 get_millisecond():python獲取精確毫秒時間戳,13位
- 自定義函數3 get_delta(t1,t2):兩個時間戳相減,返回秒數
# -*- coding:utf-8 -*- import time # 獲取當前日期,轉為10位時間戳格式 def get_second(): """ :return: 獲取精確到秒時間戳,10位 """ return int(time.time()) # 獲取當前日期,轉為13位時間戳格式 def get_millisecond(): """ :return: 獲取精確毫秒時間戳,13位 """ millis = int(round(time.time() * 1000)) return millis # 兩個13位的時間戳相減,返回秒數 def get_delta(t1,t2): """ :param t1: 13位時間戳 :param t2: 13位時間戳 :return: 兩個時間戳相減,返回秒數 """ res=int((t2 - t1)/1000) return res if __name__ == "__main__": print(get_second()) # 獲取當前時間,并轉為10位時間戳格式 >>> 1655179674 print(time.time()) # 直接打印全量精度的時間戳 >>> 1655179674.911647 time1=get_millisecond() print(time1) # 獲取當前時間,并轉為13位時間戳格式 >>> 1655179674912 # 兩個13位時間戳作差運算 k1=1567412375458 k2=1567412395853 now = int(round(time.time() * 1000)) print(now) >>> 1655179674913 t1 = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(k1/1000)) t2=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(k2/1000)) print(t1) >>> 2019-09-02 16:19:35 print(t2) >>> 2019-09-02 16:19:55 print(get_delta(k1,k2)) >>> 20
二、將10位或13位時間戳轉為日期格式(年-月-日 時-分-秒)
函數4 millisecond_to_time(millis):13位時間戳轉換為日期格式字符串
import time # 輸入毫秒級的時間,轉出正常格式的時間 def timeStamp(timeNum): timeStamp = float(timeNum/1000) timeArray = time.localtime(timeStamp) otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray) print(otherStyleTime) time_st = 1654942788469 # 隨機指定時間戳 timeStamp(time_st) # 調用函數 >>> 2022-06-11 18:19:48
參考鏈接:【1】在線時間轉換工具:http://tools.jb51.net/code/unixtime
總結
原文鏈接:https://blog.csdn.net/weixin_42782150/article/details/125275519
相關推薦
- 2022-06-02 Android?View的事件體系教程詳解_Android
- 2022-08-29 Python使用re模塊實現正則表達式操作指南_python
- 2022-06-06 微信小程序動態綁定class
- 2023-02-27 plt.subplot()參數及使用介紹_python
- 2022-04-09 SpringBoot默認日志框架(slf4j)的使用以及配置文件
- 2023-03-20 C#如何刪除指定文件或文件夾_C#教程
- 2023-01-12 shell編程時出現:未預期的符號?`then'?附近有語法錯誤?或者?:?行?:?`then'問題
- 2023-02-12 Pytorch建模過程中的DataLoader與Dataset示例詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支