This is a .Net Wrapper for date-fns Javascript library to be used with Blazor WASM and Server.
The methods have the same signature as the Javascript library so for more documentation you can check it at date-fns
Install the Package from Nuget.org
$ dotnet add package Date.Fns.Net --version 0.2.0
- Register of the dependency in a service container. In the Program.cs
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
//Add the below line
builder.Services.AddDateService(); /*Add this line*/
await builder.Build().RunAsync();
}
}
- In the index.html file located at wwwroot folder, add the following line
<script src="_content/Date.Fns.Net/date-fns.js"></script>
- In your Imports.razor add this two lines :
@using Date.Fns.Net
@using Date.Fns.Net.Services
- Now you can inject the IDateService into your components and call the available date methods like this :
var distance = await IDateService.FormatDistance(DateTime.Now, new DateTime(2000,9,15));
- Register of the dependency in a service container. In the Startup.cs add the same line to register the service
services.AddDateService(); /*Add this line*/
- Add the same line into your _Host.cshtml file to link date-fns.js file to the project :
<script src="_content/Date.Fns.Net/date-fns.js"></script>
- In your Imports.razor add this two lines :
@using Date.Fns.Net
@using Date.Fns.Net.Services
Any help or ideas are welcome. The project is still in progress trying to wrap more date-fns functions.
Thank you very much.