moment.net 1.2.0

A library aimed at adding in some relevant functionality as seen in moment.js that is missing in the System.DateTime construct

Install-Package moment.net -Version 1.2.0
dotnet add package moment.net --version 1.2.0
<PackageReference Include="moment.net" Version="1.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add moment.net --version 1.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Moment.net

Build Status Coverage Status License: MIT NuGet Badge

Overview

This library aims to port as many bits of functionality from moment.js as is necessary. A few have been ported thus far FromNow, From, ToNow, To, StartOf, EndOf and CalendarTime. Attempts are also being made to add some functionalities that might not exist in moment.js.

Getting started

We recommend getting Moment.net via nuget package manager.

Package Manager
PM > Install-Package moment.net
.NET CLI
>  dotnet add package moment.net
PackageReference
<PackageReference Include="moment.net" />
Package Manager

PM &gt; Install-Package moment.net -Version 1.1.0

Example Usage

FromNow
var dateTime = new DateTime(2017, 1, 1);
var relativeTime = dateTime.FromNow(); // 2 years ago
From
var past = new DateTime(2017, 1, 1);
var future = new DateTime(2020, 1, 1);
var relativeTime = past.From(future); // 3 years ago
ToNow
var dateTime = new DateTime(2020, 1, 1);
var relativeTime = dateTime.ToNow(); // in one year
To
var past = new DateTime(2019, 1, 1);
var future = new DateTime(2021, 1, 1);
var relativeTime = past.From(future); // in 2 years
StartOf
var date = DateTime.Parse("5/1/2008 8:30:52 AM", System.Globalization.CultureInfo.InvariantCulture);
var startOfMinute = date.StartOf(DateTimeAnchor.Minute);	//01/05/2008 08:30:00"
var startOfHour = date.StartOf(DateTimeAnchor.Hour);		//01/05/2008 08:00:00" 
var startOfDay = date.StartOf(DateTimeAnchor.Day);		//01/05/2008 00:00:00"
var startOfWeek = date.StartOf(DateTimeAnchor.Week);		//27/04/2008 00:00:00" (previous month)
var startOfMonth = date.StartOf(DateTimeAnchor.Month);		//01/05/2008 00:00:00"
var startOfYear = date.StartOf(DateTimeAnchor.Year);		//01/01/2008 00:00:00"
EndOf
var date = DateTime.Parse("5/1/2008 8:30:52 AM", System.Globalization.CultureInfo.InvariantCulture);
var endOfMinute = date.EndOf(DateTimeAnchor.Minute);	        // 01/05/2008 08:30:59
var endOfHour = date.EndOf(DateTimeAnchor.Hour);		// 01/05/2008 08:59:59
var endOfDay = date.EndOf(DateTimeAnchor.Day);		        // 01/05/2008 23:59:59
var endOfWeek = date.EndOf(DateTimeAnchor.Week);		// 03/05/2008 23:59:59
var endOfMonth = date.EndOf(DateTimeAnchor.Month);		// 31/05/2008 23:59:59
var endOfYear = date.EndOf(DateTimeAnchor.Year);		// 31/12/2008 23:59:59
CalendarTime

Calendar Time supports creating formats for displaying the return string. The format works in the standard DateTime format string manner

var startDateTime = new DateTime(2012, 12, 12);
var sameDay = new DateTime(2012, 12, 12, 12, 0, 0);
var endDateTime = new DateTime(2012, 12, 13);
var calendarTime = startDateTime.CalendarTime(endDateTime); // Tomorrow at 00:00 AM
calendarTime = endDateTime.CalendarTime(startDateTime); // Yesterday at 00:00 AM
calendarTime = startDateTime.CalendarTime(sameDay); // Today at 12:00 PM
UnixTime

UnixTime supports retrieving the number of seconds or milliseconds that have elapsed since the unix epoch

var dateTime = new DateTime(1971, 01, 01, 0, 0, 0, DateTimeKind.Utc);
var millisecondsElapsed = dateTime.UnixTimestampInMilliseconds(); // 31536000000
var secondsElapsed = dateTime.UnixTimestampInSeconds(); // 31536000
First and Last Date in a week

Moment.net supports retrieving the first or last day in a week given a specific DateTime

date.FirstDateInWeek()  // 27/04/2008 00:00:00 (previous month)
date.LastDateInWeek()   // 03/05/2008 00:00:00

The example above uses the current CultureInfo for the system in use, to specify a CultureInfo, moment.net has an overloaded method that takes takes CultureInfo as an argument date.FirstDateInWeek(someCultureInfo).

Next

Returns the date of the next Thursday.

var date = DateTime.Parse("5/1/2008 8:30:52 AM", System.Globalization.CultureInfo.InvariantCulture);
date.Next(DayOfWeek.Thursday) // 08/05/2008 08:30:52

Returns the 3rd next Thursday use

date.Next(DayOfWeek.Thursday, 3) // 22/05/2008 08:30:52
Last

Returns the date of the previous Friday.

var date = DateTime.Parse("5/1/2008 8:30:52 AM", System.Globalization.CultureInfo.InvariantCulture);
date.Last(DayOfWeek.Friday) // 25/04/2008 08:30:52

Returns the 3rd previous Thursday.

date.Next(DayOfWeek.Thursday, 3) // 10/04/2008 08:30:52
Final

Fluently returns the final day of the week in a month or year given a date

var date = DateTime.Parse("5/1/2008 8:30:52 AM", System.Globalization.CultureInfo.InvariantCulture);
// final monday in may 2008
date.Final().Monday().InMonth(); // 26/05/2008 00:00:00

// final sunday in 2008
date.Final().Sunday().InYear();  // 28/12/2008 00:00:00

Moment.net

Build Status Coverage Status License: MIT NuGet Badge

Overview

This library aims to port as many bits of functionality from moment.js as is necessary. A few have been ported thus far FromNow, From, ToNow, To, StartOf, EndOf and CalendarTime. Attempts are also being made to add some functionalities that might not exist in moment.js.

Getting started

We recommend getting Moment.net via nuget package manager.

Package Manager
PM > Install-Package moment.net
.NET CLI
>  dotnet add package moment.net
PackageReference
<PackageReference Include="moment.net" />
Package Manager

PM &gt; Install-Package moment.net -Version 1.1.0

Example Usage

FromNow
var dateTime = new DateTime(2017, 1, 1);
var relativeTime = dateTime.FromNow(); // 2 years ago
From
var past = new DateTime(2017, 1, 1);
var future = new DateTime(2020, 1, 1);
var relativeTime = past.From(future); // 3 years ago
ToNow
var dateTime = new DateTime(2020, 1, 1);
var relativeTime = dateTime.ToNow(); // in one year
To
var past = new DateTime(2019, 1, 1);
var future = new DateTime(2021, 1, 1);
var relativeTime = past.From(future); // in 2 years
StartOf
var date = DateTime.Parse("5/1/2008 8:30:52 AM", System.Globalization.CultureInfo.InvariantCulture);
var startOfMinute = date.StartOf(DateTimeAnchor.Minute);	//01/05/2008 08:30:00"
var startOfHour = date.StartOf(DateTimeAnchor.Hour);		//01/05/2008 08:00:00" 
var startOfDay = date.StartOf(DateTimeAnchor.Day);		//01/05/2008 00:00:00"
var startOfWeek = date.StartOf(DateTimeAnchor.Week);		//27/04/2008 00:00:00" (previous month)
var startOfMonth = date.StartOf(DateTimeAnchor.Month);		//01/05/2008 00:00:00"
var startOfYear = date.StartOf(DateTimeAnchor.Year);		//01/01/2008 00:00:00"
EndOf
var date = DateTime.Parse("5/1/2008 8:30:52 AM", System.Globalization.CultureInfo.InvariantCulture);
var endOfMinute = date.EndOf(DateTimeAnchor.Minute);	        // 01/05/2008 08:30:59
var endOfHour = date.EndOf(DateTimeAnchor.Hour);		// 01/05/2008 08:59:59
var endOfDay = date.EndOf(DateTimeAnchor.Day);		        // 01/05/2008 23:59:59
var endOfWeek = date.EndOf(DateTimeAnchor.Week);		// 03/05/2008 23:59:59
var endOfMonth = date.EndOf(DateTimeAnchor.Month);		// 31/05/2008 23:59:59
var endOfYear = date.EndOf(DateTimeAnchor.Year);		// 31/12/2008 23:59:59
CalendarTime

Calendar Time supports creating formats for displaying the return string. The format works in the standard DateTime format string manner

var startDateTime = new DateTime(2012, 12, 12);
var sameDay = new DateTime(2012, 12, 12, 12, 0, 0);
var endDateTime = new DateTime(2012, 12, 13);
var calendarTime = startDateTime.CalendarTime(endDateTime); // Tomorrow at 00:00 AM
calendarTime = endDateTime.CalendarTime(startDateTime); // Yesterday at 00:00 AM
calendarTime = startDateTime.CalendarTime(sameDay); // Today at 12:00 PM
UnixTime

UnixTime supports retrieving the number of seconds or milliseconds that have elapsed since the unix epoch

var dateTime = new DateTime(1971, 01, 01, 0, 0, 0, DateTimeKind.Utc);
var millisecondsElapsed = dateTime.UnixTimestampInMilliseconds(); // 31536000000
var secondsElapsed = dateTime.UnixTimestampInSeconds(); // 31536000
First and Last Date in a week

Moment.net supports retrieving the first or last day in a week given a specific DateTime

date.FirstDateInWeek()  // 27/04/2008 00:00:00 (previous month)
date.LastDateInWeek()   // 03/05/2008 00:00:00

The example above uses the current CultureInfo for the system in use, to specify a CultureInfo, moment.net has an overloaded method that takes takes CultureInfo as an argument date.FirstDateInWeek(someCultureInfo).

Next

Returns the date of the next Thursday.

var date = DateTime.Parse("5/1/2008 8:30:52 AM", System.Globalization.CultureInfo.InvariantCulture);
date.Next(DayOfWeek.Thursday) // 08/05/2008 08:30:52

Returns the 3rd next Thursday use

date.Next(DayOfWeek.Thursday, 3) // 22/05/2008 08:30:52
Last

Returns the date of the previous Friday.

var date = DateTime.Parse("5/1/2008 8:30:52 AM", System.Globalization.CultureInfo.InvariantCulture);
date.Last(DayOfWeek.Friday) // 25/04/2008 08:30:52

Returns the 3rd previous Thursday.

date.Next(DayOfWeek.Thursday, 3) // 10/04/2008 08:30:52
Final

Fluently returns the final day of the week in a month or year given a date

var date = DateTime.Parse("5/1/2008 8:30:52 AM", System.Globalization.CultureInfo.InvariantCulture);
// final monday in may 2008
date.Final().Monday().InMonth(); // 26/05/2008 00:00:00

// final sunday in 2008
date.Final().Sunday().InYear();  // 28/12/2008 00:00:00

Release Notes

Add last and final support

  • .NETStandard 1.1

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.2.0 417 6/27/2019
1.1.0 200 5/13/2019
1.0.0 208 4/21/2019
1.0.0-alpha 85 3/18/2019