Duration

Working with dates and times

In the previous lesson, you executed code to create a Test node.

Execute this code to create or update the Test node with these date and datetime values:

cypher
MERGE (x:Test {id: 1})
SET
x.date = date(),
x.datetime = datetime(),
x.timestamp = timestamp(),
x.date1 = date('2022-04-08'),
x.date2 = date('2022-09-20'),
x.datetime1 = datetime('2022-02-02T15:25:33'),
x.datetime2 = datetime('2022-02-02T22:06:12')
RETURN x

Calculate days between two dates

Write a query to retrieve this Test node and calculate the number of days between date1 and date2.

Enter the number of days:

  • ✓ 165

Hint

Did you create the Test node with the date1 and date2 values?

Use duration.inDays and extract the days value.

How many days are between date1 and date2?

Once you have entered the answer, click the Try Again button below to continue.

Solution

Before you calculate the duration, make sure you have executed this code to create or update the Test node:

cypher
MERGE (x:Test {id: 1})
SET
x.date = date(),
x.datetime = datetime(),
x.timestamp = timestamp(),
x.date1 = date('2022-04-08'),
x.date2 = date('2022-09-20'),
x.datetime1 = datetime('2022-02-02T15:25:33'),
x.datetime2 = datetime('2022-02-02T22:06:12')
RETURN x

You can run the following query to find the answer:

cypher
// calculate the duration
MATCH (x:Test)
RETURN duration.inDays(x.date1,x.date2).days

How many days are between date1 and date2?

Once you have entered the answer, click the Try Again button below to continue.

Summary

In this challenge, you wrote a query to calculate the number of days between two dates.

In the next challenge, you will write another query related to datetime data.