---
title: "Add today’s date to a C# prompt template"
url: https://recursiveai.org/onellm/cookbook/insert-current-date-into-prompt-template/
description: "Insert the current date into a C# prompt template with OneLlm and TimeProvider. Test the exact rendered date with a fixed clock, without calling a model."
version: "1.0.1"
---

# Add today’s date to a C# prompt template

**OneLLM** is a free, open-source LLM and agent library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneLlm) · [GitHub](https://github.com/recursive-ai-oss/onellm-csharp) · [Docs](https://recursiveai.org/onellm/docs/) · [Download example (.zip)](https://recursiveai.org/onellm/cookbook/insert-current-date-into-prompt-template/insert-current-date-into-prompt-template.zip)

The date comes from your app’s clock, not a model.

```shell
dotnet add package RecursiveAI.OneLlm
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneLlm.Templating;
var options = new TemplateRenderOptions
    { TimeProvider = TimeProvider.System };
Console.WriteLine(Template.Render(
    "Today: {{now | date \"2006-01-02\"}}",
    null, render: options));
```

`2006-01-02` is a Go-style layout meaning `yyyy-MM-dd`, not a fixed date.

On November 29, 2026, this printed:

```text
Today: 2026-11-29
```

Unzip [the example](https://recursiveai.org/onellm/cookbook/insert-current-date-into-prompt-template/insert-current-date-into-prompt-template.zip), then `dotnet run`.

## The full program

The download renders the same prompt but fixes the clock at July 4, 2030, in UTC. It checks the exact string and then shows the error you get when you omit the clock. Nothing is sent to a model.

```csharp
using OneLlm.Templating;

const string prompt = "Today: {{now | date \"2006-01-02\"}}";
var clock = new FixedClock(new DateTimeOffset(
    2030, 7, 4, 9, 5, 6, TimeSpan.Zero));
var options = new TemplateRenderOptions { TimeProvider = clock };
string rendered = Template.Render(prompt, null, render: options);
if (rendered != "Today: 2030-07-04")
    throw new InvalidOperationException($"Unexpected prompt: {rendered}");
Console.WriteLine(rendered);

try
{
    Template.Render(prompt, null);
    throw new InvalidOperationException("Expected a missing-clock error.");
}
catch (TemplateException error)
{
    Console.WriteLine(error.Detail);
}

sealed class FixedClock(DateTimeOffset utc) : TimeProvider
{
    public override DateTimeOffset GetUtcNow() => utc;
    public override TimeZoneInfo LocalTimeZone => TimeZoneInfo.Utc;
}
```

```text
Today: 2030-07-04
error calling now: no TimeProvider was given to the render, so there is no clock
```

If you call `now` without setting `TemplateRenderOptions.TimeProvider`, rendering throws `TemplateException`; it does not silently read the system clock.

## Variations

To format a timestamp you already have, pass it to `date` directly. No clock is required in this case.

```csharp
using OneLlm.Templating;
Console.WriteLine(Template.Render(
    """{{date "2006-01-02" "2030-07-04T09:05:06Z"}}""",
    null));
```

```text
2030-07-04
```

## Related recipes

[Render a prompt template](https://recursiveai.org/onellm/cookbook/render-prompt-template/) · [Fail on a missing prompt key](https://recursiveai.org/onellm/cookbook/fail-prompt-template-on-missing-key/)

---

## The short answer

Use `{{now | date "2006-01-02"}}` in the prompt and pass a `TimeProvider` to `Template.Render`. Use `TimeProvider.System` for the machine’s current local date. Use a fixed clock in a test so the expected string doesn’t change depending on when the test runs. Rendering produces text locally; it doesn’t call a model.

## How do I add today’s date to an LLM prompt?

Use `now` at render time and `date` to keep just the calendar date. The `|` sends the value of `now` into `date` as its last argument. The layout `2006-01-02` is Go template syntax: it means four-digit year, two-digit month, and two-digit day. It doesn’t make the date come out as January 2, 2006. Don’t substitute the .NET format string `yyyy-MM-dd`; see the [`date` entry in the template reference](https://recursiveai.org/onellm/docs/templating/#standard-functions).

Pick the clock based on what “today” means in your application. `now` uses the clock’s local time, so machines in different time zones can report different dates at the same instant. The fixed clock in the download overrides both the instant and the time zone. That keeps the test independent of the computer’s clock and local zone. If you’re reporting on a date from a record rather than today, pass the record’s timestamp to `date` as in the variation instead of deriving a new one at render time.
