seeking help with extract hours and seconds from date variables

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

seeking help with extract hours and seconds from date variables

thara vardhan-2

Dear list members

I have two date variables - imported from an excel file with the format under format/cell/custom (d/mm/yyyy h:mm) as follows:

Timefrom                              Timeto
5-Feb-2009 07:30:00.00      5-Feb-2009 17:30:29.00

I have to extract only the number of hours and seconds from each of them. Both the variables are in date format in SPSS.

Do I use the substring function?  Will that help if the variable is a date format? I tried to use date/time wizard but was not successful.

I would be grateful if someone could help me with this?

Many thanks

thara





_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

The information contained in this email is intended for the named recipient(s) 
only. It may contain private, confidential, copyright or legally privileged 
information.  If you are not the intended recipient or you have received this 
email by mistake, please reply to the author and delete this email immediately. 
You must not copy, print, forward or distribute this email, nor place reliance 
on its contents. This email and any attachment have been virus scanned. However, 
you are requested to conduct a virus scan as well.  No liability is accepted 
for any loss or damage resulting from a computer virus, or resulting from a delay
or defect in transmission of this email or any attached file. This email does not 
constitute a representation by the NSW Police Force unless the author is legally 
entitled to do so.


Reply | Threaded
Open this post in threaded view
|

Re: seeking help with extract hours and seconds from date variables

ariel barak
Hi Thara,

In this situation, it helps if the variable is a date variable. Using the XDATE commands is your best bet....here is an example.

Compute TimefromHour=XDATE.HOUR(Timefrom).
Compute TimefromSecond=XDATE.SECONDS(Timefrom).
Execute.

Best,
Ari

On Thu, Sep 24, 2009 at 8:47 PM, Thara Vardhan <[hidden email]> wrote:

Dear list members

I have two date variables - imported from an excel file with the format under format/cell/custom (d/mm/yyyy h:mm) as follows:

Timefrom                              Timeto
5-Feb-2009 07:30:00.00      5-Feb-2009 17:30:29.00

I have to extract only the number of hours and seconds from each of them. Both the variables are in date format in SPSS.

Do I use the substring function?  Will that help if the variable is a date format? I tried to use date/time wizard but was not successful.

I would be grateful if someone could help me with this?

Many thanks

thara





_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

The information contained in this email is intended for the named recipient(s) 
only. It may contain private, confidential, copyright or legally privileged 
information.  If you are not the intended recipient or you have received this 
email by mistake, please reply to the author and delete this email immediately. 
You must not copy, print, forward or distribute this email, nor place reliance 
on its contents. This email and any attachment have been virus scanned. However, 
you are requested to conduct a virus scan as well.  No liability is accepted 
for any loss or damage resulting from a computer virus, or resulting from a delay
or defect in transmission of this email or any attached file. This email does not 
constitute a representation by the NSW Police Force unless the author is legally 
entitled to do so.



Reply | Threaded
Open this post in threaded view
|

Re: seeking help with extract hours and seconds from date variables

Peck, Jon
In reply to this post by thara vardhan-2

The Xdate.time function will give you the time portion.  Then you can use the Ctime functions, such as Ctime.hours to get the hours, etc.

E.g.,

compute hours = ctime.hours(xdate.time(timefrom)).

 

This assumes, which seems likely from your description, that the variable is already a regular SPSS date/time variable.  That would not be a string, so the substring function would not be relevant.

 

HTH,

Jon Peck

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Thara Vardhan
Sent: Thursday, September 24, 2009 7:47 PM
To: [hidden email]
Subject: [SPSSX-L] seeking help with extract hours and seconds from date variables

 


Dear list members

I have two date variables - imported from an excel file with the format under format/cell/custom (d/mm/yyyy h:mm) as follows:

Timefrom                              Timeto
5-Feb-2009 07:30:00.00      5-Feb-2009 17:30:29.00

I have to extract only the number of hours and seconds from each of them. Both the variables are in date format in SPSS.

Do I use the substring function?  Will that help if the variable is a date format? I tried to use date/time wizard but was not successful.

I would be grateful if someone could help me with this?

Many thanks

thara




_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

The information contained in this email is intended for the named recipient(s) 
only. It may contain private, confidential, copyright or legally privileged 
information.  If you are not the intended recipient or you have received this 
email by mistake, please reply to the author and delete this email immediately. 
You must not copy, print, forward or distribute this email, nor place reliance 
on its contents. This email and any attachment have been virus scanned. However, 
you are requested to conduct a virus scan as well.  No liability is accepted 
for any loss or damage resulting from a computer virus, or resulting from a delay
or defect in transmission of this email or any attached file. This email does not 
constitute a representation by the NSW Police Force unless the author is legally 
entitled to do so.

 

Reply | Threaded
Open this post in threaded view
|

Reply - seeking help with extract hours and seconds from date variables

thara vardhan-2
In reply to this post by ariel barak

Hi Ariel

Thank you so much for helping me with the syntax problem. However when i tried the syntax you sent I get the result as follows:

time from hour   time from sec

7:00                0:00

where as the actual number of hours is 10 hours between

timefrom           timeto        
07:30:00.00      17:30:29.00

How do i overcome this?

 many thanks
thara
 




Ariel Barak <[hidden email]>
Sent by: "SPSSX(r) Discussion" <[hidden email]>

25/09/2009 11:58

Please respond to
Ariel Barak <[hidden email]>

To
[hidden email]
cc
Subject
Re: seeking help with extract hours and seconds from date              variables





Hi Thara,

In this situation, it helps if the variable is a date variable. Using the XDATE commands is your best bet....here is an example.

Compute TimefromHour=XDATE.HOUR(Timefrom).
Compute TimefromSecond=XDATE.SECONDS(Timefrom).
Execute.

Best,
Ari

On Thu, Sep 24, 2009 at 8:47 PM, Thara Vardhan <vard1tha@...> wrote:

Dear list members


I have two date variables - imported from an excel file with the format under format/cell/custom (d/mm/yyyy h:mm) as follows:


Timefrom                              Timeto

5-Feb-2009 07:30:00.00      5-Feb-2009 17:30:29.00


I have to extract only the number of hours and seconds from each of them.
Both the variables are in date format in SPSS.

Do I use the substring function?  Will that help if the variable is a date format? I tried to use date/time wizard but was not successful.


I would be grateful if someone could help me with this?


Many thanks


thara





_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

The information contained in this email is intended for the named recipient(s) 
only. It may contain private, confidential, copyright or legally privileged 
information.  If you are not the intended recipient or you have received this 
email by mistake, please reply to the author and delete this email immediately. 
You must not copy, print, forward or distribute this email, nor place reliance 
on its contents. This email and any attachment have been virus scanned. However, 
you are requested to conduct a virus scan as well.  No liability is accepted 
for any loss or damage resulting from a computer virus, or resulting from a delay
or defect in transmission of this email or any attached file. This email does not 
constitute a representation by the NSW Police Force unless the author is legally 
entitled to do so.





_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

All mail is subject to content scanning for possible violation of NSW Police Force
Electronic Messaging Policy. All NSW Police Force employees are required to
familiarise themselves with the content of the policy, found under Policies on the
NSW Police Force Intranet.







_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

The information contained in this email is intended for the named recipient(s) 
only. It may contain private, confidential, copyright or legally privileged 
information.  If you are not the intended recipient or you have received this 
email by mistake, please reply to the author and delete this email immediately. 
You must not copy, print, forward or distribute this email, nor place reliance 
on its contents. This email and any attachment have been virus scanned. However, 
you are requested to conduct a virus scan as well.  No liability is accepted 
for any loss or damage resulting from a computer virus, or resulting from a delay
or defect in transmission of this email or any attached file. This email does not 
constitute a representation by the NSW Police Force unless the author is legally 
entitled to do so.


Reply | Threaded
Open this post in threaded view
|

Re: Reply - seeking help with extract hours and seconds from date variables

ariel barak
Hi Thara,

Initially it sounded like you wanted to extract the hours and seconds from two date variable, which the code I sent did correctly (hours=7 and seconds =0 7:30:00.00). Now, it sounds like you're trying to calculate the difference between these two values. This is why it's usually more helpful when posters give the list some context regarding what they're trying to do, and why. I think you may want to explore the DATEDIFF command.

COMPUTE DifferenceinSeconds=DATEDIFF(timeto, timefrom, "seconds").
EXECUTE.

Keep in mind that DATEDIFF "calculates the difference between two date values and returns an integer (with any fraction component truncated) in the specified date/time units"

This code above will give you the number of seconds between the two dates, which you can also divide by 3600 to get the difference in hours if you don't want to truncate.

Hope this helps,
Ari


On Thu, Sep 24, 2009 at 9:22 PM, Thara Vardhan <[hidden email]> wrote:

Hi Ariel

Thank you so much for helping me with the syntax problem. However when i tried the syntax you sent I get the result as follows:

time from hour   time from sec

7:00                0:00

where as the actual number of hours is 10 hours between

timefrom           timeto        
07:30:00.00      17:30:29.00

How do i overcome this?

 many thanks
thara
 




Ariel Barak <[hidden email]>
Sent by: "SPSSX(r) Discussion" <[hidden email]>

25/09/2009 11:58

Please respond to
Ariel Barak <[hidden email]>

To
[hidden email]
cc
Subject
Re: seeking help with extract hours and seconds from date              variables





Hi Thara,

In this situation, it helps if the variable is a date variable. Using the XDATE commands is your best bet....here is an example.

Compute TimefromHour=XDATE.HOUR(Timefrom).
Compute TimefromSecond=XDATE.SECONDS(Timefrom).
Execute.

Best,
Ari

On Thu, Sep 24, 2009 at 8:47 PM, Thara Vardhan <[hidden email]> wrote:

Dear list members


I have two date variables - imported from an excel file with the format under format/cell/custom (d/mm/yyyy h:mm) as follows:


Timefrom                              Timeto

5-Feb-2009 07:30:00.00      5-Feb-2009 17:30:29.00


I have to extract only the number of hours and seconds from each of them.
Both the variables are in date format in SPSS.

Do I use the substring function?  Will that help if the variable is a date format? I tried to use date/time wizard but was not successful.


I would be grateful if someone could help me with this?


Many thanks


thara





_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

The information contained in this email is intended for the named recipient(s) 
only. It may contain private, confidential, copyright or legally privileged 
information.  If you are not the intended recipient or you have received this 
email by mistake, please reply to the author and delete this email immediately. 
You must not copy, print, forward or distribute this email, nor place reliance 
on its contents. This email and any attachment have been virus scanned. However, 
you are requested to conduct a virus scan as well.  No liability is accepted 
for any loss or damage resulting from a computer virus, or resulting from a delay
or defect in transmission of this email or any attached file. This email does not 
constitute a representation by the NSW Police Force unless the author is legally 
entitled to do so.





_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

All mail is subject to content scanning for possible violation of NSW Police Force
Electronic Messaging Policy. All NSW Police Force employees are required to
familiarise themselves with the content of the policy, found under Policies on the
NSW Police Force Intranet.







_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

The information contained in this email is intended for the named recipient(s) 
only. It may contain private, confidential, copyright or legally privileged 
information.  If you are not the intended recipient or you have received this 
email by mistake, please reply to the author and delete this email immediately. 
You must not copy, print, forward or distribute this email, nor place reliance 
on its contents. This email and any attachment have been virus scanned. However, 
you are requested to conduct a virus scan as well.  No liability is accepted 
for any loss or damage resulting from a computer virus, or resulting from a delay
or defect in transmission of this email or any attached file. This email does not 
constitute a representation by the NSW Police Force unless the author is legally 
entitled to do so.