Hitachi Vantara Pentaho Community Forums
Results 1 to 6 of 6

Thread: Converting string to Date

  1. #1

    Default Converting string to Date

    I am passing a date string in to a transformation as an environment variable. I need to convert this to a date to use in my transformations.

    The environment variable is in the from "dd/MM/yyyy".

    The javascript I use is as

    var now = str2date(TODAY.getString(), "dd/MM/yyyy");

    where TODAY is the environment variable.

    When I use the "Test Script" I get an error stating that "Could not apply the given format on the string".

    When I run the script, it does not give an error. The javascript output goes to calculator that extracts month, day, year. As can be seen below, the variable "now" was not correctly transformed.

    2007/08/18 11:50:12 - Calculator.0 - Wrote row #1 : [TODAY=08/18/2007, now=2008/06/08 00:00:00.000, YEAR= 2008, MONTH= 6, DAY= 8]


    How do I create a date string to a Date type? It would be great if 'Get Variable' supported types.

  2. #2
    Join Date
    Jul 2007
    Posts
    2,498

    Default

    if TODAY is an env var, shouldn't you use getEnvVar('TODAY') or something like that?
    Pedro Alves
    Meet us on ##pentaho, a FreeNode irc channel

  3. #3
    Join Date
    May 2006
    Posts
    4,882

    Default

    get variable only support strings.

    The test button does not work in this case as that will fill in some random string as data which will not match your expected data.

    And Viraf, why do you think it was converted the wrong way. the date part is filled in, the time not... what you see is only representation... try outputting the data to a file using a format e.g.

    Regards,
    Sven

  4. #4

    Default

    Sven, the environment variable was assigned to a Transformation variable named TODAY. I did check the output and it provided the same results (enclosed below).

    TODAY|YEAR|MONTH|DAY|now
    08/18/2007| 2008| 6| 8|2008/06/08 00:00:00.000

    So - what is the easiest way to convert a string to a date?

  5. #5
    Join Date
    May 2006
    Posts
    4,882

    Default

    For the output use a format specification. You have a date, but a date always includes the time part. You can just format it away ... something as "yyyy/mm/dd".

    Regards,
    Sven

  6. #6

    Default

    Thanks - I added the time and it appears to work. Under Modified Javascript the 'date2str' function had an example with it converting without time. One should probabaly fix the example as it set me down the wrong path.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Privacy Policy | Legal Notices | Safe Harbor Privacy Policy

Copyright © 2005 - 2019 Hitachi Vantara Corporation. All Rights Reserved.