class Duration
Language | Type name |
---|---|
![]() | Amazon.CDK.Duration |
![]() | software.amazon.awscdk.core.Duration |
![]() | aws_cdk.core.Duration |
![]() | @aws-cdk/core » 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 lambda from '@aws-cdk/aws-lambda';
const fn = new lambda.Function(this, 'MyFunc', {
runtime: lambda.Runtime.NODEJS_14_X,
handler: 'index.handler',
code: lambda.Code.fromInline(`exports.handler = handler.toString()`),
});
const rule = new events.Rule(this, 'rule', {
eventPattern: {
source: ["aws.ec2"],
},
});
const queue = new sqs.Queue(this, 'Queue');
rule.addTarget(new targets.LambdaFunction(fn, {
deadLetterQueue: queue, // Optional: add a dead letter queue
maxEventAge: cdk.Duration.hours(2), // Optional: set the maxEventAge retry policy
retryAttempts: 2, // Optional: set the max number of retry attempts
}));
Methods
Name | Description |
---|---|
format | Returns stringified number of duration. |
is | Checks if duration is a token or a resolvable object. |
minus(rhs) | Substract two Durations together. |
plus(rhs) | Add two Durations together. |
to | Return the total number of days in this Duration. |
to | Return the total number of hours in this Duration. |
to | Turn this duration into a human-readable string. |
to | Return an ISO 8601 representation of this period. |
to | Return an ISO 8601 representation of this period. |
to | Return the total number of milliseconds in this Duration. |
to | Return the total number of minutes in this Duration. |
to | Return the total number of seconds in this Duration. |
to | Returns a string representation of this Duration . |
unit | 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
Substract two Durations together.
plus(rhs)
public plus(rhs: Duration): Duration
Parameters
- rhs
Duration
Returns
Add two Durations together.
toDays(opts?)
public toDays(opts?: TimeConversionOptions): number
Parameters
Returns
number
Return the total number of days in this Duration.
toHours(opts?)
public toHours(opts?: TimeConversionOptions): number
Parameters
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
⚠️ Deprecated: Use toIsoString()
instead.
Returns
string
Return an ISO 8601 representation of this period.
See also: https://www.iso.org/fr/standard/70907.html
toIsoString()
public toIsoString(): string
Returns
string
Return an ISO 8601 representation of this period.
See also: https://www.iso.org/fr/standard/70907.html
toMilliseconds(opts?)
public toMilliseconds(opts?: TimeConversionOptions): number
Parameters
Returns
number
Return the total number of milliseconds in this Duration.
toMinutes(opts?)
public toMinutes(opts?: TimeConversionOptions): number
Parameters
Returns
number
Return the total number of minutes in this Duration.
toSeconds(opts?)
public toSeconds(opts?: TimeConversionOptions): number
Parameters
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 theDuration
will represent.
Returns
Create a Duration representing an amount of days.
static hours(amount)
public static hours(amount: number): Duration
Parameters
- amount
number
— the amount of Hours theDuration
will represent.
Returns
Create a Duration representing an amount of hours.
static millis(amount)
public static millis(amount: number): Duration
Parameters
- amount
number
— the amount of Milliseconds theDuration
will represent.
Returns
Create a Duration representing an amount of milliseconds.
static minutes(amount)
public static minutes(amount: number): Duration
Parameters
- amount
number
— the amount of Minutes theDuration
will represent.
Returns
Create a Duration representing an amount of minutes.
static parse(duration)
public static parse(duration: string): Duration
Parameters
- duration
string
— an ISO-formtted duration to be parsed.
Returns
Parse a period formatted according to the ISO 8601 standard.
See also: https://www.iso.org/fr/standard/70907.html
static seconds(amount)
public static seconds(amount: number): Duration
Parameters
- amount
number
— the amount of Seconds theDuration
will represent.
Returns
Create a Duration representing an amount of seconds.