> ## Documentation Index
> Fetch the complete documentation index at: https://ancplua.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# AL0026 - Avoid DateTime time accessors

> DateTime.Now and DateTime.UtcNow make code difficult to test. Use TimeProvider instead.

Source: [AL0026AvoidDateTimeNowAnalyzer.cs](https://github.com/ANcpLua/ANcpLua.Analyzers/blob/main/src/ANcpLua.Analyzers/Analyzers/AL0026AvoidDateTimeNowAnalyzer.cs)

## Description

DateTime.Now and DateTime.UtcNow make code difficult to test. Use TimeProvider.System.GetLocalNow() or GetUtcNow() instead, which can be mocked in tests.

## Bad Code

```csharp theme={null}
var now = DateTime.Now;
var utcNow = DateTime.UtcNow;
```

## Good Code

```csharp theme={null}
var now = TimeProvider.System.GetLocalNow();
var utcNow = TimeProvider.System.GetUtcNow();
```

## Properties

* **Category**: Usage
* **Severity**: Warning
* **Enabled by default**: True
* **Code fix available**: False

## Configuration

```editorconfig theme={null}
dotnet_diagnostic.AL0026.severity = warning
```

## Notes

TimeProvider was introduced in .NET 8 and provides a testable abstraction for time-related operations. You can inject a custom TimeProvider in tests to control time behavior.
