Opengist

Explore

  • All gists
  • Topics
  • Users
Give feedback on the new UI Powered by Opengist ⋅ 245ms

user:thomas gists created by user

title:mygist gists with given title

description:sync gists with given description

filename:myfile.txt gists having files with given name

extension:yml gists having files with given extension

language:go gists having files with given language

topic:homelab gists with given topic

all:systemctl search all fields

Login
d

dev-tools

Recently created Least recently created Recently updated Least recently updated

timmy / 🕒 Python 時間處理懶人包:轉字串、計算差、顯示幾分鐘前

Last active 5 hours ago date-format datetime dev-tools human-readable python time-utils timestamp

常用的 datetime 工具函式:轉格式、計算秒數差、解析時間字串、顯示幾分鐘前/剛剛/幾天前等人類友善時間字串,寫後台超實用 ✅

0 0 0 1
1 import math
2 import time
3 from datetime import datetime
4
5
6 # 轉換時間格式到字串(天)
7 def human_date(date=None):
8 if date:
9 assert isinstance(date, datetime)
10 else: