程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Mutual conversion between Python timestamp and date format

編輯:Python

Python Mutual conversion between timestamp and date format

  • The introduction
  • One 、 Get current date , To 10 Bit or 13 A time stamp
  • Two 、 take 10 Bit or 13 Bit timestamp to date format ( year - month - Japan when - branch - second )

The introduction

java The default precision is in milliseconds , The generated timestamp is 13 position , and python The default is 10 Bit , The accuracy is seconds . that python How is it generated 13 A time stamp , And how time stamps are converted to dates ( year - month - Japan when - branch - second )

  • 13 Bit is a millisecond timestamp ( difficulty : Enter the time in milliseconds , Time to transfer out of normal format )
  • 10 Bit is a second timestamp .

Python Realization 【 Time stamp 】 And 【 Date format 】 Application function summary table of mutual transformation :

Python function function Example time.time() Get the current time 1655179674.911647int(time.time()) Get a timestamp accurate to seconds ,10 position 1655179674int(round(time.time() * 1000)) Get the exact millisecond timestamp ,13 position 1655179674912time.localtime(k1) take 10 A time stamp k1 Convert to date format 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)) take 10 A time stamp k1 To 【 year - month - Japan when - branch - second 】 Date format 2019-09-02 16:19:35time.localtime(k1/1000) take 13 A time stamp k1 Convert to date format 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)) take 13 A time stamp k1 To 【 year - month - Japan when - branch - second 】 Date format 2019-09-02 16:19:35

One 、 Get current date , To 10 Bit or 13 A time stamp

  • Custom function 1 get_second():python Get a timestamp accurate to seconds ,10 position
  • Custom function 2 get_millisecond():python Get the exact millisecond timestamp ,13 position
  • Custom function 3 get_delta(t1,t2): Subtract two timestamps , Return seconds
# -*- coding:utf-8 -*-
import time
# Get current date , To 10 Bit timestamp format 
def get_second():
""" :return: Get a timestamp accurate to seconds ,10 position """
return int(time.time())
# Get current date , To 13 Bit timestamp format 
def get_millisecond():
""" :return: Get the exact millisecond timestamp ,13 position """
millis = int(round(time.time() * 1000))
return millis
# Two 13 The timestamp of bits is subtracted , Return seconds 
def get_delta(t1,t2):
""" :param t1: 13 A time stamp :param t2: 13 A time stamp :return: Subtract two timestamps , Return seconds """
res=int((t2 - t1)/1000)
return res
if __name__ == "__main__":
print(get_second()) # Get the current time , And turn to 10 Bit timestamp format 
>>>
1655179674
print(time.time()) # Print the timestamp with full accuracy directly 
>>>
1655179674.911647
time1=get_millisecond()
print(time1) # Get the current time , And turn to 13 Bit timestamp format 
>>>
1655179674912
# Two 13 Bit timestamp for difference operation 
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

Two 、 take 10 Bit or 13 Bit timestamp to date format ( year - month - Japan when - branch - second )

function 4 millisecond_to_time(millis):13 Bit timestamp converted to date format string

import time
# Enter the time in milliseconds , Time to transfer out of normal format 
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 # Randomly assigned timestamp 
timeStamp(time_st) # Call function 
>>>
2022-06-11 18:19:48

Reference link :【1】 Online time conversion tool :https://tool.lu/timestamp


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved