Announcement

Collapse
No announcement yet.

Yesterday's Date Filename

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Yesterday's Date Filename

    Greetings,

    I have spent more hours than I care to admit trying to figure out how to create my own custom Destination Mapping variable for a "Date -1"

    scenario. I have only found javascript code examples but there is no step by step instructions on where to place the code and how to execute.

    Details: FTP source to an FTP destination no tranformation scrictly passing data.

    I am using Mirth Connect Version 2.1.1.5490

    I would greatly appreciate any help. Thanks in advance!

  • #2
    If you're dealing with a java.util.Date, I believe you can do something like Date.setTime(Date.getTime()-(1000*60*60*24)). Even better though is to use a java.util.Calendar object. Then you can just call .add(Calendar.DATE,-1).

    From there, you can either invoke a SimpleDatePattern yourself, or use DateUtil to convert the date to a string specified by an output pattern.
    Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

    Nicholas Rupley
    Work: 949-237-6069
    Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


    - How do I foo?
    - You just bar.

    Comment


    • #3
      Here's what I needed a while ago.

      Code:
      var kalender = java.util.Calendar.getInstance();
      kalender.add(java.util.Calendar.DATE, -1);
      
      //format: yyyyMMdd
      var date = kalender.get(kalender.YEAR).toString()+ZeroPadLeft((kalender.get(kalender.MONTH)+1).toString(), 2)+ZeroPadLeft(kalender.get(kalender.DATE).toString(), 2) + "";
      ZeroPadLeft is used to fill the leading zero's (selfmade function)
      the reason I use + "" at the end is to ensure it is a javascript string rather than a java string object.
      Stefan

      Mirth Certified|Epic Bridges Certified|Cloverleaf Level 2 Certified

      Comment


      • #4
        Thanks! I just needed this for a filename, in the source transformer.
        You can use getTime() and DateUtil.formatDate instead of your ZeroPadLeft function as in the following code:

        Code:
        var kalender = java.util.Calendar.getInstance();
        kalender.add(java.util.Calendar.DATE, -1);
        
        //format: yyyyMMdd
        var yesterday = DateUtil.formatDate("yyyyMMdd",kalender.getTime());

        Comment


        • #5
          Yesterday's Date Filename

          What I am trying to do is use the "Destination Mappings" variables under the destinations tab to create a filename with yesterday's date for the output.

          I have tried adding "-1" to this example below but doesn't work.

          Example: myfile_${date.get('yyyy-M-d H.m.s')}

          Comment


          • #6
            You could probably do this in the template by calling date.getDate(), etc. But an easier way is to add a transformer step that puts your pre-formatted date string into the connector map. Then, you'll be able to drag it over from the Destination Mappings frame.
            Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

            Nicholas Rupley
            Work: 949-237-6069
            Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


            - How do I foo?
            - You just bar.

            Comment


            • #7
              I'm not sure what to put in the Transformer step (mapping field) to return yesterdays date formatted. Do you have an example. Thanks in advance.

              I can't upload the channel as it exceeds the max file size. Version is 2.1.1.5490
              Last edited by rmcginness; 09-06-2012, 12:15 PM.

              Comment


              • #8
                Just create a JavaScript transformer step, and place your date string in a map variable. Here's an example using Joda:

                Code:
                $co('yesterday',org.joda.time.format.DateTimeFormat.forPattern('yyyyMMdd').print((new org.joda.time.DateTime()).minusDays(1)));
                Then you can include it in your filename with the following Velocity reference:

                Code:
                ${yesterday}
                Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

                Nicholas Rupley
                Work: 949-237-6069
                Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


                - How do I foo?
                - You just bar.

                Comment


                • #9
                  I would LOVE to be able to get yesterday's date BEFORE looking for a file.

                  Scenario: I have a dir with several files each containing the date. "Created_File_20141028" is an example. right now I have channels set to run at 12:10 am and check file age at 600000 ms.

                  Is it possible to look for a file with the previous date in the filename? Like "Created_File_$(yesterday)" or something.

                  Or would it be best to do all this in a Javascript channel?

                  Comment


                  • #10
                    Originally posted by appsbyaaron View Post
                    I would LOVE to be able to get yesterday's date BEFORE looking for a file.

                    Scenario: I have a dir with several files each containing the date. "Created_File_20141028" is an example. right now I have channels set to run at 12:10 am and check file age at 600000 ms.

                    Is it possible to look for a file with the previous date in the filename? Like "Created_File_$(yesterday)" or something.

                    Or would it be best to do all this in a Javascript channel?
                    Right now, the File Reader fields are only replaced once when the channel is deployed. So it would work the first time, but perhaps not subsequent times. We have an issue for that: MIRTH-3131.

                    In the meantime, unfortunately yeah it would have to be done with a JavaScript Reader instead. Assuming you're just reading from the local filesystem, FileUtils makes it really easy though.

                    Of course the File Age method works as well.
                    Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

                    Nicholas Rupley
                    Work: 949-237-6069
                    Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


                    - How do I foo?
                    - You just bar.

                    Comment


                    • #11
                      Originally posted by narupley View Post
                      Right now, the File Reader fields are only replaced once when the channel is deployed. So it would work the first time, but perhaps not subsequent times. We have an issue for that: MIRTH-3131.

                      In the meantime, unfortunately yeah it would have to be done with a JavaScript Reader instead. Assuming you're just reading from the local filesystem, FileUtils makes it really easy though.

                      Of course the File Age method works as well.
                      Thanks "narupley" this line worked for me.

                      $co('yesterday',org.joda.time.format.DateTimeForma t.forPattern('yyyyMMdd').print((new org.joda.time.DateTime()).minusDays(1)));

                      Comment

                      Working...
                      X