Skip to content

Latest commit

 

History

History
149 lines (123 loc) · 5.53 KB

README.md

File metadata and controls

149 lines (123 loc) · 5.53 KB

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