# Dates and Times

This section discusses the operations on dates and times in the Python standard library.

### Basic dates and times

[datetime](https://docs.python.org/3/library/datetime.html) is a module that provides a number of classes for manipulating dates and times. For example, we can use it to create a date object, a time object, or a datetime object.

```python
from datetime import date, time, datetime
d1 = date.today() # today's date
t1 = time(12, 30, 00) # 12:30:00
dt1 = datetime.now() # current date and time
print(d1)
print(t1)
print(dt1)
```

Results are like this:

```
2020-11-18
12:30:00
2020-11-18 12:30:00.849000
```

These objects have some properties that can be accessed. For example, the date object has a `year`, `month`, and `day` property.

```python
print(d1.year)
print(d1.month)
print(d1.day)
```

The time object has a `hour`, `minute`, and `second` property.

```python
print(t1.hour)
print(t1.minute)
print(t1.second)
```

The datetime object has a `year`, `month`, `day`, `hour`, `minute`, and `second` property.

```python
print(dt1.year)
print(dt1.month)
print(dt1.day)
print(dt1.hour)
print(dt1.minute)
print(dt1.second)
```

We can use `weekday()` to get the day of the week for a date. For example, the day of the week for the date `2020-11-18` is `Friday`.

```python
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
print(days[d1.weekday()])
```

It will print `Friday`. The `weekday()` method returns an integer between 0 and 6, where 0 is Monday and 6 is Sunday.

`date`, `time`, and `datetime` objects are immutable. We need to use the `replace()` method to create a new object with a different value for one of the properties.

```python
d2 = d1.replace(year=2021)
print(d2)
```

This will print `2021-11-18`. We can also use the `replace()` method to change the time or datetime object.

### Date and time formatting

The main way to format dates and times is to use the `strftime()` method. For example, we can use the `strftime()` method to format the date `2020-11-18` as `November 18, 2020`. For more information, see [strftime()](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior).

An example of formatting a date, a time and a datetime object:

```python
from datetime import date, time, datetime
d1 = date(2020, 11, 18)
t1 = time(12, 30, 00)
dt1 = datetime(2020, 11, 18, 12, 30, 00)
print(d1.strftime('%B %d, %Y')) # November 18, 2020
print(t1.strftime('%I:%M %p')) # 12:30 PM
print(dt1.strftime('%B %d, %Y %I:%M %p')) # November 18, 2020 12:30 PM
```

### Calculating dates and times

We can perform calculations on dates and times using `timedelta()`. For example, we can use the `timedelta()` method to calculate the number of days between two dates. Dates and times can be compared using the `<`, `>`, `<=`, `>=`, `==`, and `!=` operators.

```python
from datetime import date, timedelta
d1 = date(2020, 11, 18)
d2 = date(2020, 12, 20)
delta = d2 - d1 # timedelta
print(delta.days) # days between d1 and d2
print(d1 > d2) # if d1 is after d2
print(d1 <= d2) # if d1 is before or on d2
```

Output is going to be `32`, `False`, and `True`. `timedelta()` can be used to perform date math. An example:

```python
from datetime import date, timedelta
now = datetime.now()
oneyear = timedelta(days=365) # one year, a timedelta object
oneweek = timedelta(weeks=1) # one week, a timedelta object
print(now + oneweek) # prints the datetime one week from now
print(now + oneyear) # prints the datetime one year from now
print(now - oneweek) # prints the datetime one week ago
```
