1

I have a date column in string. Date is as follows: SinkCreatedOn = 7/30/2022 5:21:55 PM

I am trying to parse to datetype using following code but always get null values. I have tried many different specific date formats i.e M/d/y as it says in new spark documentation

df = df.withColumn("test", F.to_date(F.to_timestamp(col("SinkCreatedOn"), "MM/dd/yyyy HH:mm:ss a")))

test returns null

I tried this code on a column that only included date and it worked. When timestamp is present it breaks.

Any help much appreciated

Thank you

Picture of Issue

0

1 Answer 1

1

You are using HH which is for 24 Hr date format. Instead, use h. Please refer SimpleDateFormat JavaDoc

The following works:

df = spark.createDataFrame([
  ["07/30/2022 05:21:55 PM"],
], ["SinkCreatedOn"])


df.withColumn("test", F.to_date(F.to_timestamp(F.col("SinkCreatedOn"), "M/d/yyyy h:mm:ss a"))).show()

+----------------------+----------+
|SinkCreatedOn         |test      |
+----------------------+----------+
|07/30/2022 05:21:55 PM|2022-07-30|
+----------------------+----------+
Sign up to request clarification or add additional context in comments.

2 Comments

the padding isn't really reqd. 'M/d/yyyy h:mm:ss a' will work just fine.
You are right. I tested and modified the answer. Thanks!

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.