# Converting century date format to yyyymmdd format

**URL:** <https://discuss.elastic.co/t/converting-century-date-format-to-yyyymmdd-format/169048>\
**Category:** Logstash\
**Created:** [February 19, 2019, 3:18pm UTC](https://discuss.elastic.co/t/converting-century-date-format-to-yyyymmdd-format/169048 "2019-02-19T15:18:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shruti51](https://avatars.discourse-cdn.com/v4/letter/s/e274bd/32.png) [@shruti51](https://discuss.elastic.co/u/shruti51)\
**Post date:** [February 19, 2019, 3:18pm UTC](https://discuss.elastic.co/t/converting-century-date-format-to-yyyymmdd-format/169048/1 "2019-02-19T15:18:38Z")

</div>

Hi,

I am trying to convert century format CYYMMDD to YYYYMMDD format in logstash.  
Please provide some inputs of how to do it.

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [February 19, 2019, 3:23pm UTC](https://discuss.elastic.co/t/converting-century-date-format-to-yyyymmdd-format/169048/2 "2019-02-19T15:23:24Z")

</div>

I do not understand the distinction between CYY, which would have the first two digits of the year followed by the last two digits of the year, and YYYY, which would look exactly the same.

---

<div class="post-metadata">

**Author:** ![shruti51](https://avatars.discourse-cdn.com/v4/letter/s/e274bd/32.png) [@shruti51](https://discuss.elastic.co/u/shruti51)\
**Post date:** [February 19, 2019, 3:30pm UTC](https://discuss.elastic.co/t/converting-century-date-format-to-yyyymmdd-format/169048/3 "2019-02-19T15:30:58Z")

</div>

Hi Badger,  
Thanks For the response.

Regarding the query,CYY will be 119 whereas YYYY will be 2019.

I have to compare my dates. For that My one format is century format. So i am trying to convert my century format to YYYYmmdd format.

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [February 19, 2019, 4:04pm UTC](https://discuss.elastic.co/t/converting-century-date-format-to-yyyymmdd-format/169048/4 "2019-02-19T16:04:31Z")

</div>

You could use something like

```
if [someField] =~ /1[0-9]{6}/ {
    mutate { gsub => ["someField", "^1", "20"] }
}
```

---

<div class="post-metadata">

**Author:** ![shruti51](https://avatars.discourse-cdn.com/v4/letter/s/e274bd/32.png) [@shruti51](https://discuss.elastic.co/u/shruti51)\
**Post date:** [February 22, 2019, 10:27am UTC](https://discuss.elastic.co/t/converting-century-date-format-to-yyyymmdd-format/169048/5 "2019-02-22T10:27:59Z")

</div>

Hi badger,

I tried this function but it didnt work. Please find the configuration:

```
input {
  beats {
    port => 5044
	}
}

  if [fields][log_type] == "SAS" {
		xml {
				source => "message"
				store_xml => false
				xpath => ["/ABC/@NU", "ABD"]
				xpath => ["/pATH/Date/text()", "StartDate"]
}

mutate { 
				add_field => ["CenturyDate", "%{StartDate}"]
		}
if [fields][CENTURYDATE] =~ /1[0-9]{6}/ 
		{
		   mutate { gsub => ["CENTURYDATE", "^1", "20"] }
}

```

For input start date: 1190324,  
Ouput: Centurydate is 1190324.

I want my output as 21190324.

Please help.

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [February 22, 2019, 1:29pm UTC](https://discuss.elastic.co/t/converting-century-date-format-to-yyyymmdd-format/169048/6 "2019-02-22T13:29:40Z")

</div>

> [@shruti51](#):
>
> mutate { add\_field =\> ["CenturyDate", "%{StartDate}"] }  
> if [fields][CENTURYDATE] =~ /1[0-9]{6}/

If you add the date to a field called CenturyDate then you need to test for that field, not for some other field. Change the conditional to be

```
if [CenturyDate] =~ ...

```

and likewise the mutate+gsub

---

<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 22, 2019, 1:34pm UTC](https://discuss.elastic.co/t/converting-century-date-format-to-yyyymmdd-format/169048/7 "2019-03-22T13:34:52Z")

</div>

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