moment.net 1.3.4

dotnet add package moment.net --version 1.3.4
NuGet\Install-Package moment.net -Version 1.3.4
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="moment.net" Version="1.3.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add moment.net --version 1.3.4
#r "nuget: moment.net, 1.3.4"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install moment.net as a Cake Addin
#addin nuget:?package=moment.net&version=1.3.4

// Install moment.net as a Cake Tool
#tool nuget:?package=moment.net&version=1.3.4

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 > Install-Package moment.net

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.Last(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
Format

Fluently returns a formatted string for a given date. if no format string is provided the format defaults to the ISO-8601 standard with no fractional seconds.

var dateTime = new DateTime(1971, 01, 01, 0, 0, 0, DateTimeKind.Utc);
date.Format(); // 1971-01-01T00:00:00+00:00
date.Format("yyyy MMM dd"); // 1971 Jan 01
IsLeapYear

Determines if a given date falls in a leap year.

var dateTime = DateTime.Parse("1992-02-01");
dateTime.IsLeapYear(); // True

var dateTime = DateTime.Parse("1900-02-01");
dateTime.IsLeapYear(); // False
IsSame

Checks in a timezone-safe manner whether two dates are the same

var one = DateTime.Parse("2022-01-01T23:00:00-01:00");
var another = DateTime.Parse("2022-01-02T03:00:00+03:00");
one.IsSame(another); // True

var wrong = DateTime.Parse("1900-02-01");
wrong.IsSame(one); // False
IsBefore

Checks in a timezone-safe manner whether one date comes before another

var one = DateTime.Parse("2022-01-02");
var another = DateTime.Parse("2022-01-03");
one.IsSameOrBefore(another); // True

var wrong = DateTime.Parse("2022-02-01");
one.IsBefore(wrong); // False
IsSameOrBefore

Checks in a timezone-safe manner whether one date is the same as or comes before another

var one = DateTime.Parse("2022-01-02");
var another = DateTime.Parse("2022-01-03");
one.IsSameOrBefore(another); // True

var same = DateTime.Parse("2022-01-02");
one.IsSameOrBefore(same); // True

var wrong = DateTime.Parse("2022-01-01");
one.IsSameOrBefore(wrong); // False
IsAfter

Checks in a timezone-safe manner whether one date comes after another

var one = DateTime.Parse("2022-01-03");
var another = DateTime.Parse("2022-01-02");
one.IsAfter(another); // True

var wrong = DateTime.Parse("2022-02-01");
one.IsAfter(wrong); // False
IsSameOrAfter

Checks in a timezone-safe manner whether one date is the same as or comes after another

var one = DateTime.Parse("2022-01-03");
var another = DateTime.Parse("2022-01-02");
one.IsSameOrAfter(another); // True

var same = DateTime.Parse("2022-01-03");
one.IsSameOrAfter(same); // True

var wrong = DateTime.Parse("2022-02-01");
one.IsSameOrAfter(wrong); // False

Localization

The methods from the class RelativeTime that return a string now accept an optional parameter ci of the type CultureInfo.

This optional parameter is used to retrieve the corresponding string resources from the main assembly (for english/invariant culture) or the satellite assemblies for other cultures.

If the ci parameter for an unsupported language is passed in, the library falls back to the invariant culture (english).

If the ci parameter is not passed in, or it is null, the library uses the culture returned by the static method CultureWrapper.GetDefaultCulture.

The value returned by this method is in turn controlled by the value of the property CultureInfo.UseCurrentThreadCultureAsDefault.

  • When true, GetDefaultCulture returns the CultureInfo associated with the running thread.
  • When false, the value specified in the CultureWrapper.DefaultCulture property is returned.

Examples using localization

FromNow
var dateTime = new DateTime(2017, 1, 1);
var relativeTime = dateTime.FromNow(new CultureInfo("es")); // 6 años atrás (spanish culture)
From
var past = new DateTime(2017, 1, 1);
var future = new DateTime(2020, 1, 1);
var relativeTime = past.From(future, new CultureInfo("ru")); // 3 years ago (russian culture -unsupported-, so fallback to invariant is used)
ToNow
var dateTime = new DateTime(2024, 1, 1);
var relativeTime = dateTime.ToNow(new CultureInfo("es")); // en un año (spanish culture)

Adding additional resources for localization

Currently, the library contains english and spanish resources for localized strings.

If you want to contribute with support for more languages, just add the corresponding String.[language identifier].resx to the root folder, and use the Strings.es.resx as a guide to fill the key-value pairs.

Be sure to set the Build Action of the .resx file to Embedded resource to avoid problems resolving the string resources.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.4 24,837 7/3/2023
1.3.2 17,117 2/24/2022
1.3.1 55,364 10/4/2020
1.3.0 22,295 5/13/2020
1.2.0 9,584 6/27/2019
1.1.0 937 5/13/2019
1.0.0 671 4/21/2019
1.0.0-alpha 456 3/18/2019

- fix the FromNow and From methods