Announcement

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

  • Shifting %tc variables by an hour

    Hi everyone,

    I'm trying to shift a datetime (format %tc) variable by an hour. Since tc variables are stored in milliseconds from Jan 1 1960, I assumed I needed to simply add the number of milliseconds in an hour to the variable, 60*60*1000.

    Starting with some dummy data...

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double tcvar
    1895225095000
    end
    format %tc tcvar

    Then I'll create a new variable, tcvar_shifted, with 60*60*1000 added to it

    Code:
    gen tcvar_shifted = tcvar + 60*60*1000
    format tcvar_shifted %tc
    list
    My original tcvar was "21jan2020 11:24:55", and my shifted variable is "21jan2020 12:23:57".

    So it almost works, but for some reason seems to be adding 58 seconds less than an hour. Any ideas why this might be?



  • #2
    Your new variable needs to be double too.

    Code:
    help datetime
    has this advice or warning many, many times, although I guess everyone dealing with dates forgets occasionally. Well, I do, just not in public.

    Comment


    • #3
      Ahh of course, thank you!

      Comment

      Working...
      X