Shell, Python time function summary

Sometimes it is necessary to write some timed task scripts, a brief summary, memos.

1. Get current time

  • python
    Accurate to 0.001 seconds under Windows, and time accuracy under Linux 0.000001 second
>>> 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
    [email protected]:/ # date "+%Y-%m-%d %H:%M:%S"
    2018-05-23 15:51:29

    3. Time addition and subtraction

    • 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)< br />>>> print t2
    2018-05-21 16:12:33.184000
    • shell
      Get the date of the previous Monday, which is the closest Monday, Not necessarily last Monday.
      For example, today is Wednesday, then the previous Monday is the day before yesterday.
      Also note that the busybox command on the Android system is a castrated version, and advanced parameters may not be supported.
    date -d "last-monday" -I
    2018-05-21
    #Similarly, you can enter:
    date -d "next -monday" -I
    2018-05-28
    #Yesterday
    date -d "yesterday" -I
    2018-05-22

    < p>

Leave a Comment

Your email address will not be published.