Skip to content
v2.273.0

Duration Class ​

Represents a length of time.

The amount can be specified either as a literal value (e.g: 10) which cannot be negative, or as an unresolved number token.

When the amount is passed as a token, unit conversion is not possible.

Example ​

ts
import * as lambda from 'aws-cdk-lib/aws-lambda';

declare const fn: lambda.Function;
const fnUrl = fn.addFunctionUrl({ authType: lambda.FunctionUrlAuthType.NONE });

new cloudfront.Distribution(this, 'Distribution', {
  defaultBehavior: {
    origin: new origins.FunctionUrlOrigin(fnUrl, {
      readTimeout: Duration.seconds(30),
      responseCompletionTimeout: Duration.seconds(90),
      keepaliveTimeout: Duration.seconds(45),
    }),
  },
});

Methods ​

days(amount: number): Duration // chainable@staticmethod def days(amount: int | float) -> Durationpublic static Duration days(Number amount)public static Duration Days(double amount)func Days(Amount *float64) Durationstatic#
Create a Duration representing an amount of days.
1 parameter · returns

Parameters

  • amountamountamountamountAmount — the amount of Days the Duration will represent.

Returns: a new Duration representing amount Days.

formatTokenToNumber(): stringdef format_token_to_number(self, ) -> strpublic String formatTokenToNumber()public string FormatTokenToNumber()func FormatTokenToNumber() *string#
Returns stringified number of duration.
hours(amount: number): Duration // chainable@staticmethod def hours(amount: int | float) -> Durationpublic static Duration hours(Number amount)public static Duration Hours(double amount)func Hours(Amount *float64) Durationstatic#
Create a Duration representing an amount of hours.
1 parameter · returns

Parameters

  • amountamountamountamountAmount — the amount of Hours the Duration will represent.

Returns: a new Duration representing amount Hours.

isUnresolved(): booleandef is_unresolved(self, ) -> boolpublic Boolean isUnresolved()public bool IsUnresolved()func IsUnresolved() *bool#
Checks if duration is a token or a resolvable object.
millis(amount: number): Duration // chainable@staticmethod def millis(amount: int | float) -> Durationpublic static Duration millis(Number amount)public static Duration Millis(double amount)func Millis(Amount *float64) Durationstatic#
Create a Duration representing an amount of milliseconds.
1 parameter · returns

Parameters

  • amountamountamountamountAmount — the amount of Milliseconds the Duration will represent.

Returns: a new Duration representing amount ms.

minus(rhs: Duration): Duration // chainabledef minus(self, rhs: Duration) -> Durationpublic Duration minus(Duration rhs)public Duration Minus(Duration rhs)func Minus(Rhs Duration) Duration#
Substract two Durations together.
minutes(amount: number): Duration // chainable@staticmethod def minutes(amount: int | float) -> Durationpublic static Duration minutes(Number amount)public static Duration Minutes(double amount)func Minutes(Amount *float64) Durationstatic#
Create a Duration representing an amount of minutes.
1 parameter · returns

Parameters

  • amountamountamountamountAmount — the amount of Minutes the Duration will represent.

Returns: a new Duration representing amount Minutes.

parse(duration: string): Duration // chainable@staticmethod def parse(duration: str) -> Durationpublic static Duration parse(String duration)public static Duration Parse(string duration)func Parse(Duration *string) Durationstatic#
Parse a period formatted according to the ISO 8601 standard.

Days are the largest ISO duration supported, i.e., weeks, months, and years are not supported.

1 parameter · returns · example

Parameters

  • durationdurationdurationdurationDuration — an ISO-formatted duration to be parsed.

Returns: the parsed Duration.

ts
// This represents 1 day, 2 hours, 3 minutes, 4 seconds, and 567 milliseconds.
'P1DT2H3M4.567S'
plus(rhs: Duration): Duration // chainabledef plus(self, rhs: Duration) -> Durationpublic Duration plus(Duration rhs)public Duration Plus(Duration rhs)func Plus(Rhs Duration) Duration#
Add two Durations together.
seconds(amount: number): Duration // chainable@staticmethod def seconds(amount: int | float) -> Durationpublic static Duration seconds(Number amount)public static Duration Seconds(double amount)func Seconds(Amount *float64) Durationstatic#
Create a Duration representing an amount of seconds.
1 parameter · returns

Parameters

  • amountamountamountamountAmount — the amount of Seconds the Duration will represent.

Returns: a new Duration representing amount Seconds.

toDays(opts?: TimeConversionOptions): numberdef to_days(self, opts: typing.Optional[TimeConversionOptions] = None) -> int | floatpublic Number toDays(TimeConversionOptions opts)public double ToDays(TimeConversionOptions? opts = null)func ToDays(Opts TimeConversionOptions) *float64#
Return the total number of days in this Duration.
returns

Returns: the value of this Duration expressed in Days.

toHours(opts?: TimeConversionOptions): numberdef to_hours(self, opts: typing.Optional[TimeConversionOptions] = None) -> int | floatpublic Number toHours(TimeConversionOptions opts)public double ToHours(TimeConversionOptions? opts = null)func ToHours(Opts TimeConversionOptions) *float64#
Return the total number of hours in this Duration.
returns

Returns: the value of this Duration expressed in Hours.

toHumanString(): stringdef to_human_string(self, ) -> strpublic String toHumanString()public string ToHumanString()func ToHumanString() *string#
Turn this duration into a human-readable string.
toIsoString(): stringdef to_iso_string(self, ) -> strpublic String toIsoString()public string ToIsoString()func ToIsoString() *string#
Return an ISO 8601 representation of this period.
returns

Returns: a string starting with 'P' describing the period

toMilliseconds(opts?: TimeConversionOptions): numberdef to_milliseconds(self, opts: typing.Optional[TimeConversionOptions] = None) -> int | floatpublic Number toMilliseconds(TimeConversionOptions opts)public double ToMilliseconds(TimeConversionOptions? opts = null)func ToMilliseconds(Opts TimeConversionOptions) *float64#
Return the total number of milliseconds in this Duration.
returns

Returns: the value of this Duration expressed in Milliseconds.

toMinutes(opts?: TimeConversionOptions): numberdef to_minutes(self, opts: typing.Optional[TimeConversionOptions] = None) -> int | floatpublic Number toMinutes(TimeConversionOptions opts)public double ToMinutes(TimeConversionOptions? opts = null)func ToMinutes(Opts TimeConversionOptions) *float64#
Return the total number of minutes in this Duration.
returns

Returns: the value of this Duration expressed in Minutes.

toSeconds(opts?: TimeConversionOptions): numberdef to_seconds(self, opts: typing.Optional[TimeConversionOptions] = None) -> int | floatpublic Number toSeconds(TimeConversionOptions opts)public double ToSeconds(TimeConversionOptions? opts = null)func ToSeconds(Opts TimeConversionOptions) *float64#
Return the total number of seconds in this Duration.
returns

Returns: the value of this Duration expressed in Seconds.

toString(): stringdef to_string(self, ) -> strpublic String toString()public string ToString()func ToString() *string#
Returns a string representation of this `Duration`.

This is never the right function to use when you want to use the Duration object in a template. Use toSeconds(), toMinutes(), toDays(), etc. instead.

unitLabel(): stringdef unit_label(self, ) -> strpublic String unitLabel()public string UnitLabel()func UnitLabel() *string#
Returns unit of the duration.