Announcement

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

  • Modify the time format of time single value or time variable

    hi , I want to extract the month and day of the current date (c(current_date)) and display them in the format of month and day, occupying two digits respectively. When the month or day is only the ones digit, use zero to fill the digit

    date wanted
    29 Sep 2021 0929
    09 Aug 2021 0809
    01 Dec 2021 1201


    The codes that I solve the problemto as follows:

    scalar M = month(date(c(current_date),"DMY"))
    scalar D = day(date(c(current_date),"DMY"))
    scalar MD = `=M'`=D' // why it's wring
    di "`=M'`=D'" // the result is 929, but i want 0929


    Looking forward to your help,thank you very much

  • #2
    The VBA language in EXCEL can be easily implemented by the following code:
    Format(Date, "mmdd")

    Comment


    • #3
      Code:
      help datetime##s8
      help datetime##s4
      help datetime_display_formats

      Comment


      • #4
        Code:
        . di %tdNNDD daily(c(current_date), "DMY")
        0928

        Comment


        • #5
          thank you very much , how to quote this month and day(0929), I need to save it as a macro or a scalar and quote it in the code behind
          Last edited by fu gang; 28 Sep 2021, 16:29.

          Comment


          • #6
            One way is to do:

            Code:
            local mydate : di %tdNNDD daily(c(current_date), "DMY")

            Comment


            • #7
              thank you very much, the result is 929, but not 0929 i hoped

              local mydate : di %tdNNDD daily(c(current_date), "DMY") di `mydate' 929

              Comment


              • #8
                Originally posted by fu gang View Post
                thank you very much, the result is 929, but not 0929 i hoped

                local mydate : di %tdNNDD daily(c(current_date), "DMY") di `mydate' 929
                That's because when the macro mydate is substituted, it is interpreted as a number, so 0928 becomes 928. Wrap it in quotes to keep the leading zero.

                Code:
                di "`mydate'"

                Comment


                • #9
                  i got it ,thank you very much

                  Comment


                  • #10
                    As for

                    I need to save it as a macro or a scalar
                    I doubt that is true. The code in e.g. #4 will work fine in a do-file or program, Putting the value in a macro or scalar is not needed unless you need the macro or scalar for some other reason.

                    Comment


                    • #11
                      I will often save my documents , adding month and day when naming the document to distinguish the documents

                      Comment


                      • #12
                        #11 That is good practice to be sure, but code such as

                        Code:
                        `: di %tdNNDD daily(c(current_date), "DMY")'
                        can be used on the fly within statements. That's perhaps a little tricksy or unclear, and wanting to use a macro or scalar as a matter of style is one thing, but you don't have to do it that way.
                        Last edited by Nick Cox; 29 Sep 2021, 06:06.

                        Comment


                        • #13
                          Thank you very much. It's very kind of you

                          Comment

                          Working...
                          X