How to convert only the date without time to a string in Python

1 Answer

0 votes
from datetime import date

# Convert a date to a string (YYYY-MM-DD)
def date_to_string(d: date) -> str:
    return d.strftime("%Y-%m-%d")

# Build a date from integers
def make_date(y: int, m: int, d: int) -> date:
    return date(y, m, d)

def main():
    # 1. Today's date
    today = date.today()
    print("Today's date is:", date_to_string(today))

    # 2. Hard-coded date
    my_date = make_date(2025, 12, 7)
    print("Hard-coded date is:", date_to_string(my_date))

if __name__ == "__main__":
    main()



"""
run:

Today's date is: 2026-05-30
Hard-coded date is: 2025-12-07

"""

 



answered 8 hours ago by avibootz

Related questions

...