DateTimeDetector 0.0.13

dotnet add package DateTimeDetector --version 0.0.13
                    
NuGet\Install-Package DateTimeDetector -Version 0.0.13
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DateTimeDetector" Version="0.0.13" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DateTimeDetector" Version="0.0.13" />
                    
Directory.Packages.props
<PackageReference Include="DateTimeDetector" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DateTimeDetector --version 0.0.13
                    
#r "nuget: DateTimeDetector, 0.0.13"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DateTimeDetector@0.0.13
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DateTimeDetector&version=0.0.13
                    
Install as a Cake Addin
#tool nuget:?package=DateTimeDetector&version=0.0.13
                    
Install as a Cake Tool

DateTimeDetector

A Roslyn analyzer that detects usage of System.DateTime and suggests replacing it with System.DateTimeOffset.

Why?

DateTime does not store time zone information, which can lead to subtle bugs — especially in distributed systems, serialization, and database interactions. DateTimeOffset preserves the offset from UTC and is generally the safer default choice.

See:

Installation

dotnet add package DateTimeDetector

Rules

Rule ID Category Severity Description
DT001 Reliability Warning Prefer DateTimeOffset over DateTime

Examples

// ❌ Flagged by DT001
DateTime now = DateTime.Now;
DateTime created = new DateTime();
List<DateTime> timestamps = new();

// ✅ After applying the code fix
DateTimeOffset now = DateTimeOffset.Now;
DateTimeOffset created = new DateTimeOffset();
List<DateTimeOffset> timestamps = new();

What gets detected

The analyzer flags any reference to System.DateTime including:

  • Variable declarations: DateTime x = ...
  • Parameters: void Method(DateTime value)
  • Return types: DateTime GetTime()
  • Properties and fields: DateTime Created { get; set; }
  • Static member access: DateTime.Now, DateTime.UtcNow
  • Object creation: new DateTime()
  • Generic type arguments: List<DateTime>

Custom types named DateTime in other namespaces are not flagged — only System.DateTime.

Limitations

  • Constructor argument signatures differ between DateTime and DateTimeOffset (e.g., new DateTime(2024, 1, 1) has no direct DateTimeOffset equivalent without an offset parameter). The analyzer will flag these, but the code fix may produce code that needs manual adjustment.
  • Some DateTime-specific members (e.g., DateTime.Today) have no DateTimeOffset equivalent. The fix replaces the type name but you may need to adjust member access.

Suppressing the diagnostic

If you intentionally need DateTime (e.g., for interop), suppress per-site:

#pragma warning disable DT001
DateTime required = SomeLegacyApi();
#pragma warning restore DT001

Or in .editorconfig:

[*.cs]
dotnet_diagnostic.DT001.severity = none
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.13 92 3/18/2026
0.0.12 88 3/18/2026
0.0.11 93 3/17/2026