Getting Current Date & Time in PySpark | curdate(), now(), current_timestamp() #pysparktutorial

Getting Current Date & Time in PySpark | curdate(), now(), current_timestamp()

Getting Current Date & Time in PySpark

Learn how to retrieve current date and time in PySpark using built-in functions such as curdate(), now(), current_timestamp(), and more. Includes examples and outputs.

1. Import Required Libraries

from pyspark.sql import SparkSession
from pyspark.sql.functions import curdate, current_date, current_timestamp, now, localtimestamp, current_timezone

2. Create Spark Session

spark = SparkSession.builder.appName("DateTimeFunctionsDemo").getOrCreate()

3. Use Date and Time Functions

✅ curdate()

df = spark.range(1)
df.select(curdate().alias("cur_date")).show()
+----------+
| cur_date |
+----------+
|2025-04-08|
+----------+

✅ current_date()

df.select(current_date().alias("current_date")).show()
+-------------+
|current_date |
+-------------+
|2025-04-08   |
+-------------+

✅ current_timestamp()

df.select(current_timestamp().alias("current_timestamp")).show(truncate=False)
+-----------------------+
|current_timestamp      |
+-----------------------+
|2025-04-08 12:34:56.123|
+-----------------------+

✅ now()

df.select(now().alias("now")).show(truncate=False)
+-----------------------+
|now                    |
+-----------------------+
|2025-04-08 12:34:56.123|
+-----------------------+

✅ localtimestamp()

df.select(localtimestamp().alias("local_time")).show(truncate=False)
+-----------------------+
|local_time             |
+-----------------------+
|2025-04-08 12:34:56.123|
+-----------------------+

✅ current_timezone()

df.select(current_timezone().alias("timezone")).show(truncate=False)
+--------+
|timezone|
+--------+
|Etc/UTC |
+--------+

🎥 Watch Video Tutorial

Author: Aamir Shahzad | PySpark Tutorial Series

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.