Linux Script- Date Manipulations

前端 未结 5 720
[愿得一人]
[愿得一人] 2021-01-18 06:09

I will set one date variable(Say \'08-JUN-2011\') and I want to do some calculations based on that date namely,
1. Have to get the first day of the given day\'s month.

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-18 06:35

    This is going to be convoluted in a shell script. You are better off using Date::Manip in perl, or something similar in another full-featured language. However, I can think of some ways to do this with the date command. First of all, you can use a --date parameter to set a starting point for date, like so:

    $ date --date='08-JUN-2011'
    Wed Jun  8 00:00:00 EDT 2011
    

    You can get the previous date like this:

    $ date --date='08-JUN-2011 -1 days'
    Tue Jun  7 00:00:00 EDT 2011
    

    For the last day of the month, I would just walking back from 31 until date does not fail. You can check $? for that

    $ date --date='31-JUN-2011';echo $?
    date: invalid date `31-JUN-2011'
    1
    $ date --date='30-JUN-2011';echo $?
    Thu Jun 30 00:00:00 EDT 2011
    0
    

    For the first day of the month...that is usually 01 :)

提交回复
热议问题