Skip to content

elrhomariyounes/Date.Fns.Net

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Date Fns .Net

NuGet

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

Install the Package from Nuget.org

$ dotnet add package Date.Fns.Net --version 0.2.0

Usage

Blazor WebAssembly

  1. 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();
        }
    }
  1. In the index.html file located at wwwroot folder, add the following line
 <script src="_content/Date.Fns.Net/date-fns.js"></script>
  1. In your Imports.razor add this two lines :
@using Date.Fns.Net
@using Date.Fns.Net.Services
  1. 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));

Blazor Server

  1. 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*/
  1. 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>
  1. In your Imports.razor add this two lines :
@using Date.Fns.Net
@using Date.Fns.Net.Services

Contributing

Any help or ideas are welcome. The project is still in progress trying to wrap more date-fns functions.

Thank you very much.