> For the complete documentation index, see [llms.txt](https://digitalgarden.batamladen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://digitalgarden.batamladen.com/notes/query-languages/kql/working-with-time-in-kql.md).

# Working with time in KQL

You want to define the time at the beginning at the query since it will drasticaly cut off a lot of logs.

It is used with the `where` command and the `TimeGenerated` field and a time command (ago, between, now\...)

### ago

if used with > the ago will return logs NEWER than time defined.

if used with < the ago will return logs OLDER than time defined.

#### syntax

```
where TimeGenerated [operator] ago([number][d=days, m=minutes, s=seconds])
```

Example:

```
FileCreationEvents
| where TimeGenerated > ago(1d)
```

result: empty

example:

```
FileCreationEvents
| where TimeGenerated < ago(1d)
```

result:

|                            |              |               |
| -------------------------- | ------------ | ------------- |
| 2022-01-03 20:08:12.451382 | B5HS-LAPTOP  | 15.jpg        |
| 2022-01-03 20:09:25.180969 | AXFE-DESKTOP | resources.pri |
| etc                        | etc          | etc           |

***

### between

the kql accept a few options to define the date time.

#### syntax:

```
where TimeGenerated [operator] between (datetime(date) .. datetime(date))
```

#### Example

```
SignInLOgs
| where TimeGenerated between (datetime(2025-7-5T02:00:00) .. datetime(2025-7-6T15:00:00))
```

You can define only date no time

```
SignInLOgs
| where TimeGenerated between (datetime(2025-7-5) .. datetime(2025-7-6))
```

***

### now

now function means... now like... current time.

#### Syntax

```
where TimeGenerated between (datetime() .. now())
```

#### Example

```
SignInLOgs
| where TimeGenerated between (datetime(2025-7-5) .. now())
```

```
SignInLOgs
| where TimeGenerated between (datetime(2025-7-5) .. now(-4))
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://digitalgarden.batamladen.com/notes/query-languages/kql/working-with-time-in-kql.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
