Class DateAdjusters
Factory class for date adjusters: functions from LocalDate to
LocalDate
,
which can be applied to LocalDate, LocalDateTime, and OffsetDateTime.
Since 2.0.x
Availability netstandard2.0
Inheritance
Object
DateAdjusters
Inherited Members
Object.Equals(Object)
Object.Equals(Object, Object)
Object.GetHashCode()
Object.GetType()
Object.MemberwiseClone()
Object.ReferenceEquals(Object, Object)
Object.ToString()
Namespace: NodaTime
Assembly: NodaTime.dll
Syntax
public static class DateAdjusters
Properties
EndOfMonth
A date adjuster to move to the last day of the current month.
Since 2.0.x
Availability netstandard2.0
Declaration
public static Func<LocalDate, LocalDate> EndOfMonth { get; }
Property Value
Type | Description |
---|---|
Func<LocalDate, LocalDate> | A date adjuster to move to the last day of the current month. |
StartOfMonth
A date adjuster to move to the first day of the current month.
Since 2.0.x
Availability netstandard2.0
Declaration
public static Func<LocalDate, LocalDate> StartOfMonth { get; }
Property Value
Type | Description |
---|---|
Func<LocalDate, LocalDate> | A date adjuster to move to the first day of the current month. |
Methods
AddPeriod(Period)
Creates a date adjuster to add the specified period to the date.
Since 3.0.x
Availability netstandard2.0
Declaration
public static Func<LocalDate, LocalDate> AddPeriod(Period period)
Parameters
Type | Name | Description |
---|---|---|
Period | period | The period to add when the adjuster is invoked. Must not contain any (non-zero) time units. |
Returns
Type | Description |
---|---|
Func<LocalDate, LocalDate> | An adjuster which adds the specified period. |
Remarks
This is the adjuster equivalent of Plus(Period).
Sample snippet
using NodaTime;
using System;
LocalDateTime localDateTime = new LocalDateTime(1985, 10, 26, 1, 18);
Offset offset = Offset.FromHours(-5);
OffsetDateTime original = new OffsetDateTime(localDateTime, offset);
var dateAdjuster = DateAdjusters.AddPeriod(Period.FromYears(30));
OffsetDateTime updated = original.With(dateAdjuster);
Console.WriteLine(updated.LocalDateTime);
Console.WriteLine(updated.Offset);
Output:
10/26/2015 01:18:00
-05
DayOfMonth(Int32)
A date adjuster to move to the specified day of the current month.
Since 2.0.x
Availability netstandard2.0
Declaration
public static Func<LocalDate, LocalDate> DayOfMonth(int day)
Parameters
Type | Name | Description |
---|---|---|
Int32 | day | The day of month to adjust dates to. |
Returns
Type | Description |
---|---|
Func<LocalDate, LocalDate> | An adjuster which changes the day to day ,
retaining the same year and month. |
Remarks
The returned adjuster will throw an exception if it is applied to a date
that would create an invalid result.
Sample snippet
using NodaTime;
using System;
var start = new LocalDate(2014, 6, 27);
var adjuster = DateAdjusters.DayOfMonth(19);
Console.WriteLine(adjuster(start));
Output:
Thursday, 19 June 2014
Month(Int32)
A date adjuster to move to the same day of the specified month.
Since 2.0.x
Availability netstandard2.0
Declaration
public static Func<LocalDate, LocalDate> Month(int month)
Parameters
Type | Name | Description |
---|---|---|
Int32 | month | The month to adjust dates to. |
Returns
Type | Description |
---|---|
Func<LocalDate, LocalDate> | An adjuster which changes the month to month ,
retaining the same year and day of month. |
Remarks
The returned adjuster will throw an exception if it is applied to a date
that would create an invalid result.
Sample snippet
using NodaTime;
using System;
var start = new LocalDate(2014, 6, 27);
var adjuster = DateAdjusters.Month(2);
Console.WriteLine(adjuster(start));
Output:
Thursday, 27 February 2014
Next(IsoDayOfWeek)
A date adjuster to move to the next specified day-of-week, adding
a week if the day is already correct.
Since 2.0.x
Availability netstandard2.0
Declaration
public static Func<LocalDate, LocalDate> Next(IsoDayOfWeek dayOfWeek)
Parameters
Type | Name | Description |
---|---|---|
IsoDayOfWeek | dayOfWeek | The day-of-week to adjust dates to. |
Returns
Type | Description |
---|---|
Func<LocalDate, LocalDate> | An adjuster which advances a date to the next occurrence of the specified day-of-week. |
Remarks
This is the adjuster equivalent of Next(IsoDayOfWeek).
Sample snippet
using NodaTime;
using System;
var start = new LocalDate(2014, 6, 27);
var adjuster = DateAdjusters.Next(IsoDayOfWeek.Thursday);
Console.WriteLine(adjuster(start));
Output:
Thursday, 03 July 2014
NextOrSame(IsoDayOfWeek)
A date adjuster to move to the next specified day-of-week, but return the
original date if the day is already correct.
Since 2.0.x
Availability netstandard2.0
Declaration
public static Func<LocalDate, LocalDate> NextOrSame(IsoDayOfWeek dayOfWeek)
Parameters
Type | Name | Description |
---|---|---|
IsoDayOfWeek | dayOfWeek | The day-of-week to adjust dates to. |
Returns
Type | Description |
---|---|
Func<LocalDate, LocalDate> | An adjuster which advances a date to the next occurrence of the specified day-of-week, or the original date if the day is already correct. |
Sample snippet
using NodaTime;
using System;
var start = new LocalDate(2014, 6, 27);
var adjuster = DateAdjusters.NextOrSame(IsoDayOfWeek.Friday);
Console.WriteLine(adjuster(start));
Output:
Friday, 27 June 2014
Previous(IsoDayOfWeek)
A date adjuster to move to the previous specified day-of-week, subtracting
a week if the day is already correct.
Since 2.0.x
Availability netstandard2.0
Declaration
public static Func<LocalDate, LocalDate> Previous(IsoDayOfWeek dayOfWeek)
Parameters
Type | Name | Description |
---|---|---|
IsoDayOfWeek | dayOfWeek | The day-of-week to adjust dates to. |
Returns
Type | Description |
---|---|
Func<LocalDate, LocalDate> | An adjuster which advances a date to the previous occurrence of the specified day-of-week. |
Remarks
This is the adjuster equivalent of Previous(IsoDayOfWeek).
Sample snippet
using NodaTime;
using System;
var start = new LocalDate(2014, 6, 27);
var adjuster = DateAdjusters.Previous(IsoDayOfWeek.Thursday);
Console.WriteLine(adjuster(start));
Output:
Thursday, 26 June 2014
PreviousOrSame(IsoDayOfWeek)
A date adjuster to move to the previous specified day-of-week, but return the
original date if the day is already correct.
Since 2.0.x
Availability netstandard2.0
Declaration
public static Func<LocalDate, LocalDate> PreviousOrSame(IsoDayOfWeek dayOfWeek)
Parameters
Type | Name | Description |
---|---|---|
IsoDayOfWeek | dayOfWeek | The day-of-week to adjust dates to. |
Returns
Type | Description |
---|---|
Func<LocalDate, LocalDate> | An adjuster which advances a date to the previous occurrence of the specified day-of-week, or the original date if the day is already correct. |
Sample snippet
using NodaTime;
using System;
var start = new LocalDate(2014, 6, 27);
var adjuster = DateAdjusters.PreviousOrSame(IsoDayOfWeek.Friday);
Console.WriteLine(adjuster(start));
Output:
Friday, 27 June 2014