# How to get current date in script

**URL:** <https://discuss.elastic.co/t/how-to-get-current-date-in-script/19794>\
**Category:** Elasticsearch\
**Created:** [September 15, 2014, 4:11pm UTC](https://discuss.elastic.co/t/how-to-get-current-date-in-script/19794 "2014-09-15T16:11:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhishek\_Gupta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhishek_gupta/32/512_2.png) [@Abhishek\_Gupta](https://discuss.elastic.co/u/Abhishek_Gupta)\
**Post date:** [September 15, 2014, 4:11pm UTC](https://discuss.elastic.co/t/how-to-get-current-date-in-script/19794/1 "2014-09-15T16:11:44Z")

</div>

Hi,

I want to write a script to calculate the number of days from current date  
on a date field. I can use a script like:  
"script\_fields": {  
"test": {  
"script": "doc['date'].date.year \* doc['date'].date.dayOfYear - "  
}  
}

How to get the current date? No date related function is mentioned in the  
help(  
[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html)  
).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/81d6726b-88ca-4a62-829d-0a7b970b8669%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/81d6726b-88ca-4a62-829d-0a7b970b8669%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![vineeth\_mohan\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vineeth_mohan_2/32/747_2.png) [@vineeth\_mohan\_2](https://discuss.elastic.co/u/vineeth_mohan_2)\
**Post date:** [September 15, 2014, 4:23pm UTC](https://discuss.elastic.co/t/how-to-get-current-date-in-script/19794/2 "2014-09-15T16:23:42Z")

</div>

Hello Abhishek ,

Something similar to this should work -

{  
"script\_fields": {  
"test": {  
"script": "doc['date'].value - new Date().getTime() \* 1000"  
}  
}  
}

Thanks  
Vineeth

On Mon, Sep 15, 2014 at 9:41 PM, Abhishek Gupta [abhi.bansal21@gmail.com](mailto:abhi.bansal21@gmail.com)  
wrote:

> Hi,
> 
> I want to write a script to calculate the number of days from current date  
> on a date field. I can use a script like:  
> "script\_fields": {  
> "test": {  
> "script": "doc['date'].date.year \* doc['date'].date.dayOfYear - "  
> }  
> }
> 
> How to get the current date? No date related function is mentioned in the  
> help(  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html)  
> ).
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/81d6726b-88ca-4a62-829d-0a7b970b8669%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/81d6726b-88ca-4a62-829d-0a7b970b8669%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/81d6726b-88ca-4a62-829d-0a7b970b8669%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/81d6726b-88ca-4a62-829d-0a7b970b8669%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAGdPd5nzE6oDppCzf67ED3d%2Bhgq995wPDTi7f7rOPSBsyE578Q%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGdPd5nzE6oDppCzf67ED3d%2Bhgq995wPDTi7f7rOPSBsyE578Q%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [July 6, 2017, 1:02am UTC](https://discuss.elastic.co/t/how-to-get-current-date-in-script/19794/3 "2017-07-06T01:02:15Z")

</div>


