Syntax for using epoch times in a calculation with the `--date=STRING` usage of the `date` command

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I have a time represented as seconds since 1970-01-01 00:00:00 UTC, like 1524884843.



I want to get the time, say, 1 month from the above specified time.



Normally if I want to get the time 1 month from now, I can use...



root@beaglebone:~/bbbrtc# date
Sat Apr 28 03:12:54 UTC 2018
root@beaglebone:~/bbbrtc# date -d "now+1 month"
Mon May 28 03:12:57 UTC 2018


I can also specify an epoch seconds time in the -d argument by prefixing it with an @ as in....



root@beaglebone:~/bbbrtc# date -d "@1524884843"
Sat Apr 28 03:07:23 UTC 2018


However when I try to combine an @ prefixed epoch time with a calculation, I get an error...



root@beaglebone:~/bbbrtc# date -d "@1524884843+1 month"
date: invalid date `@1524884843+1 month'


What is the correct syntax for combining an epoch time with a relative calculation?







share|improve this question















  • 1




    Of course, per unix.stackexchange.com/questions/422904 , there are other pitfalls to look out for here.
    – JdeBP
    Apr 30 at 6:58














up vote
2
down vote

favorite












I have a time represented as seconds since 1970-01-01 00:00:00 UTC, like 1524884843.



I want to get the time, say, 1 month from the above specified time.



Normally if I want to get the time 1 month from now, I can use...



root@beaglebone:~/bbbrtc# date
Sat Apr 28 03:12:54 UTC 2018
root@beaglebone:~/bbbrtc# date -d "now+1 month"
Mon May 28 03:12:57 UTC 2018


I can also specify an epoch seconds time in the -d argument by prefixing it with an @ as in....



root@beaglebone:~/bbbrtc# date -d "@1524884843"
Sat Apr 28 03:07:23 UTC 2018


However when I try to combine an @ prefixed epoch time with a calculation, I get an error...



root@beaglebone:~/bbbrtc# date -d "@1524884843+1 month"
date: invalid date `@1524884843+1 month'


What is the correct syntax for combining an epoch time with a relative calculation?







share|improve this question















  • 1




    Of course, per unix.stackexchange.com/questions/422904 , there are other pitfalls to look out for here.
    – JdeBP
    Apr 30 at 6:58












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have a time represented as seconds since 1970-01-01 00:00:00 UTC, like 1524884843.



I want to get the time, say, 1 month from the above specified time.



Normally if I want to get the time 1 month from now, I can use...



root@beaglebone:~/bbbrtc# date
Sat Apr 28 03:12:54 UTC 2018
root@beaglebone:~/bbbrtc# date -d "now+1 month"
Mon May 28 03:12:57 UTC 2018


I can also specify an epoch seconds time in the -d argument by prefixing it with an @ as in....



root@beaglebone:~/bbbrtc# date -d "@1524884843"
Sat Apr 28 03:07:23 UTC 2018


However when I try to combine an @ prefixed epoch time with a calculation, I get an error...



root@beaglebone:~/bbbrtc# date -d "@1524884843+1 month"
date: invalid date `@1524884843+1 month'


What is the correct syntax for combining an epoch time with a relative calculation?







share|improve this question











I have a time represented as seconds since 1970-01-01 00:00:00 UTC, like 1524884843.



I want to get the time, say, 1 month from the above specified time.



Normally if I want to get the time 1 month from now, I can use...



root@beaglebone:~/bbbrtc# date
Sat Apr 28 03:12:54 UTC 2018
root@beaglebone:~/bbbrtc# date -d "now+1 month"
Mon May 28 03:12:57 UTC 2018


I can also specify an epoch seconds time in the -d argument by prefixing it with an @ as in....



root@beaglebone:~/bbbrtc# date -d "@1524884843"
Sat Apr 28 03:07:23 UTC 2018


However when I try to combine an @ prefixed epoch time with a calculation, I get an error...



root@beaglebone:~/bbbrtc# date -d "@1524884843+1 month"
date: invalid date `@1524884843+1 month'


What is the correct syntax for combining an epoch time with a relative calculation?









share|improve this question










share|improve this question




share|improve this question









asked Apr 29 at 17:51









bigjosh

25439




25439







  • 1




    Of course, per unix.stackexchange.com/questions/422904 , there are other pitfalls to look out for here.
    – JdeBP
    Apr 30 at 6:58












  • 1




    Of course, per unix.stackexchange.com/questions/422904 , there are other pitfalls to look out for here.
    – JdeBP
    Apr 30 at 6:58







1




1




Of course, per unix.stackexchange.com/questions/422904 , there are other pitfalls to look out for here.
– JdeBP
Apr 30 at 6:58




Of course, per unix.stackexchange.com/questions/422904 , there are other pitfalls to look out for here.
– JdeBP
Apr 30 at 6:58










3 Answers
3






active

oldest

votes

















up vote
0
down vote













You can put the calculation inside parenthesis...



root@beaglebone:~/bbbrtc# date -d "@1524884843+(1 month)"
Sat Apr 28 03:07:23 UTC 2018





share|improve this answer

















  • 1




    No, that does not work.
    – Isaac
    Apr 30 at 7:49










  • That gives the same output as date -d "@1524884843".
    – Stéphane Chazelas
    Apr 30 at 9:56

















up vote
0
down vote













The only way is to use a two step conversion:



$ date -d "$(date -d "@1524884843") + 1 month"
Mon May 28 03:07:23 UTC 2018





share|improve this answer




























    up vote
    0
    down vote













    With ast-open's date (possibly ksh93's builtin date depending on how it was built and the environment):



    $ date -d '#1234567890'
    Fri Feb 13 23:31:30 GMT 2009
    $ date -d "#1234567890 30 days"
    Sun Mar 15 23:31:30 GMT 2009


    (ast date doesn't support offsets expressed in months (months don't have a fixed length)).



    With GNU date, you can use the old method (before @epoch support was added) where you express the epoch time as an offset in seconds relative to 1970-01-01 00:00:00 UTC:



    $ date -d '1970-01-01T00:00:00Z + 1234567890 seconds'
    Fri 13 Feb 23:31:30 GMT 2009
    $ date -d '1970-01-01T00:00:00Z + 1234567890 seconds + 1 month'
    Mon 16 Mar 23:31:30 GMT 2009


    Note however, that here, 1 month is 31 days, presumably because date considers the 1 month offset is relative to Jan 1970.



    If your date is the GNU ones and you would like the behaviour it exhibits for month-relative dates whereby a one-month offset gives you the same day of the month (assuming there's such a day in the next month) as that of the starting date (though not necessarily same hour of the day if there has been some DST change in the interval), then you would probably want to use @isaac's two step approach.






    share|improve this answer























      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f440771%2fsyntax-for-using-epoch-times-in-a-calculation-with-the-date-string-usage-of%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      You can put the calculation inside parenthesis...



      root@beaglebone:~/bbbrtc# date -d "@1524884843+(1 month)"
      Sat Apr 28 03:07:23 UTC 2018





      share|improve this answer

















      • 1




        No, that does not work.
        – Isaac
        Apr 30 at 7:49










      • That gives the same output as date -d "@1524884843".
        – Stéphane Chazelas
        Apr 30 at 9:56














      up vote
      0
      down vote













      You can put the calculation inside parenthesis...



      root@beaglebone:~/bbbrtc# date -d "@1524884843+(1 month)"
      Sat Apr 28 03:07:23 UTC 2018





      share|improve this answer

















      • 1




        No, that does not work.
        – Isaac
        Apr 30 at 7:49










      • That gives the same output as date -d "@1524884843".
        – Stéphane Chazelas
        Apr 30 at 9:56












      up vote
      0
      down vote










      up vote
      0
      down vote









      You can put the calculation inside parenthesis...



      root@beaglebone:~/bbbrtc# date -d "@1524884843+(1 month)"
      Sat Apr 28 03:07:23 UTC 2018





      share|improve this answer













      You can put the calculation inside parenthesis...



      root@beaglebone:~/bbbrtc# date -d "@1524884843+(1 month)"
      Sat Apr 28 03:07:23 UTC 2018






      share|improve this answer













      share|improve this answer



      share|improve this answer











      answered Apr 29 at 17:51









      bigjosh

      25439




      25439







      • 1




        No, that does not work.
        – Isaac
        Apr 30 at 7:49










      • That gives the same output as date -d "@1524884843".
        – Stéphane Chazelas
        Apr 30 at 9:56












      • 1




        No, that does not work.
        – Isaac
        Apr 30 at 7:49










      • That gives the same output as date -d "@1524884843".
        – Stéphane Chazelas
        Apr 30 at 9:56







      1




      1




      No, that does not work.
      – Isaac
      Apr 30 at 7:49




      No, that does not work.
      – Isaac
      Apr 30 at 7:49












      That gives the same output as date -d "@1524884843".
      – Stéphane Chazelas
      Apr 30 at 9:56




      That gives the same output as date -d "@1524884843".
      – Stéphane Chazelas
      Apr 30 at 9:56












      up vote
      0
      down vote













      The only way is to use a two step conversion:



      $ date -d "$(date -d "@1524884843") + 1 month"
      Mon May 28 03:07:23 UTC 2018





      share|improve this answer

























        up vote
        0
        down vote













        The only way is to use a two step conversion:



        $ date -d "$(date -d "@1524884843") + 1 month"
        Mon May 28 03:07:23 UTC 2018





        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          The only way is to use a two step conversion:



          $ date -d "$(date -d "@1524884843") + 1 month"
          Mon May 28 03:07:23 UTC 2018





          share|improve this answer













          The only way is to use a two step conversion:



          $ date -d "$(date -d "@1524884843") + 1 month"
          Mon May 28 03:07:23 UTC 2018






          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Apr 30 at 8:07









          Isaac

          6,4161633




          6,4161633




















              up vote
              0
              down vote













              With ast-open's date (possibly ksh93's builtin date depending on how it was built and the environment):



              $ date -d '#1234567890'
              Fri Feb 13 23:31:30 GMT 2009
              $ date -d "#1234567890 30 days"
              Sun Mar 15 23:31:30 GMT 2009


              (ast date doesn't support offsets expressed in months (months don't have a fixed length)).



              With GNU date, you can use the old method (before @epoch support was added) where you express the epoch time as an offset in seconds relative to 1970-01-01 00:00:00 UTC:



              $ date -d '1970-01-01T00:00:00Z + 1234567890 seconds'
              Fri 13 Feb 23:31:30 GMT 2009
              $ date -d '1970-01-01T00:00:00Z + 1234567890 seconds + 1 month'
              Mon 16 Mar 23:31:30 GMT 2009


              Note however, that here, 1 month is 31 days, presumably because date considers the 1 month offset is relative to Jan 1970.



              If your date is the GNU ones and you would like the behaviour it exhibits for month-relative dates whereby a one-month offset gives you the same day of the month (assuming there's such a day in the next month) as that of the starting date (though not necessarily same hour of the day if there has been some DST change in the interval), then you would probably want to use @isaac's two step approach.






              share|improve this answer



























                up vote
                0
                down vote













                With ast-open's date (possibly ksh93's builtin date depending on how it was built and the environment):



                $ date -d '#1234567890'
                Fri Feb 13 23:31:30 GMT 2009
                $ date -d "#1234567890 30 days"
                Sun Mar 15 23:31:30 GMT 2009


                (ast date doesn't support offsets expressed in months (months don't have a fixed length)).



                With GNU date, you can use the old method (before @epoch support was added) where you express the epoch time as an offset in seconds relative to 1970-01-01 00:00:00 UTC:



                $ date -d '1970-01-01T00:00:00Z + 1234567890 seconds'
                Fri 13 Feb 23:31:30 GMT 2009
                $ date -d '1970-01-01T00:00:00Z + 1234567890 seconds + 1 month'
                Mon 16 Mar 23:31:30 GMT 2009


                Note however, that here, 1 month is 31 days, presumably because date considers the 1 month offset is relative to Jan 1970.



                If your date is the GNU ones and you would like the behaviour it exhibits for month-relative dates whereby a one-month offset gives you the same day of the month (assuming there's such a day in the next month) as that of the starting date (though not necessarily same hour of the day if there has been some DST change in the interval), then you would probably want to use @isaac's two step approach.






                share|improve this answer

























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  With ast-open's date (possibly ksh93's builtin date depending on how it was built and the environment):



                  $ date -d '#1234567890'
                  Fri Feb 13 23:31:30 GMT 2009
                  $ date -d "#1234567890 30 days"
                  Sun Mar 15 23:31:30 GMT 2009


                  (ast date doesn't support offsets expressed in months (months don't have a fixed length)).



                  With GNU date, you can use the old method (before @epoch support was added) where you express the epoch time as an offset in seconds relative to 1970-01-01 00:00:00 UTC:



                  $ date -d '1970-01-01T00:00:00Z + 1234567890 seconds'
                  Fri 13 Feb 23:31:30 GMT 2009
                  $ date -d '1970-01-01T00:00:00Z + 1234567890 seconds + 1 month'
                  Mon 16 Mar 23:31:30 GMT 2009


                  Note however, that here, 1 month is 31 days, presumably because date considers the 1 month offset is relative to Jan 1970.



                  If your date is the GNU ones and you would like the behaviour it exhibits for month-relative dates whereby a one-month offset gives you the same day of the month (assuming there's such a day in the next month) as that of the starting date (though not necessarily same hour of the day if there has been some DST change in the interval), then you would probably want to use @isaac's two step approach.






                  share|improve this answer















                  With ast-open's date (possibly ksh93's builtin date depending on how it was built and the environment):



                  $ date -d '#1234567890'
                  Fri Feb 13 23:31:30 GMT 2009
                  $ date -d "#1234567890 30 days"
                  Sun Mar 15 23:31:30 GMT 2009


                  (ast date doesn't support offsets expressed in months (months don't have a fixed length)).



                  With GNU date, you can use the old method (before @epoch support was added) where you express the epoch time as an offset in seconds relative to 1970-01-01 00:00:00 UTC:



                  $ date -d '1970-01-01T00:00:00Z + 1234567890 seconds'
                  Fri 13 Feb 23:31:30 GMT 2009
                  $ date -d '1970-01-01T00:00:00Z + 1234567890 seconds + 1 month'
                  Mon 16 Mar 23:31:30 GMT 2009


                  Note however, that here, 1 month is 31 days, presumably because date considers the 1 month offset is relative to Jan 1970.



                  If your date is the GNU ones and you would like the behaviour it exhibits for month-relative dates whereby a one-month offset gives you the same day of the month (assuming there's such a day in the next month) as that of the starting date (though not necessarily same hour of the day if there has been some DST change in the interval), then you would probably want to use @isaac's two step approach.







                  share|improve this answer















                  share|improve this answer



                  share|improve this answer








                  edited Apr 30 at 10:03


























                  answered Apr 30 at 9:55









                  Stéphane Chazelas

                  279k53514846




                  279k53514846






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f440771%2fsyntax-for-using-epoch-times-in-a-calculation-with-the-date-string-usage-of%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Peggy Mitchell

                      Palaiologos

                      The Forum (Inglewood, California)