Announcement

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

  • transfer year-month-day-hour-minute-second data

    Hi everyone,
    I wanna lnow how to transfer year-month-day-hour-minute-second data to hour, minute and second format. What I want is :
    original:
    31dec1899 07:14:23

    hope to tansfer to :
    07:14:23
    or
    hope to transfer to:

    hour minute second
    7 14 23


    Thanks a looot!

  • #2
    If the original datum is in Stata's date-time numeric datatype, and all you want is for it to display only the time component, then you'd just apply the date-time format without year, month and day. Otherwise, it will depend upon whether the original date-time is in string or in Stata's date-time datatype.

    .ÿclearÿ*

    .ÿquietlyÿsetÿobsÿ1

    .ÿgenerateÿstrÿdsÿ=ÿ"31dec1899ÿ07:14:23"

    .ÿgenerateÿdoubleÿdnÿ=ÿclock(d,ÿ"DMYhms")

    .ÿformatÿdnÿ%tcCCYY-NN-DD_HH:MM:SS

    .ÿlist,ÿnoobs

    ÿÿ+------------------------------------------+
    ÿÿ|ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿdsÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿdnÿ|
    ÿÿ|------------------------------------------|
    ÿÿ|ÿ31dec1899ÿ07:14:23ÿÿÿ1899-12-31ÿ07:14:23ÿ|
    ÿÿ+------------------------------------------+

    .ÿ*ÿIfÿoriginalÿisÿstring

    .ÿgenerateÿdoubleÿds_dtÿ=ÿclock(substr(ds,ÿ-8,ÿ8),ÿ"hms")

    .ÿ*ÿIfÿoriginalÿisÿStataÿnumericÿdate

    .ÿgenerateÿdoubleÿdn_dtÿ=ÿdnÿ-ÿcofd(floor(dofc(dn)))

    .ÿformatÿ*_dtÿ%tcHH:MM:SS

    .ÿlistÿ,ÿnoobs

    ÿÿ+----------------------------------------------------------------+
    ÿÿ|ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿdsÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿdnÿÿÿÿÿÿds_dtÿÿÿÿÿÿdn_dtÿ|
    ÿÿ|----------------------------------------------------------------|
    ÿÿ|ÿ31dec1899ÿ07:14:23ÿÿÿ1899-12-31ÿ07:14:23ÿÿÿ07:14:23ÿÿÿ07:14:23ÿ|
    ÿÿ+----------------------------------------------------------------+

    .

    Comment

    Working...
    X