You can download this code by clicking the button below.
This code is now available for download.
The function takes a date string and a number of days as input, then randomly shifts the date forward or backward by the specified number of days.
Technology Stack : Freezegun, datetime
Code Type : Function
Code Difficulty : Intermediate
import random
from freezegun import freeze_time
import datetime
def random_date_shift(date_str, days):
"""
Shifts a given date string by a random number of days.
"""
date = datetime.datetime.strptime(date_str, "%Y-%m-%d")
random_days = random.randint(-30, 30)
shifted_date = date + datetime.timedelta(days=random_days)
return shifted_date.strftime("%Y-%m-%d")
# Code Information