0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Get last monday in python3

Posted at
  • source
from datetime import datetime as ddatetime, date as ddate, timedelta as dtimedelta
from argparse import ArgumentParser

usage = 'Usage: python {} [--ndt <ndt>] [--help]'\
        .format(__file__)
argparser = ArgumentParser(usage=usage)
argparser.add_argument('-n', '--ndt', type=str,
                       dest='ndt',
                       help='now datetime')
args = argparser.parse_args()

if args.ndt:
    print("ndt set:" + args.ndt)
    ndt = args.ndt
    nd = ddatetime.strptime(ndt, '%Y-%m-%d %H:%M:%S')
else:
    nd = ddatetime.now()

cd = nd - dtimedelta(days=((nd.weekday())))
print(cd.date())
  • execution
# python3 test.py  
2021-06-07
# python3 test.py --ndt "2021-06-09 00:00:00"
ndt set:2021-06-09 00:00:00
2021-06-07
# python3 test.py --ndt "2021-06-06 23:59:59"
ndt set:2021-06-06 23:59:59
2021-05-31
# python3 test.py --ndt "2021-06-07 00:00:02"
ndt set:2021-06-07 00:00:02
2021-06-07
# python3 test.py --ndt "2021-06-07 00:00:02"
ndt set:2021-06-07 00:00:02
2021-06-07
# python3 test.py --ndt "2021-06-10 00:00:02"
ndt set:2021-06-10 00:00:02
2021-06-07
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?