TimeAdd

TimeAdd( base, offset, unit )
TimeAdd( base, offset, unit )
TimeAdd( base, offset, unit )
TimeAdd( base, offset, unit )
time_add( base, offset, unit )
TimeAdd( base, offset, unit )
TimeAdd( base, offset, unit )

Description

The TimeAdd function increases a base Timestamp or Date by adding the offset in units.

Parameters

Argument Type Definition and Requirements

base

A timestamp or date to add to. base must match the type of unit.

offset

The number of units to add to base.

unit

The offset's unit.

When base is a Date, unit must be one of day or days.

When base is a Timestamp, unit must be one of:

  • day, or days

  • half day, or half days

  • hour, or hours

  • minute, or minutes

  • second, or seconds

  • millisecond, or milliseconds

  • microsecond, or microseconds

  • nanosecond, or nanoseconds

Returns

When base is a Date, a new Date which represents base with offset units added to it.

When base is a Timestamp, a new Timestamp which represents base with offset units added to it.

Examples

The following query returns a Timestamp representing one day after the Unix epoch:

try
{
    Value result = await client.Query(
        TimeAdd(Epoch(0, "second"), 1, "day")
    );

    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
FaunaTime(1970-01-02T00:00:00Z)
result, err := client.Query(
	f.TimeAdd(f.Epoch(1, "day"), 1, "second"))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
{0 62135683201 <nil>}
System.out.println(
    client.query(
        TimeAdd(Epoch(0, "second"), 1, "day")
    ).get());
1970-01-02T00:00:00Z
client.query(
  q.TimeAdd(q.Epoch(0, 'second'), 1, 'day')
)
.then((ret) => console.log(ret))
.catch((err) => console.error('Error: %s', err))
Time("1970-01-02T00:00:00Z")
result = client.query(
  q.time_add(q.epoch(1, "day"), 1, "second")
)
print(result)
FaunaTime('1970-01-02T00:00:01Z')
try {
  println(Await.result(
  client.query(
    TimeAdd(Epoch(0, "second"), 1, "day")
  ),
  5.seconds
))} catch {
  case unknown: Throwable => println("Error: " + unknown.getMessage())
}
1970-01-02T00:00:00Z
TimeAdd(Epoch(0, 'second'), 1, 'day')
Time("1970-01-02T00:00:00Z")

The following query returns a Timestamp 5 seconds in the future from now:

try
{
    Value result = await client.Query(
        Arr(
            Now(),
            TimeAdd(Now(), 5, "seconds")
        )
    );

    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
Arr(FaunaTime(2019-10-22T16:45:15.23Z), FaunaTime(2019-10-22T16:45:20.23Z))
result, err := client.Query(
	f.Arr{
		f.Now(),
		f.TimeAdd(f.Now(), 5, "seconds"),
	})

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
[{183000000 63746604242 <nil>} {183000000 63746604247 <nil>}]
System.out.println(
    client.query(
        Arr(
            Time("2020-07-06T12:34:56.789Z"),
            TimeAdd(Time("2020-07-06T12:34:56.789Z"), 5, "seconds")
        )
    ).get());
[2020-07-06T12:34:56.789Z, 2020-07-06T12:35:01.789Z]
client.query([
  q.Now(),
  q.TimeAdd(q.Now(), 5, 'seconds'),
])
.then((ret) => console.log(ret))
.catch((err) => console.error('Error: %s', err))
[ Time("2019-10-22T16:45:19.223863Z"),
  Time("2019-10-22T16:45:24.223863Z") ]
result = client.query(
  [
    q.now(),
    q.time_add(q.now(), 5, "seconds"),
  ]
)
print(result)
[FaunaTime('2021-01-18T22:04:09.121Z'), FaunaTime('2021-01-18T22:04:14.121Z')]
try {
  println(Await.result(
  client.query(
    Arr(
      Time("2020-07-06T12:34:56.789Z"),
      TimeAdd(Time("2020-07-06T12:34:56.789Z"), 5, "seconds")
    )
  ),
  5.seconds
))} catch {
  case unknown: Throwable => println("Error: " + unknown.getMessage())
}
[2020-07-06T12:34:56.789Z, 2020-07-06T12:35:01.789Z]
[
  Now(),
  TimeAdd(Now(), 5, 'seconds')
]
[ Time("2021-06-23T12:23:09.348Z"), Time("2021-06-23T12:23:14.348Z") ]

The following query returns a Date 1 day after 2019-11-26:

try
{
    Value result = await client.Query(
        TimeAdd(Date("2019-11-26"), 1, "day")
    );

    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
FaunaDate(2019-11-27 12:00:00 AM)
result, err := client.Query(
	f.TimeAdd(f.Date("2019-11-26"), 1, "day"))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
{0 63710409600 <nil>}
System.out.println(
    client.query(
        TimeAdd(Date("2019-11-26"), 1, "day")
    ).get());
2019-11-27
client.query(
  q.TimeAdd(q.Date('2019-11-26'), 1, 'day')
)
.then((ret) => console.log(ret))
.catch((err) => console.error('Error: %s', err))
Date("2019-11-27")
result = client.query(
  q.time_add(q.date("2019-11-26"), 1, "day")
)
print(result)
2019-11-27
try {
  println(Await.result(
  client.query(
    TimeAdd(Date("2019-11-26"), 1, "day")
  ),
  5.seconds
))} catch {
  case unknown: Throwable => println("Error: " + unknown.getMessage())
}
2019-11-27
TimeAdd(Date('2019-11-26'), 1, 'day')
Date("2019-11-27")

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve!
Visit Fauna's Discourse forums or email docs@fauna.com

Thank you for your feedback!