Skip to content

Commit d2c62b7

Browse files
authored
Merge branch 'main' into Descriptive_Statistics
2 parents 5b74a4b + 5fe9a2d commit d2c62b7

17 files changed

+1304
-3
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
## Working with Dates and Times in Python
2+
Handling dates and times is an essential aspect of many programming tasks.
3+
Python provides robust modules to work with dates and times, making it easier to perform operations like formatting, parsing, and arithmetic.
4+
This guide provides an overview of these modules and their key functionalities.
5+
6+
## 1. 'datetime' Module
7+
The datetime module supplies classes for manipulating dates and times. The main classes in the datetime module are:
8+
9+
* date: Represents a date (year, month, day).
10+
* time: Represents a time (hour, minute, second, microsecond).
11+
* datetime: Combines date and time information.
12+
* timedelta: Represents the difference between two dates or times.
13+
* tzinfo: Provides time zone information objects.
14+
15+
**Key Concepts:**
16+
17+
* Naive vs. Aware: Naive datetime objects do not contain time zone information, while aware datetime objects do.
18+
* Immutability: date and time objects are immutable; once created, they cannot be changed.
19+
20+
Example:
21+
```bash
22+
import datetime
23+
# Get the current date and time
24+
now = datetime.datetime.now()
25+
print("Current date and time:", now)
26+
```
27+
28+
## 2. Formatting Dates and Times
29+
Formatting involves converting datetime objects into human-readable strings. This is achieved using the strftime method, which stands for "string format time."
30+
You can specify various format codes to dictate how the output string should be structured.
31+
32+
**Common Format Codes:**
33+
34+
* %Y: Year with century (e.g., 2024)
35+
* %m: Month as a zero-padded decimal number (e.g., 01)
36+
* %d: Day of the month as a zero-padded decimal number (e.g., 15)
37+
* %H: Hour (24-hour clock) as a zero-padded decimal number (e.g., 13)
38+
* %M: Minute as a zero-padded decimal number (e.g., 45)
39+
* %S: Second as a zero-padded decimal number (e.g., 30)
40+
41+
Example:
42+
```bash
43+
import datetime
44+
45+
now = datetime.datetime.now()
46+
formatted_now = now.strftime("%Y-%m-%d %H:%M:%S")
47+
print("Formatted current date and time:", formatted_now)
48+
```
49+
50+
## 3. Parsing Dates and Times
51+
Parsing is the process of converting strings representing dates and times into datetime objects. The strptime method, which stands for "string parse time,"
52+
allows you to specify the format of the input string.
53+
54+
Example:
55+
```bash
56+
import datetime
57+
58+
date_string = "2024-05-15 13:45:30"
59+
date_object = datetime.datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
60+
print("Parsed date and time:", date_object)
61+
```
62+
63+
## 4. Working with Time Differences
64+
The timedelta class is used to represent the difference between two datetime objects. This is useful for calculations involving durations, such as finding the
65+
number of days between two dates or adding a certain period to a date.
66+
67+
Example:
68+
```bash
69+
import datetime
70+
71+
date1 = datetime.datetime(2024, 5, 15, 12, 0, 0)
72+
date2 = datetime.datetime(2024, 5, 20, 14, 30, 0)
73+
74+
difference = date2 - date1
75+
print("Difference:", difference)
76+
print("Days:", difference.days)
77+
print("Total seconds:", difference.total_seconds())
78+
```
79+
80+
## 5. Time Zones
81+
Time zone handling in Python is facilitated by the pytz library. It allows you to convert naive datetime objects into timezone-aware objects and perform
82+
operations across different time zones.
83+
84+
**Key Concepts:**
85+
86+
* Timezone-aware: A datetime object that includes timezone information.
87+
* Localization: The process of associating a naive datetime with a time zone.
88+
89+
Example:
90+
```bash
91+
import datetime
92+
import pytz
93+
94+
# Define a timezone
95+
tz = pytz.timezone('Asia/Kolkata')
96+
97+
# Get the current time in a specific timezone
98+
now = datetime.datetime.now(tz)
99+
print("Current time in Asia/Kolkata:", now)
100+
```
101+
102+
## 6. Date Arithmetic
103+
Date arithmetic involves performing operations like addition or subtraction on date or datetime objects using timedelta. This is useful for calculating future
104+
or past dates based on a given date.
105+
106+
Example:
107+
```bash
108+
import datetime
109+
110+
today = datetime.date.today()
111+
future_date = today + datetime.timedelta(days=10)
112+
print("Date after 10 days:", future_date)
113+
```
114+
115+
## Summary
116+
Python’s datetime module and the pytz library provide comprehensive tools for working with dates, times, and time zones. They enable you to perform a wide range
117+
of operations, from basic date manipulations to complex time zone conversions.

contrib/advanced-python/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# List of sections
22

33
- [Decorators/\*args/**kwargs](decorator-kwargs-args.md)
4+
- [Working with Dates & Times in Python](dates_and_times.md)
5+
- [Regular Expressions in Python](regular_expressions.md)
6+
- [JSON module](json-module.md)

0 commit comments

Comments
 (0)