Charming the Python: datetime
Tags: python, beginners
Published: 2019-12-18 18:00:53


*If coding tutorials with math examples are the bane of your existence, keep reading. This series uses relatable examples like dogs and cats.*

---
## Datetime

`datetime` is a module for handling date and time. If you need a quick refresher, [read here about importing modules](https://dev.to/vickilanger/charming-the-python-modules-2578 'more info about modules').

### Getting the datetime information

First, you need to import `datetime`.
```python
>>> from datetime import datetime
>>> print(datetime.now())
2019-12-18 13:33:11.223728
```
### Formating datetime output using strftime
`strftime` refers to string representations of time with formatting.

Formatting datetime output is done with many different codes. Here are a few common ones.

| Format Code | What it Does | Output |
|--|--|--|
| `%a` | Short Weekday | Tues |
| `%A` | Weekday | Tuesday |
| `%d` | Day of Month | 01, 02, …, 31 |
| `%b` | Short Month | Dec |
| `%B` | Month | December |
| `%y` | Short Year | 19 |
| `%Y` | Year | 2019 |
| `%p` | am/pm  | PM |
| `%H` | Hours (24-hour clock) | 00, 01, …, 23 |
| `%I` | Hours (12-hour clock) | 01, 02, …, 12 |
| `%M` | Minutes | 00, 01, …, 59 |
| `%S` | Seconds | 00, 01, …, 59 |

If you've been in the military, it's likely you used 24-hour time to avoid confusion. For example 1300 hrs would be the same as 1 pm. 
Here's an example using 24-hr time.
```python
>>> from datetime import datetime

>>> now = datetime.now()
>>> time = now.strftime("%H%M")
>>> print(time)
1353
```

```python
>>> from datetime import datetime

>>> another_time = now.strftime("Today is %A, %d %B %Y.")
>>> print(another_time)
Today is Wednesday, 18 December 2019.
```

For more [`strftime()` Format Codes, check out the docs](https://docs.python.org/3.9/library/datetime.html#strftime-and-strptime-format-codes)

### String to time using strptime
`strptime` takes strings and creates objects from them. 

Think of `strptime()` as being backwards from `strftime()`. Consider this example to be an intro to parsing data.
```python
>>> from datetime import datetime

>>> a_date = "Today is Wednesday, 18 December 2019."
>>> date_object = datetime.strptime(a_date, "Today is %A, %d %B %Y.")
>>> print(date_object)
2019-12-18 00:00:00
```
For more [`strptime()` Format Codes, check out the docs](https://docs.python.org/3.9/library/datetime.html#strftime-and-strptime-format-codes)

### Use `date` from datetime

```python
>>> from datetime import date

>>> print(date.today())  # return current local date
2019-12-18
```
### `time` object to represent time

```python
from datetime import time

>>> print(time(12,3))  # prints exactly what you give it, in time format
12:03:00
```
### Difference between two `datetime`s

```python
>>> from datetime import date

>>> now = datetime(year = 2019, month = 12, day = 18, hour = 12, minute = 13, second = 0)
>>> new_year = datetime(year = 2020, month = 1, day = 1, hour = 0, minute = 0, second = 0)
>>> remaining = new_year - now
>>> print(remaining)
13 days, 11:47:00
```

The below example shows the difference between to `date`s
```python
>>> from datetime import date

>>> today = date(year=2019, month=12, day=18)
>>> new_year = date(year=2020, month=1, day=1)
>>> time_left_in_year = new_year - today
>>> print(time_left_in_year)
14 days
```
---
For more info on the `datetime` module, check out [the Python docs](https://docs.python.org/3.9/library/datetime.html).

---
Series loosely based on {% post https://dev.to/asabeneh/30daysofpyton-challenge-43ii %}