shell, python function of time Summary

Sometimes you need to write some scripts regular tasks, briefly summarize, memo.

1. Get the current time

  • python
    in the windows accurate to 0.001 seconds, linux lower time accuracy of 0.000001 seconds
>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2018, 5, 23, 17, 16, 33, 61000)
>>> print datetime.datetime.now()
2018-05-23 17:16:57.688000
  • shell
date
Wed May 23 15:53:45 CST 2018

2. Time Format

  • python
>>> import datetime
>>> datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
'2018-05-23 15:50:07'
  • shell
date -I
2018-05-23
root@hammerhead:/ # date "+%Y-%m-%d %H:%M:%S"
2018-05-23 15:51:29

3. Time to add and subtract

  • python
>>> t0 = datetime.datetime.now()
>>> t1 = t0 + datetime.timedelta(seconds=600)
>>> print t0
2018-05-23 16:12:33.184000
>>> print t1
2018-05-23 16:22:33.184000
>>> t2 =  t0 - datetime.timedelta(days=2)
>>> print t2
2018-05-21 16:12:33.184000
  • shell
    get on a Monday date, that is the nearest Monday, not necessarily on Monday.
    For example, today Wednesday, then on a Monday is the day before yesterday.
    Also note, busybox commands on the Android version is castrated, advanced parameter does not necessarily support.
date -d "last-monday" -I
2018-05-21
#同理,可以输入:
date -d "next-monday" -I
2018-05-28
#昨天
date -d "yesterday" -I
2018-05-22

Guess you like

Origin www.cnblogs.com/M4K0/p/11432863.html