Time Formats

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Time Formats

Keval Khichadia
Hi,
 
I have a variable start_time that holds the start time of a class. The format is in for example,
 
start_time
 
1-Jan-1900 09:25:00
1-Jan-1900 14:15:00
 
I used the below syntax:
 
Compute StartTime = XDATE.TIME(START_TIME).
formats StartTime (TIME5).
 
and the output gives
 
9:25
14:15
 
Is there a way I can get it to read like
 
9:25 A.M.
2:15 P.M
 
Thanks,
Keval
 
 

Reply | Threaded
Open this post in threaded view
|

Re: Time Formats

mpirritano

Why would you want the AM and PM?

 

If you want AM and PM for presentation purposes I’d just make that change in the graph or whatever presentation format you finish with. It makes more sense to keep the 24hr time format for calculations.

 

I don’t believe SPSS has an AM/ PM format. MS Excel does though. Still I’d advise against it.

 

Matthew Pirritano, Ph.D.

Research Analyst IV

Medical Services Initiative (MSI)

Orange County Health Care Agency

(714) 568-5648


From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Keval Khichadia
Sent: Wednesday, July 29, 2009 12:39 PM
To: [hidden email]
Subject: Time Formats

 

Hi,

 

I have a variable start_time that holds the start time of a class. The format is in for example,

 

start_time

 

1-Jan-1900 09:25:00

1-Jan-1900 14:15:00

 

I used the below syntax:

 

Compute StartTime = XDATE.TIME(START_TIME).
formats StartTime (TIME5).

 

and the output gives

 

9:25

14:15

 

Is there a way I can get it to read like

 

9:25 A.M.

2:15 P.M

 

Thanks,

Keval

 

 

 

Reply | Threaded
Open this post in threaded view
|

Re: Time Formats

Richard Ristow
In reply to this post by Keval Khichadia
At 03:38 PM 7/29/2009, Keval Khichadia wrote:

I have a variable start_time that holds the start time of a class. The format is in for example,
 
start_time
 
1-Jan-1900 09:25:00
1-Jan-1900 14:15:00
 
I used the below syntax:
 
Compute StartTime = XDATE.TIME(START_TIME).
formats StartTime (TIME5).
 
and the output gives
 
9:25
14:15
 
Is there a way I can get it to read like
 
9:25 A.M.
2:15 P.M

As you've heard, there's no format under which an SPSS time value will display that way. Here's some code from a while back, that converts a date-time into a string with AM/PM.  It was tested when I posted it(*), but I'm not re-testing it now.

/*  ------------    Test data    ------------  */
NEW FILE.
INPUT PROGRAM.
.  NUMERIC            TIME_VAL (DATETIME22).
.  VARIABLE WIDTH     TIME_VAL (22).
.  VARIABLE ALIGNMENT TIME_VAL (CENTER).

.  COMPUTE  TIME_VAL = $TIME.
.  END CASE.
.  COMPUTE  TIME_VAL = DATE.MDY(06,06,1944) + TIME.HMS(10,34,56).
.  END CASE.
.  COMPUTE  TIME_VAL = DATE.MDY(02,14,1966) + TIME.HMS(15,54,32).
.  END CASE.
.  COMPUTE  TIME_VAL = DATE.MDY(01,10,2001) + TIME.HMS(13,23,45).
.  END CASE.

.  COMPUTE  TIME_VAL = DATE.MDY(07,03,2002) + TIME.HMS(23,30,00).
.  END CASE.
.  COMPUTE  TIME_VAL = DATE.MDY(07,04,2002) + TIME.HMS(00,00,00).
.  END CASE.
.  COMPUTE  TIME_VAL = DATE.MDY(07,04,2002) + TIME.HMS(00,30,00).
.  END CASE.
.  COMPUTE  TIME_VAL = DATE.MDY(07,04,2002) + TIME.HMS(01,00,00).
.  END CASE.
.  COMPUTE  TIME_VAL = DATE.MDY(07,04,2002) + TIME.HMS(01,30,00).
.  END CASE.

.  COMPUTE  TIME_VAL = DATE.MDY(07,04,2002) + TIME.HMS(11,30,00).
.  END CASE.
.  COMPUTE  TIME_VAL = DATE.MDY(07,04,2002) + TIME.HMS(12,00,00).
.  END CASE.
.  COMPUTE  TIME_VAL = DATE.MDY(07,04,2002) + TIME.HMS(12,30,00).
.  END CASE.
.  COMPUTE  TIME_VAL = DATE.MDY(07,04,2002) + TIME.HMS(13,00,00).
.  END CASE.
.  COMPUTE  TIME_VAL = DATE.MDY(07,04,2002) + TIME.HMS(13,30,00).
.  END CASE.
END FILE.
END INPUT PROGRAM.
EXECUTE.

/*  ------------ Desired format  ------------  */
/* Target output variable         */
STRING  CVT     (A22).
/* Scratch intermediate variables */
STRING  #DATE   (A10).
NUMERIC #TIME   (TIME10)
       /#ATIME  (TIME10).
STRING  #HMS    (A08)
       /#AMPM   (A02).

COMPUTE #DATE   = STRING(TIME_VAL,ADATE10).

COMPUTE #TIME   = XDATE.TIME(TIME_VAL).
DO IF    (#TIME < TIME.HMS(01)).
.  COMPUTE #ATIME = #TIME + TIME.HMS(12).
.  COMPUTE #HMS   = STRING(#ATIME,TIME08).
.  COMPUTE #AMPM  = 'AM'.
ELSE IF  (#TIME < TIME.HMS(12)).
.  COMPUTE #ATIME = #TIME.
.  COMPUTE #HMS  = STRING(#ATIME,TIME08).
.  COMPUTE #AMPM = 'AM'.
ELSE IF  (#TIME < TIME.HMS(13)).
.  COMPUTE #ATIME = #TIME.
.  COMPUTE #HMS  = STRING(#ATIME,TIME08).
.  COMPUTE #AMPM = 'PM'.
ELSE.
.  COMPUTE #ATIME = #TIME - TIME.HMS(12).
.  COMPUTE #HMS   = STRING(#ATIME,TIME08).
.  COMPUTE #AMPM  = 'PM'.
END IF.

COMPUTE CVT = CONCAT(#DATE,' ',#HMS,' ',#AMPM).

/*  ------------    Display      ------------  */
*  Scratch variables as permanent variables, for display .
NUMERIC NUM_TM  (TIME10)
       /NUM_ADJT(TIME10).
STRING  STR_DT  (A10)
       /STR_HMS (A08)
       /AM      (A02).
COMPUTE    STR_DT   = #DATE.
COMPUTE    NUM_TM   = #TIME.
COMPUTE    NUM_ADJT = #ATIME.
COMPUTE    STR_HMS  = #HMS.
COMPUTE    AM       = #AMPM.

STRING  X (A2).
COMPUTE X = '  '.
EXECUTE.
LIST VARIABLES=TIME_VAL X CVT.

List
              TIME_VAL X  CVT

  31-OCT-2003 00:01:26    10/31/2003 12:01:26 AM
  06-JUN-1944 10:34:56    06/06/1944 10:34:56 AM
  14-FEB-1966 15:54:32    02/14/1966  3:54:32 PM
  10-JAN-2001 13:23:45    01/10/2001  1:23:45 PM
  03-JUL-2002 23:30:00    07/03/2002 11:30:00 PM
  04-JUL-2002 00:00:00    07/04/2002 12:00:00 AM
  04-JUL-2002 00:30:00    07/04/2002 12:30:00 AM
  04-JUL-2002 01:00:00    07/04/2002  1:00:00 AM
  04-JUL-2002 01:30:00    07/04/2002  1:30:00 AM
  04-JUL-2002 11:30:00    07/04/2002 11:30:00 AM
  04-JUL-2002 12:00:00    07/04/2002 12:00:00 PM
  04-JUL-2002 12:30:00    07/04/2002 12:30:00 PM
  04-JUL-2002 13:00:00    07/04/2002  1:00:00 PM
  04-JUL-2002 13:30:00    07/04/2002  1:30:00 PM

Number of cases read:  14    Number of cases listed:  14


LIST VARIABLES=TIME_VAL X STR_DT NUM_TM NUM_ADJT STR_HMS AM.

List
              TIME_VAL X  STR_DT         NUM_TM   NUM_ADJT STR_HMS  AM

  31-OCT-2003 00:01:26    10/31/2003    0:01:26   12:01:26 12:01:26 AM
  06-JUN-1944 10:34:56    06/06/1944   10:34:56   10:34:56 10:34:56 AM
  14-FEB-1966 15:54:32    02/14/1966   15:54:32    3:54:32  3:54:32 PM
  10-JAN-2001 13:23:45    01/10/2001   13:23:45    1:23:45  1:23:45 PM
  03-JUL-2002 23:30:00    07/03/2002   23:30:00   11:30:00 11:30:00 PM
  04-JUL-2002 00:00:00    07/04/2002    0:00:00   12:00:00 12:00:00 AM
  04-JUL-2002 00:30:00    07/04/2002    0:30:00   12:30:00 12:30:00 AM
  04-JUL-2002 01:00:00    07/04/2002    1:00:00    1:00:00  1:00:00 AM
  04-JUL-2002 01:30:00    07/04/2002    1:30:00    1:30:00  1:30:00 AM
  04-JUL-2002 11:30:00    07/04/2002   11:30:00   11:30:00 11:30:00 AM
  04-JUL-2002 12:00:00    07/04/2002   12:00:00   12:00:00 12:00:00 PM
  04-JUL-2002 12:30:00    07/04/2002   12:30:00   12:30:00 12:30:00 PM
  04-JUL-2002 13:00:00    07/04/2002   13:00:00    1:00:00  1:00:00 PM
  04-JUL-2002 13:30:00    07/04/2002   13:30:00    1:30:00  1:30:00 PM

Number of cases read:  14    Number of cases listed:  14
..................................
*Posted:
Date:    Fri, 31 Oct 2003 00:05:42 -0500
From:    Richard Ristow <[hidden email]>
Subject: Re: $time AM/PM
To:      [hidden email]

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Time Formats

Albert-Jan Roskam
In reply to this post by Keval Khichadia
Hi Keval!

I've been tinkering with this problem a bit as I was trying to find a short Python solution. In the end, it's not really that conscise. Maybe I should have used the datacursor functionality of the spssdata module instead of the spss module. Anyway, the syntax below works, guaranteed.

Cheers!!
Albert-Jan

* sample data.
data list free / timevar (time8).
begin data
"14:03"
"10:55"
"23:01"
end data.

* actual code.
BEGIN PROGRAM.
import time, spss, spssaux, random
def format_to_ampm(in_time_var, out_time_var, str_len = 8):
    temp_time_var = "timevarstr"+str(random.randint(0,9999999))
    spss.Submit("string %s (a%s)." % (temp_time_var, str_len))
    spss.Submit("compute %s = string(%s, time%s)." % (temp_time_var, in_time_var, str_len))
    varDict = spssaux.VariableDict([temp_time_var])
    dataCursor = spss.Cursor([varDict.VariableIndex(temp_time_var)], accessType='w')
    dataCursor.SetVarNameAndType([out_time_var],[str_len])
    dataCursor.CommitDictionary()
    for case in range(spss.GetCaseCount()):
            internal_time = time.strptime(dataCursor.fetchone()[0], "%H:%M:%S")
            desired_time = time.strftime("%H:%M %p", internal_time)
            print have_time, internal_time, desired_time
            dataCursor.SetValueChar(out_time_var, desired_time)
            dataCursor.CommitCase()
    dataCursor.close()
    spss.Submit("add files / file = * / drop = %s." % temp_time_var )
format_to_ampm(in_time_var = "timevar", out_time_var = "blaah")
END PROGRAM.
exe.


--- On Wed, 7/29/09, Keval Khichadia <[hidden email]> wrote:

> From: Keval Khichadia <[hidden email]>
> Subject: Time Formats
> To: [hidden email]
> Date: Wednesday, July 29, 2009, 9:38 PM
> Hi,
>
> I have a variable start_time that holds the start time
> of a class. The format is in for example,
>
> start_time
>
> 1-Jan-1900 09:25:00
> 1-Jan-1900 14:15:00
>
> I used the below syntax:
>
> Compute StartTime = XDATE.TIME(START_TIME).
> formats StartTime (TIME5).
>
> and the output gives
>
> 9:25
> 14:15
>
> Is there a way I can get it to read like
>
> 9:25 A.M.
> 2:15 P.M
>
> Thanks,
> Keval
>
>
>
>
>

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD