python - How to change the date dynamically using a command in linux -
python - How to change the date dynamically using a command in linux -
i writing python code alter date in linux scheme today-1 (dynamically). tried various combinations but, yet not able succeed. searched , found close proximity scenario in question .
i able alter date if execute command static value say:
date --set="$(date +'2013%m%d %h:%m')"
however, don't want specify hardcoded value year i.e., 2013. instead want specify "%y-1"
i.e.,
date --set="$(date +'%y-1%m%d %h:%m')"
if run above command next error
[root@ramesh ~]$ date --set="$(date +'%y-1%m%d %h:%m')" date: invalid date `14-11016 13:05'
thanks answer. did not seek approach though, reason beingness has 1 time 1 time again dealt formatting issues when working arithmetic operations incase if want to.
so, figured out much simpler , generalized approach
fetch previous_year value command
date --date='1 years ago'
this gives previous year date. can used in python programme update scheme in next way
"date --set=$(date +'%%y%%m%s %%h:%%m') % previous_year"
this method has few advantages like
i can apply method day , month "1 days ago", "1 month ago"
along +%d, +%m, +%y
values.
e.g., date --date='1 years ago' +%y
python linux datetime
Comments
Post a Comment