aws-cdk-lib.Duration

class Duration

LanguageType name
.NETAmazon.CDK.Duration
Gogithub.com/aws/aws-cdk-go/awscdk/v2#Duration
Javasoftware.amazon.awscdk.Duration
Pythonaws_cdk.Duration
TypeScript (source)aws-cdk-lib » Duration

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

import * as ecs from "aws-cdk-lib/aws-ecs"
declare const cluster: ecs.ICluster
declare const taskDefinition: ecs.TaskDefinition

const rule = new events.Rule(this, 'Rule', {
  schedule: events.Schedule.rate(cdk.Duration.hours(1)),
});

rule.addTarget(
  new targets.EcsTask( {
      cluster: cluster,
      taskDefinition: taskDefinition,
      propagateTags: ecs.PropagatedTagSource.TASK_DEFINITION,
      tags: [
        {
          key: 'my-tag',
          value: 'my-tag-value',
        },
      ],
    })
);

Methods

NameDescription
formatTokenToNumber()Returns stringified number of duration.
isUnresolved()Checks if duration is a token or a resolvable object.
minus(rhs)Substract two Durations together.
plus(rhs)Add two Durations together.
toDays(opts?)Return the total number of days in this Duration.
toHours(opts?)Return the total number of hours in this Duration.
toHumanString()Turn this duration into a human-readable string.
toIsoString()Return an ISO 8601 representation of this period.
toMilliseconds(opts?)Return the total number of milliseconds in this Duration.
toMinutes(opts?)Return the total number of minutes in this Duration.
toSeconds(opts?)Return the total number of seconds in this Duration.
toString()Returns a string representation of this Duration.
unitLabel()Returns unit of the duration.
static days(amount)Create a Duration representing an amount of days.
static hours(amount)Create a Duration representing an amount of hours.
static millis(amount)Create a Duration representing an amount of milliseconds.
static minutes(amount)Create a Duration representing an amount of minutes.
static parse(duration)Parse a period formatted according to the ISO 8601 standard.
static seconds(amount)Create a Duration representing an amount of seconds.

formatTokenToNumber()

public formatTokenToNumber(): string

Returns

  • string

Returns stringified number of duration.


isUnresolved()

public isUnresolved(): boolean

Returns

  • boolean

Checks if duration is a token or a resolvable object.


minus(rhs)

public minus(rhs: Duration): Duration

Parameters

  • rhs Duration

Returns

  • Duration

Substract two Durations together.


plus(rhs)

public plus(rhs: Duration): Duration

Parameters

  • rhs Duration

Returns

  • Duration

Add two Durations together.


toDays(opts?)

public toDays(opts?: TimeConversionOptions): number

Parameters

  • opts TimeConversionOptions

Returns

  • number

Return the total number of days in this Duration.


toHours(opts?)

public toHours(opts?: TimeConversionOptions): number

Parameters

  • opts TimeConversionOptions

Returns

  • number

Return the total number of hours in this Duration.


toHumanString()

public toHumanString(): string

Returns

  • string

Turn this duration into a human-readable string.


toIsoString()

public toIsoString(): string

Returns

  • string

Return an ISO 8601 representation of this period.

See also: https://www.iso.org/standard/70907.html


toMilliseconds(opts?)

public toMilliseconds(opts?: TimeConversionOptions): number

Parameters

  • opts TimeConversionOptions

Returns

  • number

Return the total number of milliseconds in this Duration.


toMinutes(opts?)

public toMinutes(opts?: TimeConversionOptions): number

Parameters

  • opts TimeConversionOptions

Returns

  • number

Return the total number of minutes in this Duration.


toSeconds(opts?)

public toSeconds(opts?: TimeConversionOptions): number

Parameters

  • opts TimeConversionOptions

Returns

  • number

Return the total number of seconds in this Duration.


toString()

public toString(): string

Returns

  • string

Returns a string representation of this Duration.

This is 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()

public unitLabel(): string

Returns

  • string

Returns unit of the duration.


static days(amount)

public static days(amount: number): Duration

Parameters

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

Returns

  • Duration

Create a Duration representing an amount of days.


static hours(amount)

public static hours(amount: number): Duration

Parameters

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

Returns

  • Duration

Create a Duration representing an amount of hours.


static millis(amount)

public static millis(amount: number): Duration

Parameters

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

Returns

  • Duration

Create a Duration representing an amount of milliseconds.


static minutes(amount)

public static minutes(amount: number): Duration

Parameters

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

Returns

  • Duration

Create a Duration representing an amount of minutes.


static parse(duration)

public static parse(duration: string): Duration

Parameters

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

Returns

  • Duration

Parse a period formatted according to the ISO 8601 standard.

See also: https://www.iso.org/standard/70907.html


static seconds(amount)

public static seconds(amount: number): Duration

Parameters

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

Returns

  • Duration

Create a Duration representing an amount of seconds.