How to calculate a date a week ago from today

前端 未结 4 1447
终归单人心
终归单人心 2021-01-13 09:08

I wanna calculate the date a week ago from today with a specific format and put it in to a variable. For example, today is Nov 21st. 2014, and I wanna print out

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-13 09:31

    Why not just subtract X days from the "mday" field of localtime? This example shows subtracting 60 days from the end of august. I'm not sure who corrects the month but I think I'm getting the right answer...

    $ date
    Wed Aug 30 14:34:14 DFT 2017
    $ perl -MPOSIX -e '@t=localtime time; $t[3] -= 60; print strftime( "%Y/%m/%d", @t), "\n";'
    2017/07/01
    

提交回复
热议问题