# Weekyear\_week as scripted field

**URL:** https://discuss.elastic.co/t/weekyear-week-as-scripted-field/117770
**Category:** Kibana
**Created:** [January 31, 2018, 8:59am UTC](https://discuss.elastic.co/t/weekyear-week-as-scripted-field/117770 "2018-01-31T08:59:53Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jakn](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@jakn](https://discuss.elastic.co/u/jakn)
#### Post date: [January 31, 2018, 8:59am UTC](https://discuss.elastic.co/t/weekyear-week-as-scripted-field/117770/1 "2018-01-31T08:59:53Z")

</div>

How do I create a scripted field showing timestamp formatted as 'weekyear\_week'?

The weekyear\_week format is described here:  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html#date-math](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html#date-math)

For creating a scripted field containing dayOfWeek in my time zone (CET) I use the following in Kibana 6.

`LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['@timestamp'].value.millis), ZoneId.of('CET')).getDayOfWeek()`

---

<div class="post-metadata">

### Author: ![Joe\_Fleming](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joe_fleming/32/3561_2.png) [@Joe\_Fleming](https://discuss.elastic.co/u/Joe_Fleming)
#### Post date: [January 31, 2018, 7:34pm UTC](https://discuss.elastic.co/t/weekyear-week-as-scripted-field/117770/2 "2018-01-31T19:34:48Z")

</div>

I don't enough about Painless to actually help you out here, but looking at the [API reference](https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-api-reference.html), it seems like you might be able to specify a custom format using the [DateTImeFormatter](https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-api-reference.html#painless-api-reference-DateTimeFormatter).

---

<div class="post-metadata">

### Author: ![jpcarey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpcarey/32/46668_2.png) [@jpcarey](https://discuss.elastic.co/u/jpcarey)
#### Post date: [January 31, 2018, 8:03pm UTC](https://discuss.elastic.co/t/weekyear-week-as-scripted-field/117770/3 "2018-01-31T20:03:56Z")

</div>

I get `Unsupported field: OffsetSeconds"` when trying to use `DateTimeFormatter` on `LocalDateTime`.

This worked ok, although - I haven't really checked if it is correct.

```auto
if (doc['@timestamp'] != null) {
    Instant instant = Instant.ofEpochMilli( doc['@timestamp'].value.millis );
    ZoneId zoneId = ZoneId.of ( "America/Los_Angeles" );
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    DateTimeFormatter formatter = builder.appendPattern("YYYY'-W'ww").toFormatter();
    String formattedDate = formatter.format(
      ZonedDateTime.ofInstant ( instant , zoneId )
    );
    return formattedDate;
}

```

---

<div class="post-metadata">

### Author: ![jakn](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@jakn](https://discuss.elastic.co/u/jakn)
#### Post date: [February 9, 2018, 9:38am UTC](https://discuss.elastic.co/t/weekyear-week-as-scripted-field/117770/4 "2018-02-09T09:38:05Z")

</div>

> [@jpcarey](#):
>
> if (doc['@timestamp'] != null) {  
> Instant instant = Instant.ofEpochMilli( doc['@timestamp'].value.millis );  
> ZoneId zoneId = ZoneId.of ( "America/Los\_Angeles" );  
> DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();  
> DateTimeFormatter formatter = builder.appendPattern("YYYY'-W'ww").toFormatter();  
> String formattedDate = formatter.format(  
> ZonedDateTime.ofInstant ( instant , zoneId )  
> );  
> return formattedDate;  
> }

Unfortunately, the output does not comply with the local week definitions.  
If time zone is set to "Europe/Copenhagen" in the suggested script then the week changes between Saturday and Sunday although by definition a new week begins on Mondays in Denmark.

> **[Current Week in Denmark - Savvy Time](https://savvytime.com/current-week/denmark)**
>
> Current week number in Denmark. Week rules, Start and End dates, monthly calendar with current week. View current week in any country around the world. How many weeks in a year?

How should the script be adjusted to end up with a week definition valid in the time zone used in the scripted field?

---

<div class="post-metadata">

### Author: ![jakn](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@jakn](https://discuss.elastic.co/u/jakn)
#### Post date: [February 19, 2018, 7:17am UTC](https://discuss.elastic.co/t/weekyear-week-as-scripted-field/117770/5 "2018-02-19T07:17:30Z")

</div>

Any suggestions?

---

<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: [March 19, 2018, 7:17am UTC](https://discuss.elastic.co/t/weekyear-week-as-scripted-field/117770/6 "2018-03-19T07:17:53Z")

</div>

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