datetime常規操作

2021-10-23 14:06:27 字數 1033 閱讀 1245

from datetime import datetime, timedelta

if __name__ == '__main__':

# 獲取當前時間

a = datetime.today()

# 或者

b = datetime.now()

# 指定特定時間

c = datetime(2020, 8, 11, 17, 52, 43)

print("today:".format(a))

print("now:".format(b))

print("datetime:".format(c))

# 使用timedelta來完成時間間隔的轉換, 可指定負數

x = timedelta(days=-5, hours=5, minutes=1,seconds=10)

e = c+x

print("e:%r" % e)

# 字串轉換成時間

str_to_date = datetime.strptime('2023年9月1日 14點23分', '%y年%m月%d日 %h點%m分')

print(str_to_date)

# 時間轉換成字串 時間物件.方法

date_to_str = a.strftime('%y-%m-%d %h:%m:%s')

print(date_to_str)

today:datetime.datetime(2020, 8, 20, 17, 42, 14, 279398)

now:datetime.datetime(2020, 8, 20, 17, 42, 14, 279397)

datetime:datetime.datetime(2020, 8, 11, 17, 52, 43)

e:datetime.datetime(2020, 8, 6, 22, 53, 53)

2017-09-01 14:23:00

2020-08-20 17:42:14

process finished with exit code 0

DateTime 操作詳解

今天datetime.now.date.toshortdatestring 昨天,就是今天的日期減一 datetime.now.adddays 1 toshortdatestring 明天,同理,加一 datetime.now.adddays 1 toshortdatestring 本週 要知道本週...

oracle常規操作

1 建立表 sql create table test id varchar2 10 age number 2 備份表 sql create table asselect from test group by id 3 刪除表 sql drop table test 刪除表結構和表資料 4 清空表 ...

ROS常規操作

記錄下在使用ros的過程中,一些常用操作。在ros中如果使用乙個工作空間,直接在 bashrc檔案的最後一行加入 source hellow catkin ws devel setup.bash 就可以讓roscd找到工作空間中的包了。但是如果使用多個工作空間,直接在 bashrc 中再加一句話 s...