-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDates.py
92 lines (63 loc) · 2.57 KB
/
Dates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#* Dates - Python
# In Python, you can work with dates using the `datetime` module. Here is a description
# basic how to work with dates in Python:
#? Obtaining the Current Date and Time:
from datetime import datetime
# Get the current date and time
current_date = datetime.now()
print("Current date and time:", current_date)
#? Date Formatting:
# You can format a date into a text string using the `strftime` method.
# Format the date as a string
formatted_string = current_date.strftime("%Y-%m-%d %H:%M:%S")
print("Formatted date:", formatted_string)
#*Here are some common formatting codes:
#* - `%Y`:
# Year with four digits.
#* - `%m`:
# Month as number (01, 02, ..., 12).
#* - `%d`:
# Day of the month (01, 02, ..., 31).
#* - `%H`:
# Time (00, 01, ..., 23).
#* - `%M`:
# Minutes (00, 01, ..., 59).
#* - `%S`:
# Seconds (00, 01, ..., 59).
#? String Parsing to Date:
# You can convert a string to a date using the `strptime` method.
from datetime import datetime
# Parsing a string to date
date_string = "2022-11-15 12:30:00"
parsed_date = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
print("Parsed date:", parsed_date)
#? Operations with Dates:
# You can perform mathematical operations on dates using the `timedelta` module.
from datetime import datetime, timedelta
# Add a day to the current date
one_day_later = current_date + timedelta(days=1)
print("Current date + one day:", one_day_later)
# Subtract a week from the current date
one_week_before = current_date - timedelta(weeks=1)
print("Current date - one week:", one_week_before)
#? Difference between Dates:
# You can calculate the difference between two dates using the subtraction operator.
from datetime import datetime
# Define two dates
start_date = datetime(2022, 1, 1)
end_date = datetime(2022, 12, 31)
# Calculate the difference
difference = end_date - start_date
print("Difference between dates:", difference)
print("Days difference:", difference.days)
#? Time Zones:
# If you need to work with time zones, you can use the `pytz` module.
from datetime import datetime
import pytz
# Get the current date and time in a specific time zone
timezone = pytz.timezone("America/New_York")
current_date_ny = datetime.now(timezone)
print("Current date and time in New York:", current_date_ny)
# Here are some basics on how to work with dates in Python using the module
# `datetime`. You can explore more features and options in the official Python documentation. Practice
# and experiment to strengthen your date handling skills in Python!.