Announcement

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

  • Access the actual current date and time from computer clock to input in file name

    Hi,

    I would like to access the actual current date and time from computer clock to input in file names. I am running multiple simulations per day, I would like to achieve results sorted by date and time under the following format:
    save "YYYY-MM-DD-hh:mm-Results.dta"

    Is that something Stata can do? Thanks a lot in advance.

    Click image for larger version

Name:	Capture.JPG
Views:	1
Size:	12.6 KB
ID:	1565074


  • #2
    Code:
    . di "$S_DATE $S_TIME"
    24 Jul 2020 09:17:39
    
    . local wanted = subinstr("$S_DATE", " ", "_", .) + "_" + "$S_TIME"
    
    . di "`wanted'"
    24_Jul_2020_09:19:52
    and so on.

    Comment


    • #3
      Awesome !

      Can you also show us how to put the date and time in what I think they call Hungarian format, e.g., here in this example it would be 20200724091952?

      I wanted once to set my seed like this, and either I did not manage to figure it out, or figured it out and then forgot how I did it.

      This might be useful for Francois as well, dates in Hungarian format sort very nicely.

      Originally posted by Nick Cox View Post
      Code:
      . di "$S_DATE $S_TIME"
      24 Jul 2020 09:17:39
      
      . local wanted = subinstr("$S_DATE", " ", "_", .) + "_" + "$S_TIME"
      
      . di "`wanted'"
      24_Jul_2020_09:19:52
      and so on.

      Comment


      • #4
        It's called ISO 8601, as it is the international standard for this, and the international standard allows delimiters, such as "T" between the date and time portions, for improved readability.

        . display in smcl as text %tcCCYYNNDD!THHMMSS clock("`c(current_date)'`c(current_time)'", "DMYhms")
        20200724T180942

        Comment


        • #5
          This is truly awesome, thank you so much Nick and Joseph.

          Slight modification: Is it possible to avoid displaying seconds in the final output both in ISO 8601 and in the format suggested by Nick? Thanks!

          Comment


          • #6
            Code:
            . di substr("$S_TIME", 1, 5)
            12:55

            Comment


            • #7
              Originally posted by Francois Durant View Post
              Is it possible to avoid displaying seconds in the final output both in ISO 8601 and in the format suggested by Nick?
              Nick has shown you for his. For the approach that I illustrated with an ISO 8601 format, you'd just forgo the final two esses in the format statement.

              . local now : display %tcCCYYNNDDHHMM clock("`c(current_date)'`c(current_time)'", "DMYhms")

              . display in smcl as text "`now'"
              202007242104

              Comment


              • #8
                Another use of dates is for generating an automatic log of activity. In my profile.do is this code for a daily log file. Very useful when someone asks months later how a variable was generated or edited!

                local ver = 16 /* could get this automatically */
                local logdate = string( d(`c(current_date)'), "%dCY-N-D" )
                capture cmdlog using "c:\stata`ver'logfiles\log`logdate'.txt", append

                Comment

                Working...
                X