# Format the date and time fields

## Use the date and time fields

In `.cshtml` views the date field is automatically formatted. To take advantage of the automatic date format, in Sitefinity ASP.NET Core Renderer, you must reference the date in the views, using the `FormatDate` HTML helper.

To do this, in the `.cshtml` files, render the date object in the following way:

```Razor
@using Progress.Sitefinity.AspNetCore.Mvc.Rendering

<div id="date">
    @Html.FormatDate(DateTime.UtcNow, this.Context)
</div>
```

## Date and time calculation

On the frontend, the ASP.NET Core Renderer supports the following ways for calculating date and time:

* Calculate the date server-side\
  This is the default behavior of the ASP.NET Core Renderer.\
  All the dates are rendered according to the current culture of the page and the time zone of the current site in Sitefinity CMS.
* Calculate the date client-side\
  The date and time can also be calculated on the client, using the client browsers time zone information.

## Modify the server-side date and time format

The default format for these dates is `"d MMM, hh:mm tt"`. You can change this, by modifying the `ServerDateFormat` property in the `appSettings.json` file of the Renderer in the following way:

```JSON
"Sitefinity": {
  "Url": "https://yoursitefinitywebsiteurl",
  "WebServicePath": "api/default",
  "ServerDateFormat": "dd-mm-yyyy"
}
```

## Customize the default formatting logic

You can replace the formatting logic entirely, by providing a custom implementation of the `Progress.Sitefinity.AspNetCore.Mvc.Rendering.IDateTimeFormatter` interface in the following way:

```C#
using Microsoft.AspNetCore.Http;
using System;

namespace StefinityWebApp
{
    /// <summary>
    /// The date time formatter used to format frontend dates.
    /// </summary>
    public interface IDateTimeFormatter
    {
        /// <summary>
        /// Formats the <see cref="DateTime"/> for the frontend.
        /// </summary>
        /// <param name="dateTime">The <see cref="DateTime"/> object. Must be supplied in UTC.</param>
        /// <param name="httpContext">The http context.</param>
        /// <returns>A formatted date time string.</returns>
        string Format(DateTime dateTime, HttpContext httpContext);
    }
}
```

## Enable client-side date and time calculation

To switch to client-side date and time calculation, you must modify the `DateFormatSource` property in the `appSettings.json` file of the Renderer in the following way:

```JSON
"Sitefinity": {
  "Url": "https://yoursitefinitywebsiteurl",
  "WebServicePath": "api/default",
  "DateFormatSource": "Client"
}
```

This automatically adds a script on the frontend of each widget that has a call to `@Html.FormatDate`. This script traverses the entire page and looks for a specific marker to convert the dates.

## Customize the client-side date and time format

To replace the default formatting logic on the client, you must include a script tag like the following:

```Razor
<script id="sfDateOptions" type="application/json">
    {
    "weekday": "long", 
    "year": "numeric", 
    "month": "long", 
    "day": "numeric" 
    }
</script>
```

> info **RECOMMENDATION**: We recommend placing the `<script>` tag in the `<head>` tag of your root layout file.

> info **NOTE**: The language for the formatting is read from the `html` tag. Therefore, it is important to set the `lang` attribute. The Renderer pages have a dynamically generated `lang` attribute, based on the requested language of the page.
