0

I have a scenario where I am getting a rundate value getting passed in AWS Glue job as 'YYYY-MM-DD' format.

Lets say 2021-04-19.

Now, I am readin this rundate as 'datetime.strptime(rundate, "%y-%m-%d")'

But now i want to create 2 variables out of it variable A and variable B such as-

Variable A= rundate- 2 weeks (should save it in YYYYMMDD format)

Variable B = rundate- 1 week (should save it in YYYYMMDD format)

and then use this variables in filtering the data in data frame.

Beginner
  • 71
  • 1
  • 3
  • 10

1 Answers1

0

Use datetime lib use timedelta to subtract weeks/days..etc from your rundate.

Example:

Using Python:

import datetime
varA=datetime.datetime.strftime(datetime.datetime.strptime(rundate, "%Y-%m-%d")-datetime.timedelta(days=7),"%Y-%m-%d")
#'2021-04-12'

varB=datetime.datetime.strftime(datetime.datetime.strptime(rundate, "%Y-%m-%d")-datetime.timedelta(days=14),"%Y-%m-%d")
#'2021-04-05'

Using pyspark's Spark session:

rundate='2021-04-19'
varA=spark.sql(f"select string(date_sub('{rundate}',7))").collect()[0][0]
#'2021-04-12'
varB=spark.sql(f"select string(date_sub('{rundate}',14))").collect()[0][0]
#'2021-04-05'
notNull
  • 30,258
  • 4
  • 35
  • 50