摘要:specific_time = datetime(2023, 1, 1, 12, 30)print(f"Specific date and time: {specific_time}")
获取当前日期和时间:
from datetime import datetimenow = datetime.nowprint(f"Current date and time: {now}")召唤过去或未来的瞬间,精确地塑造它:
specific_time = datetime(2023, 1, 1, 12, 30)print(f"Specific date and time: {specific_time}")格式化日期和时间:
formatted = now.strftime("%Y-%m-%d %H:%M:%S")print(f"Formatted date and time: {formatted}")解析字符串中的日期和时间:
date_string = "2023-01-01 15:00:00"parsed_date = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")print(f"Parsed date and time: {parsed_date}")穿越时光之间的距离,向前或向后跳跃:
from datetime import timedeltadelta = timedelta(days=7)future_date = now + deltaprint(f"Date after 7 days: {future_date}")日期和时间比较:
if specific_time > now: print("Specific time is in the future.")else: print("Specific time has passed.")提取日期:年、月、日以及更多信息
year = now.yearmonth = now.monthday = now.dayHour = now.hourMinute = now.minuteSecond = now.secondprint(f"Year: {year}, Month: {month}, Day: {day}, Hour: {hour}, Minute: {minute}, Second: {second}")与尊重本地时间的时区一起工作:
from datetime import timezone, timedeltautc_time = datetime.now(timezone.utc)print(f"Current UTC time: {utc_time}")# Adjusting to a specific timezone (e.g., EST)est_time = utc_time - timedelta(hours=5)print(f"Current EST time: {est_time}")weekday = now.strftime("%A")print(f"Today is: {weekday}")timestamp = datetime.timestamp(now)print(f"Current timestamp: {timestamp}")# Converting a timestamp back to a datetimedate_from_timestamp = datetime.fromtimestamp(timestamp)print(f"Date from timestamp: {date_from_timestamp}")来源:自由坦荡的湖泊AI
免责声明:本站系转载,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请在30日内与本站联系,我们将在第一时间删除内容!