4

I have a string which has a date in this format: n_date=2014-04-20

I want to convert it into date data type

var Final_date = str2date(n_date,"yyyy-MM-dd");

but I get an error.

I'm do this in pentaho

Ani Menon
  • 27,209
  • 16
  • 105
  • 126
Mudi
  • 95
  • 1
  • 2
  • 13
  • Please post your error. – Bram Apr 28 '15 at 06:03
  • Pentaho is in Java so I have to ask - Java or JavaScript? – jdphenix Apr 28 '15 at 06:08
  • Could not apply the given format yyyy-MM-dd on the string for test value : Format.parseObject(String) failed (script#18) – Mudi Apr 28 '15 at 06:08
  • – jdphenix Javascript – Mudi Apr 28 '15 at 06:09
  • I got this n_date string from previous database input step in pentaho.I want to convert it into date with its actual format then i store it into Postgres db. – Mudi Apr 28 '15 at 06:14
  • A simple `var Final_date = new Date("2015-12-12") do not suits to you needs? – Alexis Paques Apr 28 '15 at 06:16
  • No,Bcz when i use new Date() it does not gave me an error. but when i check my date format in postgress it is wrong. – Mudi Apr 28 '15 at 06:21
  • 1
    is your question about Pentaho Data Integration (Kettle)? In which step you try to convert `String` to `Date`? Is it `Modified JavaScript Value` step? – mzy Apr 28 '15 at 11:29
  • Yes it is in Pentaho Data Integration and Modified JavaScript Value step. actully i get the date from previous step – Mudi Apr 28 '15 at 13:46

1 Answers1

7

You don't need a Java script step to do that. It's much easier and faster to use a calculator step or a select values step instead:

1) Calculator step: Create a new field, Final_date as a Copy of field A; on Field A put the name of your input string; Data type is date and on Conversion mask choose the yyyy-MM-dd format (you don't have to pick one from the dropdown menu, you can write your own);

2) Select values: on the Metadata panel, choose your input field and Date as the data type; as above, fill in the date format your data comes in.

The difference between 1) and 2) is that in the 1st case you get a new field of the Date type, whereas in the 2nd case you change the data type of the input field.

nsousa
  • 4,448
  • 1
  • 10
  • 15
  • Thanks nsousa.I will try with this step. – Mudi Apr 29 '15 at 08:59
  • I have table which contain String fields,I want to convert them into numbers. but my final output is wrong after apply the function. var n_avg_cpc=parseFloat(trim(avg_cpc)); if(avg_cpc> 0) { var n_avg_cpc=parseFloat(trim(avg_cpc))/1000000; } – Mudi May 18 '15 at 11:28
  • that should be a separate question, not a comment in this one. – nsousa May 18 '15 at 14:10
  • I know its a comment. reason for ask this things is here when i add this text in a ask question window. i was failed. despite all of my tries it give me error. – Mudi May 20 '15 at 06:48