بخشی از مجموعه

Several example codes

~1 دقیقه مطالعه • بروزرسانی ۶ مهر ۱۴۰۴

Program Overview

This Python program retrieves and displays the current system date and time.
It uses the datetime module, which provides powerful tools for working with time and date values.
The output includes the date, time, and a formatted timestamp.


Python Code:


from datetime import datetime

# Get current date and time
now = datetime.now()

# Display output
print("--- Current Date and Time ---")
print("Date:", now.date())
print("Time:", now.time())
print("Formatted:", now.strftime("%Y-%m-%d %H:%M:%S"))

Sample Output:


--- Current Date and Time ---  
Date: 2025-09-28  
Time: 13:18:00.123456  
Formatted: 2025-09-28 13:18:00

Explanation:

- datetime.now() retrieves the current system timestamp
- .date() and .time() extract the date and time separately
- strftime() formats the output for readability
- You can customize the format string to match your preferred style


نوشته و پژوهش شده توسط دکتر شاهین صیامی