Announcement

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

  • Minus 3 minutes from a datetime

    Dear Stata users,

    I have a data of time variable. Its storage type is string. I want to minus 3 minutes (180000ms) from this time variable. How can I do that? Thank you very much.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input strL hms
    "00:05:03,000"
    "00:05:06,097"
    "00:05:09,822"
    "00:05:14,312"
    "00:05:16,125"
    "00:05:18,131"
    "00:05:24,190"
    "00:05:27,229"
    end

  • #2
    Code:
    generate double hms_minus3min = clock(subinstr(hms,",",".",.),"hms")-180000
    format hms_minus3min %tcHH:MM:SS.sss

    Comment


    • #3
      Thank you so much daniel klein.
      I did the first step, then format newdate %tc, and got result of HH:MM:SS format, because I don't know the use of format %tcHH:MM:SS.sss. Thank you again.

      Comment


      • #4
        Hmm, I thought Chen might want the milliseconds separated by a comma, and that could be achieved easily with

        Code:
        format hms_minus3min %tcHH:MM:SS,sss
        but not so! we get
        Code:
        invalid %format
        I guess we could do it in string format with

        Code:
        gen wanted = subinstr(string(hms_minus3min, "%tcHH:MM:SS.sss"), ".", ",", 1)
        But I find it curious that the date-time format does not permit the comma separator for milliseconds.

        Comment


        • #5
          The period is not a separator; it's part of the (fractional) milliseconds. Here, I insert a comma before the (fractional) milliseconds:
          Code:
          . display %tcHH:MM:SS,.sss hms_minus3min[1]
          00:02:03,.000
          What is indeed a bit puzzling is that the period is not affected by
          Code:
          . set dp comma
          
          . display %tcHH:MM:SS.sss hms_minus3min[1]
          00:02:03.000

          Comment


          • #6
            Hemanshu Kumar daniel klein thank you for attention and elaboration.

            Comment

            Working...
            X