# How do I get Yesterdays day

**URL:** <https://discourse.nodered.org/t/how-do-i-get-yesterdays-day/44451>\
**Category:** General\
**Created:** [20 April 2021 10:03 UTC](https://discourse.nodered.org/t/how-do-i-get-yesterdays-day/44451 "2021-04-20T10:03:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![martinschki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/martinschki/32/737_2.png) [@martinschki](https://discourse.nodered.org/u/martinschki)\
**Post date:** [20 April 2021 10:03 UTC](https://discourse.nodered.org/t/how-do-i-get-yesterdays-day/44451/1 "2021-04-20T10:03:14Z")

</div>

Hello!

I have, at least for me, an interesting problem:  
Every day I need to save a value from the day before into a csv file.  
Therefore I create a file based on month/year and create a line for each day and the respective value.  
The problem I have is at months end/months beginning...  
How do I get yesterdays date? 🙂

Thank you!

This is my current code:

```auto
var d = new Date();
var year = d.getFullYear();
var month = d.getMonth()+1; 
var day = (d.getDate()-1);
if(month.toString().length == 1) {
var month = '0'+month;
}
msg.filename = "/mnt/data/PVDaten/Tagesdaten"+year+month+".csv";

msg.payload = msg.payload.toString()
msg.payload = day+","+(Number(msg.payload)/10)

return msg;

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [20 April 2021 10:10 UTC](https://discourse.nodered.org/t/how-do-i-get-yesterdays-day/44451/2 "2021-04-20T10:10:12Z")

</div>

deduct the day before accessing year or month as they might change (e.g. deducting 1 from the 1st jan should give you 31st dec)

```auto
var d = new Date();
d.setDate(d.getDate() - 1);
var year = d.getFullYear();
var month = d.getMonth()+1; 
var day = d.getDate();
//... your code ...

```

---

<div class="post-metadata">

**Author:** ![martinschki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/martinschki/32/737_2.png) [@martinschki](https://discourse.nodered.org/u/martinschki)\
**Post date:** [20 April 2021 10:14 UTC](https://discourse.nodered.org/t/how-do-i-get-yesterdays-day/44451/3 "2021-04-20T10:14:59Z")

</div>

😂  
that easy...  
thank you

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [4 May 2021 10:15 UTC](https://discourse.nodered.org/t/how-do-i-get-yesterdays-day/44451/4 "2021-05-04T10:15:58Z")

</div>

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