# CEL input: How to subtract 1day from now()

**URL:** <https://discuss.elastic.co/t/cel-input-how-to-subtract-1day-from-now/351894>\
**Category:** Beats\
**Tags:** filebeat\
**Created:** [January 26, 2024, 5:51pm UTC](https://discuss.elastic.co/t/cel-input-how-to-subtract-1day-from-now/351894 "2024-01-26T17:51:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelk](https://avatars.discourse-cdn.com/v4/letter/k/13edae/32.png) [@kelk](https://discuss.elastic.co/u/kelk)\
**Post date:** [January 26, 2024, 5:51pm UTC](https://discuss.elastic.co/t/cel-input-how-to-subtract-1day-from-now/351894/1 "2024-01-26T17:51:33Z")

</div>

hi,  
I'm trying to pull data from an api end-point. but it requires a dynamic value to supplied (For instance Date. I want to provide yesterday's date)  
In the actual `go` library, there are quite ways  
eg [Go Playground - The Go Programming Language](https://go.dev/play/p/FIBiKgDPcgc)

```auto
fmt.Println("time-24h", time.Now().Add(-24*time.Hour))

```

But in CEL input, how to

- define a value? (eg in mito library [this way](https://pkg.go.dev/github.com/elastic/mito/lib#pkg-constants) but not working in CEL input)
- How to put dynamic value like `go` to subtract 1day or 24hrs etc?

```auto
- type: cel
  interval: 10m
  resource:
    url: https://api.sunrise-sunset.org/json?lat=36.7201600&lng=-4.4203400
  program: |
    bytes(request("GET", state.url + "&date=" + now.format("2006-01-01") )
      .with({"Header": {"Accept": ["application/json"]}})
      .do_request().Body
    ).as(body,{ 
                  "events" : [{"mydata": body.decode_json() }]
              }
        )

```

the above example gives for 'current' date. But how to get it now(-1d) ?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 23, 2024, 7:51pm UTC](https://discuss.elastic.co/t/cel-input-how-to-subtract-1day-from-now/351894/2 "2024-02-23T19:51:46Z")

</div>

Closed on February 23, 2024, 7:51pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.

---

<div class="post-metadata">

**Author:** ![Andre\_Letterer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andre_letterer/32/42248_2.png) [@Andre\_Letterer](https://discuss.elastic.co/u/Andre_Letterer)\
**Post date:** [February 6, 2025, 1:00pm UTC](https://discuss.elastic.co/t/cel-input-how-to-subtract-1day-from-now/351894/3 "2025-02-06T13:00:06Z")

</div>

I've just seen this thread and wanted for the sake of completeness to set an answer.

Afaiu the issue is about this particular part:  
`now.format("2006-01-01")`

First of all the reference date should be for a correct rendering `2006-01-02` as second January 2006 was a Monday, see [here](https://go.dev/src/time/format.go).

And then for for subtraction or addition of time fractions, CEL has `duration`. [Here](https://codelabs.developers.google.com/codelabs/cel-go#8) are some examples.  
In addition to that, `duration` only accepts `h`, `m`, `s`, so you need to convert the `1d` into `24h`.

Son in this case a valid example can be `now - duration("24h")`. That one still contains both date as well as time, so you wanted to format down to the time, which would need you to encapsulate into brackets: `now - duration("24h")).format("2006-01-02")`. and finally for being complete I would enclose the whole statement into brackets.

Which basically means you would convert the program fraction:  
`..."&date=" + now.format("2006-01-01") )`  
into:  
`..."&date=" + (now - duration("24h")).format("2006-01-02") ) ) )`
