Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • duration between 2 dates

    I cannot get duration (in days) to work between two dates Date1 and Date 2.

    list Date1: 23mar2021 04:00:00

    I changed the format of date1 and dat2 from %tc to %td.
    then gen vent_days = date2 - date1


    i get vent_days: 2.21e+07
    Date2: 1.91e+12
    Date1: 1.91e+12


    Now sure how I am getting numbers so high they are exponential? The number of days between these dates should be between 1 and 30-ish.

    As an update: I realize this is because there are times. So if I have a variable that includes the times not just the date, how do I use state to computer duration?
    Last edited by jeff jennings; 01 Nov 2021, 11:50.

  • #2
    Do not change the display format thinking that this converts from one date-time type to another. That does not work. See https://journals.sagepub.com/doi/pdf...867X1201200415

    There are 24 x 60 x 60000 ms in 1 day, so that should be the difference in ms between just now and 1 day earlier. I use display on sandbox examples.

    Code:
    di 24 * 60 * 60000
    86400000
    
    . di clock("1 Nov 2021 18:26:00", "DMY hms") - clock("31 Oct 2021 18:26:00", "DMY hms")
    86400000
    
    .. di (clock("1 Nov 2021 18:26:00", "DMY hms") - clock("31 Oct 2021 18:26:00", "DMY hms")) / 864e5
    1
    In your case, I think it boils down to


    Code:
    gen wanted = (date2 - date1) / 864e5
    People seem to hope that they can use date-times without reading the documentation (much), but in practice you need to read as much of

    help datetime

    as solves your problem.
    Last edited by Nick Cox; 01 Nov 2021, 13:05.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Do not change the display format thinking that this converts from one date-time type to another. That does not work. See https://journals.sagepub.com/doi/pdf...867X1201200415



      In your case, I think it boils down to


      Code:
      gen wanted = (date2 - date1) / 864e5
      as solves your problem.
      That worked. I hope when future people google "stata date duration between dates" your answer pops up. Every time I googled this, I would get confused but I have a weakness for dates - even when I code in PHP dates are my weak spot!.
      Last edited by jeff jennings; 03 Nov 2021, 06:40.

      Comment


      • #4
        To quote myself on a paper that should appear in Stata Journal 22(1) 2022


        Dates are complicated.The entire rigmarole of dates (to say
        nothing of times too...) is a complicated mess. From
        your early education onwards, you have been exposed repeatedly to the
        rules you usually need to know, but handling dates in software like
        Stata poses a new set of challenges. Some software handles dates through
        one or more distinct data or variable types. Stata's decision that dates
        and times are just integers simplifies much, but still implies details
        about formats and functions that may need mastering. help datetime
        remains the best place to start.

        Comment

        Working...
        X