Find the answer to your Linux question:
Results 1 to 4 of 4
How to convert date format such as 7/18/2015 to the number of month from request date in sh scripting ? Let say I have output in my log.txt -> 7/18/2015. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2012
    Posts
    2

    How to convert date format such as 7/18/2015 to the number of month?


    How to convert date format such as 7/18/2015 to the number of month from request date in sh scripting ?

    Let say I have output in my log.txt -> 7/18/2015. How I convert it to the full number of month in shell scripting ? Thanks in advance.

  2. #2
    Linux Newbie
    Join Date
    Jun 2012
    Location
    SF Bay area
    Posts
    148
    Quote Originally Posted by amerabest View Post
    How to convert date format such as 7/18/2015 to the number of month from request date in sh scripting ?

    Let say I have output in my log.txt -> 7/18/2015. How I convert it to the full number of month in shell scripting ? Thanks in advance.
    If your system supports the "--date" flag for passing a time into the "date" command, this should work to convert m/d/yyyy to seconds since 1/1/1970. You can do "time math" from there and convert back to date formats later if necessary.

    Code:
    date --date '7/18/2015' +"%s"

  3. #3
    Just Joined!
    Join Date
    Oct 2012
    Posts
    2
    Hmm...interesting. Thanks. Good to know
    But a little bit complicated in my case, since some month has 28-29-30-31 days. Any other solution how to count number of month from 'date' till 7/18/2015?

  4. #4
    Linux Newbie
    Join Date
    Jun 2012
    Location
    SF Bay area
    Posts
    148
    Sorry, I misunderstood what you were trying to do. Actually I still don't know what you mean since a "month" isn't a fixed amount of time. So what does the number of months from an arbitrary date to 7/18/2015 mean? Unless the starting date is also the 18th day of a month it's going to be a fraction of a "month" even with a fuzzy definition of that unit.

    If you can explain what you're trying to do a bit more I can probably come up with a suggestion.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •