How Can I Get minute() to Return 00 instead of 0?












1















I am working on pulling time data from a dttm object in R and formatting it in HH:MM. I have been able to accomplish this using



paste(hour(datetime), minute(datetime), sep=':')


with one exception: when the minutes are less than 10. When I do this, the minute function just returns the minutes without a leading 0. For example:



library(lubridate)

datetime = ymd_hms('2018-11-18 14:00:00')

minute(datetime)


This gives an output of



> minute(datetime)
[1] 0


I believe there are options using hms or other packages/functions, or by building an if-then, but I'd like to minimize dependencies and complexity and stick within the particulars of lubridate to accomplish my goal if at all possible.



(Note: I realize the same issue exists for hour(), but all the times I'm using are PM, so it's not as pressing in my case).










share|improve this question























  • your data is having 14 hour 0 minutes so obviously it will give minute as 0 change it to 14:30 , it will give you output as 30

    – Hunaidkhan
    Nov 21 '18 at 5:50






  • 4





    sprintf("%02d", minute(datetime)) ?

    – Ronak Shah
    Nov 21 '18 at 5:51
















1















I am working on pulling time data from a dttm object in R and formatting it in HH:MM. I have been able to accomplish this using



paste(hour(datetime), minute(datetime), sep=':')


with one exception: when the minutes are less than 10. When I do this, the minute function just returns the minutes without a leading 0. For example:



library(lubridate)

datetime = ymd_hms('2018-11-18 14:00:00')

minute(datetime)


This gives an output of



> minute(datetime)
[1] 0


I believe there are options using hms or other packages/functions, or by building an if-then, but I'd like to minimize dependencies and complexity and stick within the particulars of lubridate to accomplish my goal if at all possible.



(Note: I realize the same issue exists for hour(), but all the times I'm using are PM, so it's not as pressing in my case).










share|improve this question























  • your data is having 14 hour 0 minutes so obviously it will give minute as 0 change it to 14:30 , it will give you output as 30

    – Hunaidkhan
    Nov 21 '18 at 5:50






  • 4





    sprintf("%02d", minute(datetime)) ?

    – Ronak Shah
    Nov 21 '18 at 5:51














1












1








1








I am working on pulling time data from a dttm object in R and formatting it in HH:MM. I have been able to accomplish this using



paste(hour(datetime), minute(datetime), sep=':')


with one exception: when the minutes are less than 10. When I do this, the minute function just returns the minutes without a leading 0. For example:



library(lubridate)

datetime = ymd_hms('2018-11-18 14:00:00')

minute(datetime)


This gives an output of



> minute(datetime)
[1] 0


I believe there are options using hms or other packages/functions, or by building an if-then, but I'd like to minimize dependencies and complexity and stick within the particulars of lubridate to accomplish my goal if at all possible.



(Note: I realize the same issue exists for hour(), but all the times I'm using are PM, so it's not as pressing in my case).










share|improve this question














I am working on pulling time data from a dttm object in R and formatting it in HH:MM. I have been able to accomplish this using



paste(hour(datetime), minute(datetime), sep=':')


with one exception: when the minutes are less than 10. When I do this, the minute function just returns the minutes without a leading 0. For example:



library(lubridate)

datetime = ymd_hms('2018-11-18 14:00:00')

minute(datetime)


This gives an output of



> minute(datetime)
[1] 0


I believe there are options using hms or other packages/functions, or by building an if-then, but I'd like to minimize dependencies and complexity and stick within the particulars of lubridate to accomplish my goal if at all possible.



(Note: I realize the same issue exists for hour(), but all the times I'm using are PM, so it's not as pressing in my case).







r lubridate






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 5:48









Todd BurusTodd Burus

7710




7710













  • your data is having 14 hour 0 minutes so obviously it will give minute as 0 change it to 14:30 , it will give you output as 30

    – Hunaidkhan
    Nov 21 '18 at 5:50






  • 4





    sprintf("%02d", minute(datetime)) ?

    – Ronak Shah
    Nov 21 '18 at 5:51



















  • your data is having 14 hour 0 minutes so obviously it will give minute as 0 change it to 14:30 , it will give you output as 30

    – Hunaidkhan
    Nov 21 '18 at 5:50






  • 4





    sprintf("%02d", minute(datetime)) ?

    – Ronak Shah
    Nov 21 '18 at 5:51

















your data is having 14 hour 0 minutes so obviously it will give minute as 0 change it to 14:30 , it will give you output as 30

– Hunaidkhan
Nov 21 '18 at 5:50





your data is having 14 hour 0 minutes so obviously it will give minute as 0 change it to 14:30 , it will give you output as 30

– Hunaidkhan
Nov 21 '18 at 5:50




4




4





sprintf("%02d", minute(datetime)) ?

– Ronak Shah
Nov 21 '18 at 5:51





sprintf("%02d", minute(datetime)) ?

– Ronak Shah
Nov 21 '18 at 5:51












2 Answers
2






active

oldest

votes


















2














With format you can get your own custom date format after specifying -



> format(datetime, "%M")
[1] "00"


OR



Thanks @Ronak -



> sprintf("%02d", minute(datetime))
[1] "00"





share|improve this answer
























  • Always amazing what you can achieve with base R!

    – Christoph
    Nov 21 '18 at 6:54











  • @Christoph Amazingly, you can get the hour and the minute with base R.

    – Hong Ooi
    Nov 21 '18 at 7:20











  • Awesome. The second option worked great in the particular script I was running. Thanks.

    – Todd Burus
    Nov 21 '18 at 7:26



















1














Why are you doing all this extra work? Just get the time directly from your datetime (POSIXct) object.



strftime(datetime, "%H:%M")
# [1] "14:00"





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2fstackoverflow.com%2fquestions%2f53405936%2fhow-can-i-get-minute-to-return-00-instead-of-0%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    With format you can get your own custom date format after specifying -



    > format(datetime, "%M")
    [1] "00"


    OR



    Thanks @Ronak -



    > sprintf("%02d", minute(datetime))
    [1] "00"





    share|improve this answer
























    • Always amazing what you can achieve with base R!

      – Christoph
      Nov 21 '18 at 6:54











    • @Christoph Amazingly, you can get the hour and the minute with base R.

      – Hong Ooi
      Nov 21 '18 at 7:20











    • Awesome. The second option worked great in the particular script I was running. Thanks.

      – Todd Burus
      Nov 21 '18 at 7:26
















    2














    With format you can get your own custom date format after specifying -



    > format(datetime, "%M")
    [1] "00"


    OR



    Thanks @Ronak -



    > sprintf("%02d", minute(datetime))
    [1] "00"





    share|improve this answer
























    • Always amazing what you can achieve with base R!

      – Christoph
      Nov 21 '18 at 6:54











    • @Christoph Amazingly, you can get the hour and the minute with base R.

      – Hong Ooi
      Nov 21 '18 at 7:20











    • Awesome. The second option worked great in the particular script I was running. Thanks.

      – Todd Burus
      Nov 21 '18 at 7:26














    2












    2








    2







    With format you can get your own custom date format after specifying -



    > format(datetime, "%M")
    [1] "00"


    OR



    Thanks @Ronak -



    > sprintf("%02d", minute(datetime))
    [1] "00"





    share|improve this answer













    With format you can get your own custom date format after specifying -



    > format(datetime, "%M")
    [1] "00"


    OR



    Thanks @Ronak -



    > sprintf("%02d", minute(datetime))
    [1] "00"






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 21 '18 at 5:56









    Vivek KalyanaranganVivek Kalyanarangan

    5,0361827




    5,0361827













    • Always amazing what you can achieve with base R!

      – Christoph
      Nov 21 '18 at 6:54











    • @Christoph Amazingly, you can get the hour and the minute with base R.

      – Hong Ooi
      Nov 21 '18 at 7:20











    • Awesome. The second option worked great in the particular script I was running. Thanks.

      – Todd Burus
      Nov 21 '18 at 7:26



















    • Always amazing what you can achieve with base R!

      – Christoph
      Nov 21 '18 at 6:54











    • @Christoph Amazingly, you can get the hour and the minute with base R.

      – Hong Ooi
      Nov 21 '18 at 7:20











    • Awesome. The second option worked great in the particular script I was running. Thanks.

      – Todd Burus
      Nov 21 '18 at 7:26

















    Always amazing what you can achieve with base R!

    – Christoph
    Nov 21 '18 at 6:54





    Always amazing what you can achieve with base R!

    – Christoph
    Nov 21 '18 at 6:54













    @Christoph Amazingly, you can get the hour and the minute with base R.

    – Hong Ooi
    Nov 21 '18 at 7:20





    @Christoph Amazingly, you can get the hour and the minute with base R.

    – Hong Ooi
    Nov 21 '18 at 7:20













    Awesome. The second option worked great in the particular script I was running. Thanks.

    – Todd Burus
    Nov 21 '18 at 7:26





    Awesome. The second option worked great in the particular script I was running. Thanks.

    – Todd Burus
    Nov 21 '18 at 7:26













    1














    Why are you doing all this extra work? Just get the time directly from your datetime (POSIXct) object.



    strftime(datetime, "%H:%M")
    # [1] "14:00"





    share|improve this answer




























      1














      Why are you doing all this extra work? Just get the time directly from your datetime (POSIXct) object.



      strftime(datetime, "%H:%M")
      # [1] "14:00"





      share|improve this answer


























        1












        1








        1







        Why are you doing all this extra work? Just get the time directly from your datetime (POSIXct) object.



        strftime(datetime, "%H:%M")
        # [1] "14:00"





        share|improve this answer













        Why are you doing all this extra work? Just get the time directly from your datetime (POSIXct) object.



        strftime(datetime, "%H:%M")
        # [1] "14:00"






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 5:58









        Hong OoiHong Ooi

        42.3k1091133




        42.3k1091133






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • 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%2fstackoverflow.com%2fquestions%2f53405936%2fhow-can-i-get-minute-to-return-00-instead-of-0%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

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?