No internet connection
  1. Home
  2. How to

Date and string

By N Leyers @NickLeyers_old_acc
    2019-10-03 15:08:58.532Z

    Hi there

    This came up before I think: is it possible to give in the value of the date?
    Right now, I use KM for this but SF can do as well I guess.
    I always start every filename with the date (yymmdd).
    Right now I'll type 3 times 'd' and it's replaced by the date.

    So that's for the 2the request: can a key or wordstring be a trigger for a macro?... That's very powerfull because I'm running out of key-combinations to remember ;-)

    Solved in post #2, click to view
    • 18 replies
    1. @NickLeyers + @Nick_Leyers (just in case hehe),

      thanks for asking.

      Yes Javascript is pretty good at handling date related stuff. To get it out as yymmdd, you'll have to do something like this:

      function yymmdd() {
          var now = new Date();
          return now.getFullYear().toString().slice(-2) + 
                 ('0' + (now.getMonth() + 1).toString()).slice(-2) +
                 ('0' + now.getDate().toString()).slice(-2);
      }
      
      log(yymmdd());
      
      Reply1 LikeSolution
      1. I moved this to the How to section (for the part about dates) - the request for key sequences I believe we've already registered (at least I remember it and we do think about it from time to time).

        1. In reply tochrscheuer:
          NN Leyers @NickLeyers_old_acc
            2019-10-07 08:58:30.189Z

            Nice, thanks.
            If I use this at the end (witout the log)
            sf.keyboard.press({ keys: (yymmdd()) });
            Than I get 'a' as result. Is there a better way?

            1. Yes you need the sf.keyboard.type to type a string of characters. press is for special keys :)

        2. AlexanderW @AlexanderW
            2020-09-14 19:44:25.237Z

            I'd like to jump in here,
            i have tried to extract a function from the above that inserts the current date in the format YYYYMMDD at the current cursor position, as this would be helpful when naming delivery files. Alternatively also date and time would be great.
            What I haven't got yet is that the date is 'typed in' to the naming field.

            Best,
            Alex

            1. NN Leyers @NickLeyers_old_acc
                2020-09-15 16:56:37.241Z

                If you use the sf.keyboard.type, the date is 'typed'?

                1. AlexanderW @AlexanderW
                    2020-09-15 19:00:17.283Z

                    Thank you Nick for asking. I am not very experienced with scripting.
                    After reading this thread I have tried the following, but it threw an error message:
                    function yyyymmdd() {
                    var now = new Date();
                    return now.getFullYear().toString().slice(-0) +
                    ('0' + (now.getMonth() + 1).toString()).slice(-2) +
                    ('0' + now.getDate().toString()).slice(-2);
                    }

                    log(yyyymmdd());
                    sf.keyboard.type({ keys: (yyyymmdd()) });

                    1. Kitch Membery @Kitch2020-09-15 19:24:36.665Z

                      Hi AlexanderW!,

                      function yyyymmdd() {
                          var now = new Date();
                          return now.getFullYear().toString().slice() + 
                                 ('0' + (now.getMonth() + 1).toString()).slice(-2) +
                                 ('0' + now.getDate().toString()).slice(-2);
                      }
                      
                      const todaysDate = yyyymmdd();
                      
                      sf.keyboard.type({
                          text: todaysDate,
                      })
                      

                      This should do it :-)

                      1. AlexanderW @AlexanderW
                          2020-09-15 21:34:59.530Z

                          Hi Kitch,
                          awesome, that did the trick. Thank you for helping out.
                          It's also great that I learn a little bit at every step just by looking at the scripts.

                          Cheers,
                          Alex

                          1. Kitch Membery @Kitch2020-09-15 21:37:22.212Z

                            You’re totally welcome:-)

                          2. In reply toKitch:
                            GGabriel Lundh @Gabriel_Lundh
                              2021-09-03 07:46:45.728Z

                              Hi master Kitch!

                              I am trying my best to rearrange the output of todaysDate. I have a structure that I have to follow for a job which is MMDDYY. So I tried to arrange it:

                              function mmddyy() {
                                  var now = new Date();
                                  return now.getMonth() +1).toString()).slice(-2) +
                                         ('0' + (now.getDate().toString()).slice(-2) +
                                         ('0' + now.getFullYear().toString()).slice(-2);
                              
                              }
                              

                              But for some reason this throws the script of... How should I go about doing this?

                              You rock!
                              All the best,
                              Gabriel

                              1. Kitch Membery @Kitch2021-09-03 07:57:59.810Z

                                Hi @Gabriel_Lundh

                                This should do the trick :-)

                                function mmddyy() {
                                    const now = new Date();
                                
                                    const yy = now.getFullYear().toString().slice(-2); 
                                    const mm = ('0' + (now.getMonth() + 1).toString()).slice(-2);
                                    const dd = ('0' + now.getDate().toString()).slice(-2);
                                
                                    return mm+dd+yy;
                                }
                                
                                const todaysDate = mmddyy();
                                
                                log(todaysDate);
                                

                                Rock on!

                                1. GGabriel Lundh @Gabriel_Lundh
                                    2021-09-03 08:04:30.898Z

                                    Its hard to show how much I appreciate these quick and perfect turnarounds!
                                    Thank you so much, You rule Kitch!!

                                    1. Kitch Membery @Kitch2021-09-03 08:17:45.332Z

                                      You're totally welcome mate! :-)

                                      Rock on!

                                    2. In reply toKitch:
                                      JJosh Reinhardt @Josh_Reinhardt
                                        2024-01-30 05:15:32.762Z

                                        I'm wanting to use this script to enter the date text into a track name. I'd like to add this as an action in a macro so it types the current date followed by other actions. How can this be done? Thanks in advance.

                                        1. Kitch Membery @Kitch2024-01-30 18:23:39.058Z

                                          Hi @Josh_Reinhardt,

                                          To do that, create a script and give it a name like "Date Stamp".

                                          Add the following code to the script.

                                          function mmddyy() {
                                              const now = new Date();
                                          
                                              const yy = now.getFullYear().toString().slice(-2);
                                              const mm = ('0' + (now.getMonth() + 1).toString()).slice(-2);
                                              const dd = ('0' + now.getDate().toString()).slice(-2);
                                          
                                              return mm + dd + yy;
                                          }
                                          
                                          const todaysDate = mmddyy();
                                          
                                          sf.keyboard.type({ text: todaysDate });
                                          

                                          You can then create a new macro and add the "Date Stamp" script as an action using the "Add Action button" to the macro, followed by other actions.

                                  • In reply toAlexanderW:
                                    Kitch Membery @Kitch2020-09-15 19:27:56.706Z

                                    Oh and btw... You are probably wondering how to format your code in the forum.

                                    To format the code surround it with 3 backticks on their own line (one line of 3 backticks before the code, one line of 3 backticks after the code) - ```

                                    Rock on!