Skip to content

时间日期格式

python
# python 3.8.6
from datetime import datetime, date, timedelta
import time


print(time.time())                        # <class 'float'> 1657247017.1310942

print(datetime.strptime('20220708', '%Y%m%d')) # <class 'datetime.date'> 2022-07-08
print(date.fromisoformat('2022-07-08'))        # <class 'datetime.date'> 2022-07-08
print(date.fromtimestamp(1657247131))          # <class 'datetime.date'> 2022-07-08
print(date.fromtimestamp(time.time()))         # <class 'datetime.date'> 2022-07-08
print(date.today())                            # <class 'datetime.date'> 2022-07-08
print(date.today() - timedelta(days=10))       # <class 'datetime.date'> 2022-06-28
                             
print(date.today().__str__())             # <class 'str'> 2022-07-08
print(date.today().isoformat())           # <class 'str'> 2022-07-08
                             
print(datetime.combine(date.today(), datetime.min.time())) # <class 'datetime.datetime'> 2022-08-10 00:00:00
print(datetime.fromisoformat('2022-08-09 22:52:50.000000') # <class 'datetime.datetime'> 2022-08-09 22:52:50 
print(datetime.fromtimestamp(1657247442))                  # <class 'datetime.datetime'> 2022-07-08 10:30:42
print(datetime.now())                                      # <class 'datetime.datetime'> 2022-07-08 10:30:42.403608
print(datetime.now().timestamp())                          # <class 'float'> 1657247442.403608
print(datetime.utcnow())                                   # <class 'datetime.datetime'>, datetime.datetime(2022, 7, 8, 2, 52, 20, 755084)
print(datetime.utcnow().timestamp())                       # <class 'float'>, 1657220016.181139


print(datetime.isoformat(datetime.now()))                              # <class 'str'> 2022-07-08T10:42:31.035107

print(datetime.isoformat(datetime.now(), sep=" ", timespec="seconds")) # <class 'str'> 2022-07-08 10:42:31
print(datetime.now().isoformat(sep=" ", timespec="seconds"))           # <class 'str'> 2022-07-08 10:42:31

print(datetime.now().strftime("%A, %d. %B %Y %I:%M%p"))                # <class 'str'> Friday, 08. July 2022 10:42AM
print(datetime.now().strftime("%Y年%m月%d日 %H时%M分%S秒"))             # <class 'str'> 2022年07月08日 10时42分31秒

Released under the MIT License.