1. The datetime library and Split function
In this lesson, we are going to explore a nifty package available in Python to help us manage dates - the datetime module, as well as take a look at the split function.
2. Types of conflicts
In finance, there is often confusion around dates. is 09-10 the 9th of October or the 10th of September? Sometimes you cannot tell just by looking.
This is an example of a regional difference, but there can be differences in punctuation as well, for example, dashes, lines, and others. Luckily in Python, there is a built-in library and module with functions that help us handle these differences. It is called the datetime module.
3. The datetime function
The datetime module is very easy to invoke. It can be invoked with the following code.
In this course, we will be exploring the datetime module of Python, specifically the datetime dot strptime() class method, which creates a datetime object from a string representing a date and time and a corresponding format string.
As an example, a date written in the following format, month day and year separated by dashes would identify the month day and year using the datetime function as follows, which you can check with the table on the right.
Therefore the full function would be written as follows, and the output when printing the month would correctly return 12.
4. Using the split() function
We will also be using the split function when working with formats and dates. This function helps python identify and split a date into parts, for example, dates that are split with a dash, space or forward slash sign and then assign them a string variable.
Look at this example, we split the date into day, month and year based on the split function and when we print the result then the correct value is returned.
5. Let's practice!
Now let's try working with datetime through some examples.