1

I have a dataframe with two columns 'time1' and 'time2' with the format : "hh:mm:ss"

I want to create a new column that is the difference between 'time1' and 'time2'.

I have tried this :

 df.withColumn("diff",
                  datediff(
                         to_timestamp($"time1", "hh:mm:ss"),
                         to_timestamp($"time2", "hh:mm:ss")
                          )
            )

But it always return 0 for diff. What is the correct way to do this ?

data sample :

time1, time2
05:35:30, 05:35:12
07:30:55, 02:39:10
08:35:30, 09:36:10
04:35:30, 05:33:50

1 Answer 1

2

First of all the format should be "HH:mm:ss" Second datediff only returns diff in days

So if you want a diff in minutes:

df.withColumn("diffs", 
     (to_timestamp('time1, "HH:mm:ss").cast("bigint") - to_timestamp('time2, "HH:mm:ss")
         .cast("bigint")) / lit(60))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.