conversion of date into seconds in unix

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












0















I have a requirement where I will be give the time in below format



2019-02-08T19:24:30.220Z by this I need to output number of days between the given date and present date.



given date = 2019-02-08T19:24:30.220Z
present date = 2019-02-20T19:24:30.220Z



output = 12










share|improve this question
























  • What unix is it? Does that unix have GNU utilities, ksh93, zsh, perl or python?

    – Stéphane Chazelas
    Feb 20 at 11:10






  • 1





    Your clock must be off as you posted your question at 2019-02-20T11:03:04Z

    – Stéphane Chazelas
    Feb 20 at 11:20











  • I am using Mac and shell is zsh

    – Sugatur Deekshith S N
    Feb 20 at 11:59












  • I've added a zsh solution to my answer.

    – Stéphane Chazelas
    Feb 20 at 12:28















0















I have a requirement where I will be give the time in below format



2019-02-08T19:24:30.220Z by this I need to output number of days between the given date and present date.



given date = 2019-02-08T19:24:30.220Z
present date = 2019-02-20T19:24:30.220Z



output = 12










share|improve this question
























  • What unix is it? Does that unix have GNU utilities, ksh93, zsh, perl or python?

    – Stéphane Chazelas
    Feb 20 at 11:10






  • 1





    Your clock must be off as you posted your question at 2019-02-20T11:03:04Z

    – Stéphane Chazelas
    Feb 20 at 11:20











  • I am using Mac and shell is zsh

    – Sugatur Deekshith S N
    Feb 20 at 11:59












  • I've added a zsh solution to my answer.

    – Stéphane Chazelas
    Feb 20 at 12:28













0












0








0








I have a requirement where I will be give the time in below format



2019-02-08T19:24:30.220Z by this I need to output number of days between the given date and present date.



given date = 2019-02-08T19:24:30.220Z
present date = 2019-02-20T19:24:30.220Z



output = 12










share|improve this question
















I have a requirement where I will be give the time in below format



2019-02-08T19:24:30.220Z by this I need to output number of days between the given date and present date.



given date = 2019-02-08T19:24:30.220Z
present date = 2019-02-20T19:24:30.220Z



output = 12







shell-script osx date






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 20 at 12:36









roaima

45.6k757124




45.6k757124










asked Feb 20 at 11:03









Sugatur Deekshith S NSugatur Deekshith S N

416




416












  • What unix is it? Does that unix have GNU utilities, ksh93, zsh, perl or python?

    – Stéphane Chazelas
    Feb 20 at 11:10






  • 1





    Your clock must be off as you posted your question at 2019-02-20T11:03:04Z

    – Stéphane Chazelas
    Feb 20 at 11:20











  • I am using Mac and shell is zsh

    – Sugatur Deekshith S N
    Feb 20 at 11:59












  • I've added a zsh solution to my answer.

    – Stéphane Chazelas
    Feb 20 at 12:28

















  • What unix is it? Does that unix have GNU utilities, ksh93, zsh, perl or python?

    – Stéphane Chazelas
    Feb 20 at 11:10






  • 1





    Your clock must be off as you posted your question at 2019-02-20T11:03:04Z

    – Stéphane Chazelas
    Feb 20 at 11:20











  • I am using Mac and shell is zsh

    – Sugatur Deekshith S N
    Feb 20 at 11:59












  • I've added a zsh solution to my answer.

    – Stéphane Chazelas
    Feb 20 at 12:28
















What unix is it? Does that unix have GNU utilities, ksh93, zsh, perl or python?

– Stéphane Chazelas
Feb 20 at 11:10





What unix is it? Does that unix have GNU utilities, ksh93, zsh, perl or python?

– Stéphane Chazelas
Feb 20 at 11:10




1




1





Your clock must be off as you posted your question at 2019-02-20T11:03:04Z

– Stéphane Chazelas
Feb 20 at 11:20





Your clock must be off as you posted your question at 2019-02-20T11:03:04Z

– Stéphane Chazelas
Feb 20 at 11:20













I am using Mac and shell is zsh

– Sugatur Deekshith S N
Feb 20 at 11:59






I am using Mac and shell is zsh

– Sugatur Deekshith S N
Feb 20 at 11:59














I've added a zsh solution to my answer.

– Stéphane Chazelas
Feb 20 at 12:28





I've added a zsh solution to my answer.

– Stéphane Chazelas
Feb 20 at 12:28










1 Answer
1






active

oldest

votes


















3














With ksh93 (often installed by default on commercial SysV based unices like AIX or Solaris), which also happens to be the /bin/sh of Solaris 11 and newer:



date=2019-02-08T19:24:30.220Z
export LC_ALL=C # to make sure the decimal radix is "."
then_in_seconds=$(printf '%(%s.%N)Tn' "$date")
now_in_seconds=$(printf '%(%s.%N)Tn' now)
difference_in_seconds=$((now_in_seconds - then_in_seconds))
difference_in_24h_periods=$((difference_in_seconds / 24 / 60 / 60))
echo "Result: $difference_in_24h_periods"


At 2019-02-20T11:17:30Z and a bit, that gave me:



Result: 11.6618110817684377


You can use $((f(difference_in_24h_periods))) where f is one of round, floor, ceil, nearbyint, trunc, rint, int like in C if you want the difference as an integer, or use printf format specifications to specify the number of significant digits.



With zsh:



zmodload zsh/datetime
date=2019-02-08T19:24:30.220Z
TZ=UTC0 strftime -rs then_in_seconds '%Y-%m-%dT%H:%M:%S' "$date%.*"
then_in_seconds+=.$$date##*.%Z
now_in_seconds=$EPOCHREALTIME
difference_in_seconds=$((now_in_seconds - then_in_seconds))
difference_in_24h_periods=$((difference_in_seconds / 24 / 60 / 60))
echo "Result: $difference_in_24h_periods"





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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    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%2f501818%2fconversion-of-date-into-seconds-in-unix%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    With ksh93 (often installed by default on commercial SysV based unices like AIX or Solaris), which also happens to be the /bin/sh of Solaris 11 and newer:



    date=2019-02-08T19:24:30.220Z
    export LC_ALL=C # to make sure the decimal radix is "."
    then_in_seconds=$(printf '%(%s.%N)Tn' "$date")
    now_in_seconds=$(printf '%(%s.%N)Tn' now)
    difference_in_seconds=$((now_in_seconds - then_in_seconds))
    difference_in_24h_periods=$((difference_in_seconds / 24 / 60 / 60))
    echo "Result: $difference_in_24h_periods"


    At 2019-02-20T11:17:30Z and a bit, that gave me:



    Result: 11.6618110817684377


    You can use $((f(difference_in_24h_periods))) where f is one of round, floor, ceil, nearbyint, trunc, rint, int like in C if you want the difference as an integer, or use printf format specifications to specify the number of significant digits.



    With zsh:



    zmodload zsh/datetime
    date=2019-02-08T19:24:30.220Z
    TZ=UTC0 strftime -rs then_in_seconds '%Y-%m-%dT%H:%M:%S' "$date%.*"
    then_in_seconds+=.$$date##*.%Z
    now_in_seconds=$EPOCHREALTIME
    difference_in_seconds=$((now_in_seconds - then_in_seconds))
    difference_in_24h_periods=$((difference_in_seconds / 24 / 60 / 60))
    echo "Result: $difference_in_24h_periods"





    share|improve this answer





























      3














      With ksh93 (often installed by default on commercial SysV based unices like AIX or Solaris), which also happens to be the /bin/sh of Solaris 11 and newer:



      date=2019-02-08T19:24:30.220Z
      export LC_ALL=C # to make sure the decimal radix is "."
      then_in_seconds=$(printf '%(%s.%N)Tn' "$date")
      now_in_seconds=$(printf '%(%s.%N)Tn' now)
      difference_in_seconds=$((now_in_seconds - then_in_seconds))
      difference_in_24h_periods=$((difference_in_seconds / 24 / 60 / 60))
      echo "Result: $difference_in_24h_periods"


      At 2019-02-20T11:17:30Z and a bit, that gave me:



      Result: 11.6618110817684377


      You can use $((f(difference_in_24h_periods))) where f is one of round, floor, ceil, nearbyint, trunc, rint, int like in C if you want the difference as an integer, or use printf format specifications to specify the number of significant digits.



      With zsh:



      zmodload zsh/datetime
      date=2019-02-08T19:24:30.220Z
      TZ=UTC0 strftime -rs then_in_seconds '%Y-%m-%dT%H:%M:%S' "$date%.*"
      then_in_seconds+=.$$date##*.%Z
      now_in_seconds=$EPOCHREALTIME
      difference_in_seconds=$((now_in_seconds - then_in_seconds))
      difference_in_24h_periods=$((difference_in_seconds / 24 / 60 / 60))
      echo "Result: $difference_in_24h_periods"





      share|improve this answer



























        3












        3








        3







        With ksh93 (often installed by default on commercial SysV based unices like AIX or Solaris), which also happens to be the /bin/sh of Solaris 11 and newer:



        date=2019-02-08T19:24:30.220Z
        export LC_ALL=C # to make sure the decimal radix is "."
        then_in_seconds=$(printf '%(%s.%N)Tn' "$date")
        now_in_seconds=$(printf '%(%s.%N)Tn' now)
        difference_in_seconds=$((now_in_seconds - then_in_seconds))
        difference_in_24h_periods=$((difference_in_seconds / 24 / 60 / 60))
        echo "Result: $difference_in_24h_periods"


        At 2019-02-20T11:17:30Z and a bit, that gave me:



        Result: 11.6618110817684377


        You can use $((f(difference_in_24h_periods))) where f is one of round, floor, ceil, nearbyint, trunc, rint, int like in C if you want the difference as an integer, or use printf format specifications to specify the number of significant digits.



        With zsh:



        zmodload zsh/datetime
        date=2019-02-08T19:24:30.220Z
        TZ=UTC0 strftime -rs then_in_seconds '%Y-%m-%dT%H:%M:%S' "$date%.*"
        then_in_seconds+=.$$date##*.%Z
        now_in_seconds=$EPOCHREALTIME
        difference_in_seconds=$((now_in_seconds - then_in_seconds))
        difference_in_24h_periods=$((difference_in_seconds / 24 / 60 / 60))
        echo "Result: $difference_in_24h_periods"





        share|improve this answer















        With ksh93 (often installed by default on commercial SysV based unices like AIX or Solaris), which also happens to be the /bin/sh of Solaris 11 and newer:



        date=2019-02-08T19:24:30.220Z
        export LC_ALL=C # to make sure the decimal radix is "."
        then_in_seconds=$(printf '%(%s.%N)Tn' "$date")
        now_in_seconds=$(printf '%(%s.%N)Tn' now)
        difference_in_seconds=$((now_in_seconds - then_in_seconds))
        difference_in_24h_periods=$((difference_in_seconds / 24 / 60 / 60))
        echo "Result: $difference_in_24h_periods"


        At 2019-02-20T11:17:30Z and a bit, that gave me:



        Result: 11.6618110817684377


        You can use $((f(difference_in_24h_periods))) where f is one of round, floor, ceil, nearbyint, trunc, rint, int like in C if you want the difference as an integer, or use printf format specifications to specify the number of significant digits.



        With zsh:



        zmodload zsh/datetime
        date=2019-02-08T19:24:30.220Z
        TZ=UTC0 strftime -rs then_in_seconds '%Y-%m-%dT%H:%M:%S' "$date%.*"
        then_in_seconds+=.$$date##*.%Z
        now_in_seconds=$EPOCHREALTIME
        difference_in_seconds=$((now_in_seconds - then_in_seconds))
        difference_in_24h_periods=$((difference_in_seconds / 24 / 60 / 60))
        echo "Result: $difference_in_24h_periods"






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 20 at 15:08

























        answered Feb 20 at 11:14









        Stéphane ChazelasStéphane Chazelas

        310k57584945




        310k57584945



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501818%2fconversion-of-date-into-seconds-in-unix%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?