-1

I need to create a javascript function to get yesterdays date and time as midnight 00:00:00. For example 2018-08-03 00:00:00. The set.Hours() function is not working in my case. The script is to be used in spoon pentaho data integration. How i can do this. Please help

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Bommu
  • 229
  • 1
  • 4
  • 14
  • 2
    What is going wrong ? What do you mean by _The set.Hours() function is not working in my case._ ? – Rayon Aug 04 '18 at 05:06
  • 1
    `The set.Hours() function` you mean the `dateObject.setHours()` function? because `set.Hours` looks like you're "doing it wrong"™ – Jaromanda X Aug 04 '18 at 05:20
  • yes the same function. i have to use this javascript function inside spoon software. So dateObject.setHours() is not working in this software. – Bommu Aug 04 '18 at 05:40

2 Answers2

2

You should firstly subtract one day, then set the hours to 00:00:00, e.g.

var d = new Date();
d.setDate(d.getDate() - 1);
d.setHours(0,0,0,0);

console.log(d.toString());

If the setHours function is not working for you (i.e. not available or broken, which seems very peculiar), then you could use:

var d = new Date();
d.setDate(d.getDate() - 1);
d = new Date(d.getFullYear(), d.getMonth(), d.getDate());

console.log(d.toString());

// Or even
var e = new Date();
e = new Date(e.getFullYear(), e.getMonth(), e.getDate() - 1);

console.log(e.toString());
RobG
  • 142,382
  • 31
  • 172
  • 209
0

In Pentaho Data Integration you should always try and use the other steps before resorting to JavaScript, as the performance lowers significantly.

Use a Formula step with the function Today()(which records the current date at 00:00:00) for a column, create a colum with the value -1, and use the Calculator step with the operation DATE A + B Days.

If at some point you want the exact time of the date, you can use the function Now() in the formula step.

Cristian Curti
  • 1,004
  • 1
  • 7
  • 13