Announcement

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

  • Removing hours, minutes and seconds from numeric date

    I have several variables that are a mixture of dates and times. I do not need the times and would just like to remove them. For example, a date such as 03oct2019 16:20:00 would just need to be shortened to 03oct2019 in order to be consistent with other dates. I have tried the code below but it's not giving me what I want. When I tried to convert the date back to a string in order to use the "DMY#", I get an error that says cannot be reversibly converted.



    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str7 id double(first_date_of_recruitment last_date_of_recruitment)
    "1"  1.8857388e+12             .
    "2"  1888391520000             .
    "3"  1.8883725e+12 1.8909549e+12
    "5"  1891002480000             .
    "7"  1.8884376e+12             .
    "11" 1891023360000             .
    "13"             .             .
    "19" 1890964620000             .
    "30"             .             .
    "32"             . 1883241780000
    end
    format %tcDD-NN-YY first_date_of_recruitment
    format %tcDD-NN-YY last_date_of_recruitment
    ------------------ copy up to and including the previous line ------------------




    ----------------------- copy starting from the next line -----------------------
    Code:
    foreach var of varlist first_date_of_recruitment last_date_of_recruitment{
        
    generate double `var'2 = clock(`var', "YDM hms")
    format `var'2 %tcDD-NN-YY
    
    }
    ------------------ copy up to and including the previous line ------------------



  • #2
    The function dofc() pulls a daily date out of a datetime.

    See
    Code:
    help datetime
    and consider this token example

    .
    Code:
     di %tc  clock("2024 August 6 22:00", "YMD hm")
    06aug2024 22:00:00
    
    . di %td dofc(clock("2024 August 6 22:00", "YMD hm"))
    06aug2024

    Comment


    • #3
      Thank you Nick, this worked perfectly!

      Comment

      Working...
      X