Converting Time

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

Converting Time

Lorraine Britto
I would like to convert time to hours. for example in my time column (1:15 PM) I would like to convert it to 13:15.  Any help will be greatly appreciated.
 
Lorraine Britto
212-222-7691


-----------------------------------------
Visit www.nyc.gov/hhc

CONFIDENTIALITY NOTICE: The information in this E-Mail may be
confidential and may be legally privileged. It is intended solely
for the addressee(s). If you are not the intended recipient, any
disclosure, copying, distribution or any action taken or omitted to
be taken in reliance on this e-mail, is prohibited and may be
unlawful. If you have received this E-Mail message in error, notify
the sender by reply E-Mail and delete the message.

Reply | Threaded
Open this post in threaded view
|

Re: Converting Time

Melissa Ives
Assuming that the time and the AM/PM indicator are 2 variables
(otherwise, I believe SPSS used military time as you note below.)  Here
is syntax we use to convert--where a1a is the time value (1:15) and A1b
is the AM/PM indicator.

if (a1b='AM') miltime=a1a.
if (a1b='PM' and a1a lt time.hms(12,0,0)) miltime=a1a+time.hms(12,0,0).
if (a1b='PM' and a1a gt time.hms(12,0,0)) miltime=a1a.
if (a1a=time.hms(12,0,0)) miltime=a1a.
* format miltime(time5) do in variable view (Choose Date & hh:mm).
freq miltime.


Melissa

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Lorraine Britto
Sent: Thursday, September 27, 2007 10:21 AM
To: [hidden email]
Subject: [SPSSX-L] Converting Time

I would like to convert time to hours. for example in my time column
(1:15 PM) I would like to convert it to 13:15.  Any help will be greatly
appreciated.

Lorraine Britto
212-222-7691


-----------------------------------------
Visit www.nyc.gov/hhc

CONFIDENTIALITY NOTICE: The information in this E-Mail may be
confidential and may be legally privileged. It is intended solely for
the addressee(s). If you are not the intended recipient, any disclosure,
copying, distribution or any action taken or omitted to be taken in
reliance on this e-mail, is prohibited and may be unlawful. If you have
received this E-Mail message in error, notify the sender by reply E-Mail
and delete the message.



PRIVILEGED AND CONFIDENTIAL INFORMATION
This transmittal and any attachments may contain PRIVILEGED AND
CONFIDENTIAL information and is intended only for the use of the
addressee. If you are not the designated recipient, or an employee
or agent authorized to deliver such transmittals to the designated
recipient, you are hereby notified that any dissemination,
copying or publication of this transmittal is strictly prohibited. If
you have received this transmittal in error, please notify us
immediately by replying to the sender and delete this copy from your
system. You may also call us at (309) 827-6026 for assistance.
Reply | Threaded
Open this post in threaded view
|

Re: Converting Time

Richard Ristow
At 04:32 PM 9/27/2007, Melissa Ives wrote:

>Assuming that the time and the AM/PM indicator are 2 variables, Here
>is syntax we use to convert--where a1a is the time value (1:15) and
>A1b is the AM/PM indicator.
>
>if (a1b='AM') miltime=a1a.
>if (a1b='PM' and a1a lt time.hms(12,0,0))
>miltime=a1a+time.hms(12,0,0).
>if (a1b='PM' and a1a gt time.hms(12,0,0)) miltime=a1a.
>if (a1a=time.hms(12,0,0)) miltime=a1a.
>* format miltime(time5) do in variable view (Choose Date & hh:mm).

I believe that this code does not correctly handle times between
midnight and 1:00am. Following is a demo, plus two fix - one with an
extra IF, but I like the alternative DO IF logic. This is SPSS 14 draft
output (WRR:not saved separately):
........................
NUMERIC miltime (TIME5).
if (a1b='AM') miltime=a1a.
if (a1b='PM' and a1a lt time.hms(12,0,0)) miltime=a1a+time.hms(12,0,0).
if (a1b='PM' and a1a gt time.hms(12,0,0)) miltime=a1a.
if (a1a=time.hms(12,0,0)) miltime=a1a.

NUMERIC miltime2 (TIME5).
if (a1b='AM' & a1a GE TIME.HMS(12,0,0)) miltime2=a1a-time.hms(12,0,0).
IF (a1b='AM' & a1a LT TIME.HMS(12,0,0)) miltime2=a1a

if (a1b='PM' & a1a lt time.hms(12,0,0)) miltime2=a1a+time.hms(12,0,0).
if (a1b='PM' & a1a GE time.hms(12,0,0)) miltime2=a1a.

NUMERIC miltime3 (TIME5).
DO IF   a1b eq 'AM' AND a1a GE TIME.HMS(12).
.  COMPUTE miltime3 = a1a - TIME.HMS(12).
ELSE IF a1b eq 'AM'.
.  COMPUTE miltime3 = a1a.
ELSE IF a1b eq 'PM' AND a1a GE TIME.HMS(12).
.  COMPUTE miltime3 = a1a.
ELSE IF a1b eq 'PM'.
.  COMPUTE miltime3 = a1a + TIME.HMS(12).
END IF.

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |28-SEP-2007 13:49:58       |
|-----------------------------|---------------------------|
   a1a a1b miltime miltime2 miltime3

12:00 AM   12:00     0:00     0:00
12:34 AM   12:34     0:34     0:34
  6:54 AM    6:54     6:54     6:54
12:00 PM   12:00    12:00    12:00
12:34 PM   12:34    12:34    12:34
  6:54 PM   18:54    18:54    18:54

Number of cases read:  6    Number of cases listed:  6

===================
APPENDIX: Test data
===================
DATA LIST LIST
    /a1a (TIME5)
     a1b (A2).
BEGIN DATA
12:00 AM
12:34 AM
06:54 AM
12:00 PM
12:34 PM
06:54 PM
END DATA.
Reply | Threaded
Open this post in threaded view
|

Re: Converting Time

Richard Ristow
In reply to this post by Melissa Ives
At 09:41 AM 10/16/2007, Lorraine Britto wrote, off-list:

>I used the attached syntax as AM/PM were in the time variable.
>Reg_TM was the variable holding the Time with AM/PM
>Regist is the converted variable for military time.
>
>COMPUTE Regist=NUM(SUBSTR(Reg_TM,1,5),time).
>DO IF ((SUBSTR(Reg_TM,7,2)='PM')
>AND NOT (SUBSTR(Reg_TM,1,2)='12')).
>COMPUTE Regist=Regist+43200.
>Else IF
>((SUBSTR(Reg_TM,1,2)='12') AND (SUBSTR(Reg_TM,7,2)='AM')).
>COMPUTE Regist=Regist-43200.
>END IF.
>FORMATS Regist (time).

Fine so far; see demo, at end of this posting. In the demo, for  I've
changed formatting and indenting. I think "TIME.HMS(12)" is more
readable than "43200", for a time of '12 hours'.

>The issue I am now looking at is subtracting time, especially when
>Time A is 23:00:00 (10/1/2007) and Time B is 00:00:00 (10/2/2007). The
>time difference should be 1 hour.

If you have variables Time_A and Time_B that are both SPSS time
variables; if you want their difference, but occasionally Time_B looks
earlier than Time_A because Time_B is actually on the next day, try
this (untested):

NUMERIC TimeDiff(TIME5.)

COMPUTE TimeDiff = Time_B - Time_A.
IF     (TimeDiff LT 0)
         TimeDiff = TimeDiff + TIME.HMS(24).

==============================================
APPENDIX: Test and demo (SPSS 14 draft output)
==============================================
Test data (adapted from posting
Fri, 28 Sep 2007 13:52:44 -0400)
----------------------------------------------
DATA LIST FIXED
    /Reg_TM 01-08 (A).
BEGIN DATA
12:00 AM
12:34 AM
06:54 AM
12:00 PM
12:34 PM
06:54 PM
END DATA.
==============================================
Code and results:
----------------------------------------------
COMPUTE Regist=NUM(SUBSTR(Reg_TM,1,5),time).
DO IF   ((SUBSTR(Reg_TM,7,2)='PM')
  AND NOT (SUBSTR(Reg_TM,1,2)='12')).
.  COMPUTE Regist=Regist+TIME.HMS(12).
Else IF
         ((SUBSTR(Reg_TM,1,2)='12')
   AND    (SUBSTR(Reg_TM,7,2)='AM')).
COMPUTE Regist=Regist-TIME.HMS(12).
END IF.
FORMATS Regist (time).
LIST.

List
|-----------------------------|---------------------------|
|Output Created               |16-OCT-2007 13:26:44       |
|-----------------------------|---------------------------|
Reg_TM     Regist

12:00 AM  0:00:00
12:34 AM  0:34:00
06:54 AM  6:54:00
12:00 PM 12:00:00
12:34 PM 12:34:00
06:54 PM 18:54:00

Number of cases read:  6    Number of cases listed:  6

=====================
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: Converting Time

Lorraine Britto
Hi Richard,
The TimeDiff syntax you sent works well - Thanks again Lorraine

>>> Richard Ristow <[hidden email]> 10/16/2007 1:38 PM >>>
At 09:41 AM 10/16/2007, Lorraine Britto wrote, off-list:

>I used the attached syntax as AM/PM were in the time variable.
>Reg_TM was the variable holding the Time with AM/PM
>Regist is the converted variable for military time.
>
>COMPUTE Regist=NUM(SUBSTR(Reg_TM,1,5),time).
>DO IF ((SUBSTR(Reg_TM,7,2)='PM')
>AND NOT (SUBSTR(Reg_TM,1,2)='12')).
>COMPUTE Regist=Regist+43200.
>Else IF
>((SUBSTR(Reg_TM,1,2)='12') AND (SUBSTR(Reg_TM,7,2)='AM')).
>COMPUTE Regist=Regist-43200.
>END IF.
>FORMATS Regist (time).

Fine so far; see demo, at end of this posting. In the demo, for  I've
changed formatting and indenting. I think "TIME.HMS(12)" is more
readable than "43200", for a time of '12 hours'.

>The issue I am now looking at is subtracting time, especially when
>Time A is 23:00:00 (10/1/2007) and Time B is 00:00:00 (10/2/2007). The
>time difference should be 1 hour.

If you have variables Time_A and Time_B that are both SPSS time
variables; if you want their difference, but occasionally Time_B looks
earlier than Time_A because Time_B is actually on the next day, try
this (untested):

NUMERIC TimeDiff(TIME5.)

COMPUTE TimeDiff = Time_B - Time_A.
IF     (TimeDiff LT 0)
         TimeDiff = TimeDiff + TIME.HMS(24).

==============================================
APPENDIX: Test and demo (SPSS 14 draft output)
==============================================
Test data (adapted from posting
Fri, 28 Sep 2007 13:52:44 -0400)
----------------------------------------------
DATA LIST FIXED
    /Reg_TM 01-08 (A).
BEGIN DATA
12:00 AM
12:34 AM
06:54 AM
12:00 PM
12:34 PM
06:54 PM
END DATA.
==============================================
Code and results:
----------------------------------------------
COMPUTE Regist=NUM(SUBSTR(Reg_TM,1,5),time).
DO IF   ((SUBSTR(Reg_TM,7,2)='PM')
  AND NOT (SUBSTR(Reg_TM,1,2)='12')).
..  COMPUTE Regist=Regist+TIME.HMS(12).
Else IF
         ((SUBSTR(Reg_TM,1,2)='12')
   AND    (SUBSTR(Reg_TM,7,2)='AM')).
COMPUTE Regist=Regist-TIME.HMS(12).
END IF.
FORMATS Regist (time).
LIST.

List
|-----------------------------|---------------------------|
|Output Created               |16-OCT-2007 13:26:44       |
|-----------------------------|---------------------------|
Reg_TM     Regist

12:00 AM  0:00:00
12:34 AM  0:34:00
06:54 AM  6:54:00
12:00 PM 12:00:00
12:34 PM 12:34:00
06:54 PM 18:54:00

Number of cases read:  6    Number of cases listed:  6


-----------------------------------------
Visit www.nyc.gov/hhc

CONFIDENTIALITY NOTICE: The information in this E-Mail may be
confidential and may be legally privileged. It is intended solely
for the addressee(s). If you are not the intended recipient, any
disclosure, copying, distribution or any action taken or omitted to
be taken in reliance on this e-mail, is prohibited and may be
unlawful. If you have received this E-Mail message in error, notify
the sender by reply E-Mail and delete the message.

=====================
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