Announcement

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

  • Creating a date & time-stamp with particular formatting

    I am attempting to create a date & time stamp variable called upload_timestamp that I need to format specifically as "m/d/Y, H:M:S" for the service I am uploading the data too. I am using Stata MP 17.0.

    I've created a timestamp using:
    Code:
    gen upload_timestamp = "`c(current_date)'"+"," +"`c(current_time)'"
    Which creates:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str20 upload_timestamp
    " 7 Dec 2022,17:21:51"
    " 7 Dec 2022,17:21:51"
    " 7 Dec 2022,17:21:51"
    " 7 Dec 2022,17:21:51"
    " 7 Dec 2022,17:21:51"
    Which is halfway there, but I am having trouble converting it into the format I require. I tried:
    Code:
    gen upload_timestamp = "`%tdN/D/CY date(c(current_date))'"+"_" +"`c(current_time)'"
    but it results in output missing the date half of the timestamp completely:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str9 upload_timestamp
    ",17:18:57"
    ",17:18:57"
    ",17:18:57"
    ",17:18:57"
    ",17:18:57"
    Any thoughts on how I can fix this? Thanks in advance!

  • #2
    What you get with display is a string, so consider

    Code:
    di daily("`c(current_date)'", "DMY")
    di  %tdN/D/CY daily("`c(current_date)'", "DMY")
    Res.:

    Code:
    . di daily("`c(current_date)'", "DMY")
    22986
    
    . di  %tdN/D/CY daily("`c(current_date)'", "DMY")
    12/07/2022

    Comment


    • #3
      In other words, you need:
      Code:
      gen upload_timestamp = "`:dis %tdN/D/CY date("`c(current_date)'","DMY")'" + ", " + "`c(current_time)'"
      and you can optionally change date() to daily() in line with #2 (the two are synonyms)
      Last edited by Hemanshu Kumar; 08 Dec 2022, 10:47.

      Comment


      • #4
        Thank you both for the help! That worked perfectly Hemanshu.

        Comment

        Working...
        X