Techno Blender
Digitally Yours.

Dealing with Dates in Python. This article is about the possible… | by KahEm Chu | Oct, 2022

0 50


This article is about the possible manipulation you can perform on the DateTime variables.

Photo by Estée Janssens on Unsplash

Do you struggle when dealing with DateTime objects? Well, I have to admit that I struggle frequently. I always need to do plenty of searching to find suitable methods for my use case. Then, I decided to write this article, as documentation for my dear readers and myself.

When I am developing the tool to automate report preparation or Excel file combination, I will have to be able to identify the info from the filenames or the folder. Normally the system-generated files or periodic reports will be named according to a fixed pattern and will be stored in the same folders.

The filename usually is the combination of the report name, the date or period of the report and the file extension—for example, a periodic report named “Electric Consumption Report 2022_Q4_WW43.xlsx” and a daily report named “Tools Requisition Report 20221021.csv”. To retrieve the correct files, we will need to calculate the date or period in the filename based on the time the report automation tool is running.

With that, this article will be structured as below:

  1. Parsing and Formatting DateTime (strptime vs strftime)
  2. Extract Year / Month / Day Info
  3. Calculate World Week from Date
  4. Calculate Week Day from Date
  5. Convert DateTime Object to Period
  6. Calculate Data Time Interval

Let’s get started!

Parsing DateTime means we transform a string object which contained the date into a DateTime object. For example, when we get the date from the report “Tools Requisition Report 20221021.csv” with Regular Expression or any other method, the date “20221021” will be a string variable.

After we parse it, it will become a DateTime object and will be written in ISO format (YYYY-MM-DD), 2022–10–21. Then, we can format it into a specific format, like October 21, 2022. Do note that the DateTime object will become a string object after we formatted it.

Confusing? Don’t worry!

You will have a clearer picture from the examples below.

Parsing DateTime

There are two methods from the DateTime library to parse the date:

  • datetime.fromisoformat()
  • datetime.strptime()

Let’s see what’s the difference between them!

import datetime as dtmy_date1 = "2022-07-01"
print(my_date1)
print(type(my_date1))
my_date2 = "01-07-2022"
print(my_date2)
print(type(my_date2))
my_date3 = "01July2022"
print(my_date3)
print(type(my_date3))

Okay, so I created 3 different dates as variables, they are string objects at the moment. Let’s parse them into the DateTime object now.

my_date1a = dt.datetime.fromisoformat(my_date1)
print(type(my_date1a))
my_date3a = dt.datetime.fromisoformat(my_date3)
print(type(my_date3a))
Image by Author.

For the first method, datetime.fromisoformat(), just like the method name, it only can parse the date which is in ISO format, YYYY-MM-DD, as in the variable named my_date1. Hence, when we try to use this method for other DateTime formats as in my_date3, it will return a Value Error. The my_date1a variable is the DateTime object we get by parsing the my_date1 variable.

Below is an example of parsing the date with datetime.strptime() method. For this method, we have to specify the format code based on the date format to parse the date. You may read more about the format code at strftime() and strptime() Behavior.

# my_date2 = "01-07-2022"
my_date2a = dt.datetime.strptime(my_date2, "%d-%m-%Y")
print(type(my_date2a))
print(my_date2a)
# Output:
# <class 'datetime.datetime'>
# 2022-07-03 00:00:00

Let’s see another example with a different date format.

# my_date3 = "01July2022"
my_date3a = dt.datetime.strptime(my_date3, "%d%B%Y")
print(type(my_date3a))
print(my_date3a)
# Output:
# <class 'datetime.datetime'>
# 2022-07-01 00:00:00

Parsing the date with strptime() method is by replacing the day, the month and the year with the respective format codes. As shown in the example above, %d for the day, %m for the month digit, %B for the month’s full name and %Y for the year with century.

Well, it might look very complicated for now if you are new to this, but I assure you that you will be great once you are familiar with the format code. Remember, you can always refer to strftime() and strptime() Behavior. 😉

Formatting DateTime

After we parse the string object into the DateTime object, it will be shown in ISO format. If you wish it to be in other forms, we have to use datetime.strftime() method to format the date.

# my_date3a: 2022-07-01 00:00:00
my_format_date = dt.datetime.strftime(my_date3a, "%B %d, %Y")
print(my_format_date)
# Output:
# July 01, 2022

Do note that after we format the date, it will become a string.

After we parse the string into the DateTime object, we can get info from it.

Note: The example below uses the variable from the example in the first section.

To obtain the year, month and day info, we just have to use the respective attribute below from the DateTime object.

  • datetime_object.year
  • datetime_object.month
  • datetime_object.day
# my_date3a: 2022-07-01 00:00:00# Get Year Info
my_date3a.year
# Output:
# 2022
# Get Month Info
my_date3a.month
# Output:
# 7
# Get Day Info
my_date3a.day
# Output:
# 1

Simple right? 😄

However, we are not able to extract the info above from a formatted date.

# my_format_date = "July 01, 2022"
my_format_date.month
Image by Author.

It will return an Attribute Error. This is because when we formatted the date into other formats, it will become a string object again. We can only return the DateTime attributes from a DateTime object.

print(type(my_date3a))
print(type(my_format_date))
Output:
<class 'datetime.datetime'>
<class 'str'>

Be aware that the product of strftime() is a string object, while the product of strptime() is a DateTime object.

Note: In this section, we will use new variables for the example.

First, I create two new string objects with different dates and then parse them into the DateTime objects.

import datetime as dtmy_date_str_1 = "2022-07-01"
my_date_1 = dt.datetime.strptime(my_date_str_1, "%Y-%m-%d")
print(my_date_1)
my_date_str_2 = "2022-07-03"
my_date_2 = dt.datetime.strptime(my_date_str_2, "%Y-%m-%d")
print(my_date_2)
# Output:
# 2022-07-01 00:00:00
# 2022-07-03 00:00:00

We will use the isocalendar() method to get the world week info from the DateTime object. This is because the DateTime object does not have a world week attribute.

print(my_date_1.isocalendar())
print(my_date_2.isocalendar())
# Output:
# datetime.IsoCalendarDate(year=2022, week=26, weekday=5)
# datetime.IsoCalendarDate(year=2022, week=26, weekday=7)

The isocalendar() method will return a tuple that contains the ISO year, the week number and the weekday. The weekday will be returned as a number. We can return the value with the respective index.

print("Date 1: 2022-07-01")
print("Year:", my_date_1.isocalendar()[0])
print("World Week Number: ", my_date_1.isocalendar()[1])
print("Weekday: ", my_date_1.isocalendar()[2])
print("Date 2: 2022-07-03")
print("Year:", my_date_2.isocalendar()[0])
print("World Week Number: ", my_date_2.isocalendar()[1])
print("Weekday: ", my_date_2.isocalendar()[2])
# Output:
# Date 1: 2022-07-01
# Year: 2022
# World Week Number: 26
# Weekday: 5
# Date 2: 2022-07-03
# Year: 2022
# World Week Number: 26
# Weekday: 7

That’s how we get the week number, also the year and the weekday.

Note: The example below uses the variable from the example in section 3.

There is more than one method to return the weekday info from a date. One of the methods is via the isocalendar() as shown in the previous section. Another method is using the weekday() method shown below.

print("Date 1: 2022-07-01")
print("Weekday: ", my_date_1.weekday())
print("Date 2: 2022-07-03")
print("Weekday: ", my_date_2.weekday())
# Output:
# Date 1: 2022-07-01
# Weekday: 4
# Date 2: 2022-07-03
# Weekday: 6

Well, 2022–07–01 is Friday. The isocalendar() method does not follow the python index rule. So actually, both isocalendar() and weekday() methods start the counting on Monday, but isocalendar() uses an index starting from 1, while weekday() is a python function which starts from 0. The two methods mentioned return the weekday as numbers. There is one more method to do so, can you guess it🤔?

It’s the strftime() method.

We can get the name of the weekday by formatting the date with the respective format code.

date_weekday_1 = dt.datetime.strftime(my_date_1, "%a")
print(date_weekday_1)
date_weekday_2 = dt.datetime.strftime(my_date_2, "%a")
print(date_weekday_2)
# Output:
# Fri
# Sun

We can return the weekday’s abbreviated name by formatting them using the “%a” format code as shown in the example above or return the weekday’s full name by using the “%A” format code as shown in the example below.

date_weekday_1 = dt.datetime.strftime(my_date_1, "%A")
print(date_weekday_1)
date_weekday_2 = dt.datetime.strftime(my_date_2, "%A")
print(date_weekday_2)
# Output:
# Friday
# Sunday

We can also return the weekday as a number.

date_weekday_1 = dt.datetime.strftime(my_date_1, "%w")
print(date_weekday_1)
date_weekday_2 = dt.datetime.strftime(my_date_2, "%w")
print(date_weekday_2)
# Output:
# 5
# 0

Fun fact, when you use the strftime() method, the counting starts on Sunday and the index starts from 0, just as shown in the documentation.

For better understanding, I have consolidated the comparison table below to show the difference between the few methods that return weekdays as a number from the date that I showed above.

Differences between Methods to Return Weekday as Number from Date. Image by Author.

Note: In this section, we will use new variables for the example.

We already went through the methods and attributes to return the year, month, day, world week number, and weekday. If you still remember the example of the report name I gave at the beginning of this article, “Electric Consumption Report 2022_Q4_WW43.xlsx”, there is still one piece of info we have not yet obtained, which is the quarter.

To obtain the quarter from the date, we have to use the pandas library together with the DateTime library.

import pandas as pd
import datetime as dt
# pandas.Timestamp.to_period
date_1 = '2022-10-21'
timestamp_1 = pd.Timestamp(date_1)

First, we create a date as a string object and then convert it into a timestamp. After that, we can convert the timestamp into a period.

year_period = timestamp_1.to_period(freq='Y')
month_period = timestamp_1.to_period(freq='M')
week_period = timestamp_1.to_period(freq='W')
quarter_period = timestamp_1.to_period(freq='Q')
print("Year: ", year_period)
print("Month: ", month_period)
print("Week: ", week_period)
print("Quarter: ", quarter_period)
# Output:
# Year: 2022
# Month: 2022-10
# Week: 2022-10-17/2022-10-23
# Quarter: 2022Q4

It’s quite simple, right?

According to pandas’ official documentation, there are only 4 types of output for the pandas.Timestamp.to_period() method. In previous sections, we get the year, month and week separately. Then, this method returns the specific period of the date instead. For example, 2022Q4 refers to the 4th quarter of the year 2022.

There is one more step required to retrieve only “Q4” instead of “2022Q4”. The quarter_period variable is a period object now. So, we need to convert it into a string object and then return the last two strings to get the “Q4”.

print(str(quarter_period))[-2:]

Besides that, we can define a python function to indicate the quarter for each month. This method is also applicable when your organization have own financial year calculation method. For example, the first quarter might fall in November, December and January.

# Output:
# Q4

The above shows how to return the quarter when your organization do not follow the standard quarter definition. You may modify the condition according to the quarter definition of your organization.

Note: In this section, we will use new variables for the example.

There are two types of calculation for the DateTime interval:

  1. Calculate the interval between two dates
  2. Add/minus a time interval to the date

Let’s see them one by one!

Before that, let’s create some dates for the example.

import datetime as dtmy_date1 = dt.datetime.fromisoformat("2022-07-01")
my_date2 = dt.datetime.fromisoformat("2022-07-05")

Calculate the interval between two dates

It’s really simple to calculate the interval between two dates. We just need minus one date from another.

print(my_date2 - my_date1)# Output:
# datetime.timedelta(days=4)

The output will be a timedelta, which refers to the difference between two DateTime objects.

Add/minus a time interval to the date

Another example is to add or minus a time interval to the date.

from datetime import timedeltaprint(my_date2 - timedelta(days=10))
print(my_date2 + timedelta(days=10))
print(my_date2 - timedelta(seconds=10))
print(my_date2 + timedelta(seconds=10))
# Output:
# 2022-06-25 00:00:00
# 2022-07-15 00:00:00
# 2022-07-04 23:59:50
# 2022-07-05 00:00:10

We will use the timedelta class from the DateTime library for this operation. This class allow us to add/minus days, seconds or microseconds to/from the dates.

In conclusion, the methods of parsing the string object into the DateTime object and formatting the DateTime object into a specific format have been shown. Then, the ways to get the year, month, day, world week and also weekday from the date are discussed.

The way to convert a DateTime object into a period, like a year, a month in the year, and also a quarter in the year were presented. For organizations that have their financial year calculation method, the condition statement can be used to return the correct quarter.

Lastly, the two types of calculation for the DateTime interval, which calculates the interval between two dates and adds/minus a time interval to the date are explained.

With all the methods and examples I have shown, can you recreate the filenames below with today’s date (26/10/2022)? 😎 Drop your answer in the comment!

  • “Electric Consumption Report 2022_Q4_WW43.xlsx”
  • “Tools Requisition Report 20221021.csv”

Answer at the bottom of the article. Try it yourself before you check the answer! 😉

Thank you and congrats for reading to the end 😊!

Photo by Alexas_Fotos on Unsplash

Answer:

Hope you get it right! 😊


This article is about the possible manipulation you can perform on the DateTime variables.

Photo by Estée Janssens on Unsplash

Do you struggle when dealing with DateTime objects? Well, I have to admit that I struggle frequently. I always need to do plenty of searching to find suitable methods for my use case. Then, I decided to write this article, as documentation for my dear readers and myself.

When I am developing the tool to automate report preparation or Excel file combination, I will have to be able to identify the info from the filenames or the folder. Normally the system-generated files or periodic reports will be named according to a fixed pattern and will be stored in the same folders.

The filename usually is the combination of the report name, the date or period of the report and the file extension—for example, a periodic report named “Electric Consumption Report 2022_Q4_WW43.xlsx” and a daily report named “Tools Requisition Report 20221021.csv”. To retrieve the correct files, we will need to calculate the date or period in the filename based on the time the report automation tool is running.

With that, this article will be structured as below:

  1. Parsing and Formatting DateTime (strptime vs strftime)
  2. Extract Year / Month / Day Info
  3. Calculate World Week from Date
  4. Calculate Week Day from Date
  5. Convert DateTime Object to Period
  6. Calculate Data Time Interval

Let’s get started!

Parsing DateTime means we transform a string object which contained the date into a DateTime object. For example, when we get the date from the report “Tools Requisition Report 20221021.csv” with Regular Expression or any other method, the date “20221021” will be a string variable.

After we parse it, it will become a DateTime object and will be written in ISO format (YYYY-MM-DD), 2022–10–21. Then, we can format it into a specific format, like October 21, 2022. Do note that the DateTime object will become a string object after we formatted it.

Confusing? Don’t worry!

You will have a clearer picture from the examples below.

Parsing DateTime

There are two methods from the DateTime library to parse the date:

  • datetime.fromisoformat()
  • datetime.strptime()

Let’s see what’s the difference between them!

import datetime as dtmy_date1 = "2022-07-01"
print(my_date1)
print(type(my_date1))
my_date2 = "01-07-2022"
print(my_date2)
print(type(my_date2))
my_date3 = "01July2022"
print(my_date3)
print(type(my_date3))

Okay, so I created 3 different dates as variables, they are string objects at the moment. Let’s parse them into the DateTime object now.

my_date1a = dt.datetime.fromisoformat(my_date1)
print(type(my_date1a))
my_date3a = dt.datetime.fromisoformat(my_date3)
print(type(my_date3a))
Image by Author.

For the first method, datetime.fromisoformat(), just like the method name, it only can parse the date which is in ISO format, YYYY-MM-DD, as in the variable named my_date1. Hence, when we try to use this method for other DateTime formats as in my_date3, it will return a Value Error. The my_date1a variable is the DateTime object we get by parsing the my_date1 variable.

Below is an example of parsing the date with datetime.strptime() method. For this method, we have to specify the format code based on the date format to parse the date. You may read more about the format code at strftime() and strptime() Behavior.

# my_date2 = "01-07-2022"
my_date2a = dt.datetime.strptime(my_date2, "%d-%m-%Y")
print(type(my_date2a))
print(my_date2a)
# Output:
# <class 'datetime.datetime'>
# 2022-07-03 00:00:00

Let’s see another example with a different date format.

# my_date3 = "01July2022"
my_date3a = dt.datetime.strptime(my_date3, "%d%B%Y")
print(type(my_date3a))
print(my_date3a)
# Output:
# <class 'datetime.datetime'>
# 2022-07-01 00:00:00

Parsing the date with strptime() method is by replacing the day, the month and the year with the respective format codes. As shown in the example above, %d for the day, %m for the month digit, %B for the month’s full name and %Y for the year with century.

Well, it might look very complicated for now if you are new to this, but I assure you that you will be great once you are familiar with the format code. Remember, you can always refer to strftime() and strptime() Behavior. 😉

Formatting DateTime

After we parse the string object into the DateTime object, it will be shown in ISO format. If you wish it to be in other forms, we have to use datetime.strftime() method to format the date.

# my_date3a: 2022-07-01 00:00:00
my_format_date = dt.datetime.strftime(my_date3a, "%B %d, %Y")
print(my_format_date)
# Output:
# July 01, 2022

Do note that after we format the date, it will become a string.

After we parse the string into the DateTime object, we can get info from it.

Note: The example below uses the variable from the example in the first section.

To obtain the year, month and day info, we just have to use the respective attribute below from the DateTime object.

  • datetime_object.year
  • datetime_object.month
  • datetime_object.day
# my_date3a: 2022-07-01 00:00:00# Get Year Info
my_date3a.year
# Output:
# 2022
# Get Month Info
my_date3a.month
# Output:
# 7
# Get Day Info
my_date3a.day
# Output:
# 1

Simple right? 😄

However, we are not able to extract the info above from a formatted date.

# my_format_date = "July 01, 2022"
my_format_date.month
Image by Author.

It will return an Attribute Error. This is because when we formatted the date into other formats, it will become a string object again. We can only return the DateTime attributes from a DateTime object.

print(type(my_date3a))
print(type(my_format_date))
Output:
<class 'datetime.datetime'>
<class 'str'>

Be aware that the product of strftime() is a string object, while the product of strptime() is a DateTime object.

Note: In this section, we will use new variables for the example.

First, I create two new string objects with different dates and then parse them into the DateTime objects.

import datetime as dtmy_date_str_1 = "2022-07-01"
my_date_1 = dt.datetime.strptime(my_date_str_1, "%Y-%m-%d")
print(my_date_1)
my_date_str_2 = "2022-07-03"
my_date_2 = dt.datetime.strptime(my_date_str_2, "%Y-%m-%d")
print(my_date_2)
# Output:
# 2022-07-01 00:00:00
# 2022-07-03 00:00:00

We will use the isocalendar() method to get the world week info from the DateTime object. This is because the DateTime object does not have a world week attribute.

print(my_date_1.isocalendar())
print(my_date_2.isocalendar())
# Output:
# datetime.IsoCalendarDate(year=2022, week=26, weekday=5)
# datetime.IsoCalendarDate(year=2022, week=26, weekday=7)

The isocalendar() method will return a tuple that contains the ISO year, the week number and the weekday. The weekday will be returned as a number. We can return the value with the respective index.

print("Date 1: 2022-07-01")
print("Year:", my_date_1.isocalendar()[0])
print("World Week Number: ", my_date_1.isocalendar()[1])
print("Weekday: ", my_date_1.isocalendar()[2])
print("Date 2: 2022-07-03")
print("Year:", my_date_2.isocalendar()[0])
print("World Week Number: ", my_date_2.isocalendar()[1])
print("Weekday: ", my_date_2.isocalendar()[2])
# Output:
# Date 1: 2022-07-01
# Year: 2022
# World Week Number: 26
# Weekday: 5
# Date 2: 2022-07-03
# Year: 2022
# World Week Number: 26
# Weekday: 7

That’s how we get the week number, also the year and the weekday.

Note: The example below uses the variable from the example in section 3.

There is more than one method to return the weekday info from a date. One of the methods is via the isocalendar() as shown in the previous section. Another method is using the weekday() method shown below.

print("Date 1: 2022-07-01")
print("Weekday: ", my_date_1.weekday())
print("Date 2: 2022-07-03")
print("Weekday: ", my_date_2.weekday())
# Output:
# Date 1: 2022-07-01
# Weekday: 4
# Date 2: 2022-07-03
# Weekday: 6

Well, 2022–07–01 is Friday. The isocalendar() method does not follow the python index rule. So actually, both isocalendar() and weekday() methods start the counting on Monday, but isocalendar() uses an index starting from 1, while weekday() is a python function which starts from 0. The two methods mentioned return the weekday as numbers. There is one more method to do so, can you guess it🤔?

It’s the strftime() method.

We can get the name of the weekday by formatting the date with the respective format code.

date_weekday_1 = dt.datetime.strftime(my_date_1, "%a")
print(date_weekday_1)
date_weekday_2 = dt.datetime.strftime(my_date_2, "%a")
print(date_weekday_2)
# Output:
# Fri
# Sun

We can return the weekday’s abbreviated name by formatting them using the “%a” format code as shown in the example above or return the weekday’s full name by using the “%A” format code as shown in the example below.

date_weekday_1 = dt.datetime.strftime(my_date_1, "%A")
print(date_weekday_1)
date_weekday_2 = dt.datetime.strftime(my_date_2, "%A")
print(date_weekday_2)
# Output:
# Friday
# Sunday

We can also return the weekday as a number.

date_weekday_1 = dt.datetime.strftime(my_date_1, "%w")
print(date_weekday_1)
date_weekday_2 = dt.datetime.strftime(my_date_2, "%w")
print(date_weekday_2)
# Output:
# 5
# 0

Fun fact, when you use the strftime() method, the counting starts on Sunday and the index starts from 0, just as shown in the documentation.

For better understanding, I have consolidated the comparison table below to show the difference between the few methods that return weekdays as a number from the date that I showed above.

Differences between Methods to Return Weekday as Number from Date. Image by Author.

Note: In this section, we will use new variables for the example.

We already went through the methods and attributes to return the year, month, day, world week number, and weekday. If you still remember the example of the report name I gave at the beginning of this article, “Electric Consumption Report 2022_Q4_WW43.xlsx”, there is still one piece of info we have not yet obtained, which is the quarter.

To obtain the quarter from the date, we have to use the pandas library together with the DateTime library.

import pandas as pd
import datetime as dt
# pandas.Timestamp.to_period
date_1 = '2022-10-21'
timestamp_1 = pd.Timestamp(date_1)

First, we create a date as a string object and then convert it into a timestamp. After that, we can convert the timestamp into a period.

year_period = timestamp_1.to_period(freq='Y')
month_period = timestamp_1.to_period(freq='M')
week_period = timestamp_1.to_period(freq='W')
quarter_period = timestamp_1.to_period(freq='Q')
print("Year: ", year_period)
print("Month: ", month_period)
print("Week: ", week_period)
print("Quarter: ", quarter_period)
# Output:
# Year: 2022
# Month: 2022-10
# Week: 2022-10-17/2022-10-23
# Quarter: 2022Q4

It’s quite simple, right?

According to pandas’ official documentation, there are only 4 types of output for the pandas.Timestamp.to_period() method. In previous sections, we get the year, month and week separately. Then, this method returns the specific period of the date instead. For example, 2022Q4 refers to the 4th quarter of the year 2022.

There is one more step required to retrieve only “Q4” instead of “2022Q4”. The quarter_period variable is a period object now. So, we need to convert it into a string object and then return the last two strings to get the “Q4”.

print(str(quarter_period))[-2:]

Besides that, we can define a python function to indicate the quarter for each month. This method is also applicable when your organization have own financial year calculation method. For example, the first quarter might fall in November, December and January.

# Output:
# Q4

The above shows how to return the quarter when your organization do not follow the standard quarter definition. You may modify the condition according to the quarter definition of your organization.

Note: In this section, we will use new variables for the example.

There are two types of calculation for the DateTime interval:

  1. Calculate the interval between two dates
  2. Add/minus a time interval to the date

Let’s see them one by one!

Before that, let’s create some dates for the example.

import datetime as dtmy_date1 = dt.datetime.fromisoformat("2022-07-01")
my_date2 = dt.datetime.fromisoformat("2022-07-05")

Calculate the interval between two dates

It’s really simple to calculate the interval between two dates. We just need minus one date from another.

print(my_date2 - my_date1)# Output:
# datetime.timedelta(days=4)

The output will be a timedelta, which refers to the difference between two DateTime objects.

Add/minus a time interval to the date

Another example is to add or minus a time interval to the date.

from datetime import timedeltaprint(my_date2 - timedelta(days=10))
print(my_date2 + timedelta(days=10))
print(my_date2 - timedelta(seconds=10))
print(my_date2 + timedelta(seconds=10))
# Output:
# 2022-06-25 00:00:00
# 2022-07-15 00:00:00
# 2022-07-04 23:59:50
# 2022-07-05 00:00:10

We will use the timedelta class from the DateTime library for this operation. This class allow us to add/minus days, seconds or microseconds to/from the dates.

In conclusion, the methods of parsing the string object into the DateTime object and formatting the DateTime object into a specific format have been shown. Then, the ways to get the year, month, day, world week and also weekday from the date are discussed.

The way to convert a DateTime object into a period, like a year, a month in the year, and also a quarter in the year were presented. For organizations that have their financial year calculation method, the condition statement can be used to return the correct quarter.

Lastly, the two types of calculation for the DateTime interval, which calculates the interval between two dates and adds/minus a time interval to the date are explained.

With all the methods and examples I have shown, can you recreate the filenames below with today’s date (26/10/2022)? 😎 Drop your answer in the comment!

  • “Electric Consumption Report 2022_Q4_WW43.xlsx”
  • “Tools Requisition Report 20221021.csv”

Answer at the bottom of the article. Try it yourself before you check the answer! 😉

Thank you and congrats for reading to the end 😊!

Photo by Alexas_Fotos on Unsplash

Answer:

Hope you get it right! 😊

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment