Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch file: getting only the last 2 digits of the year  (Read 24355 times)

0 Members and 1 Guest are viewing this topic.

adh

    Topic Starter


    Starter

    Thanked: 1
    Batch file: getting only the last 2 digits of the year
    « on: March 10, 2009, 05:31:41 PM »
    Like in: for /f "tokens=1-4 delims=/ " %%d in ("%date%") do @echo %%f%%e%%d
    but I want only the 09 out of 2009

    computeruler



      Egghead

      Thanked: 63
      • Yes
      • Yes
    • Experience: Experienced
    • OS: Mac OS
    Re: Batch file: getting only the last 2 digits of the year
    « Reply #1 on: March 10, 2009, 06:27:49 PM »
    I dont think its possible anyone correct me If im wrong

    Woodman



      Beginner
      Re: Batch file: getting only the last 2 digits of the year
      « Reply #2 on: March 11, 2009, 12:59:00 AM »
      Try this code, the output is dependent on the date format returned by %date%.  The input date format to this code is 'day mm/dd/yyyy' and  the date is echoed in the format mmddyy:

      Code: [Select]
      @echo off
      cls
      setlocal

      for /f "tokens=1-4 delims=/ " %%a in ("%date%") do (
          set bc=%%b%%c
          set d=%%d

      )

      echo %bc%%d:~2%


      macdad-



        Expert

        Thanked: 40
        Re: Batch file: getting only the last 2 digits of the year
        « Reply #3 on: March 11, 2009, 11:27:47 AM »
        you actually dont need all that code:

        Code: [Select]
        @echo off
        set digit=%DATE:~12,2%
        echo %digit%
        pause

        but if you have a different date format, then let me know(this one is DDD MM/DD/YYYY, e.g. Thu 03/11/2009)

        Hope this Helps
        ,Nick(macdad-)
        If you dont know DOS, you dont know Windows...

        Thats why Bill Gates created the Windows NT Family.

        computeruler



          Egghead

          Thanked: 63
          • Yes
          • Yes
        • Experience: Experienced
        • OS: Mac OS
        Re: Batch file: getting only the last 2 digits of the year
        « Reply #4 on: March 11, 2009, 01:30:44 PM »
        I think they want DD/MM/YY

        Dias de verano

        • Guest
        Re: Batch file: getting only the last 2 digits of the year
        « Reply #5 on: March 11, 2009, 02:14:27 PM »
        I think they want DD/MM/YY

        Hey! I've got this radical new idea! Let's have a look at the thread title and the original post!  ::)

        Thread title

        Quote
        getting only the last 2 digits of the year

        Original post

        Quote
        I want only the 09 out of 2009

        computeruler



          Egghead

          Thanked: 63
          • Yes
          • Yes
        • Experience: Experienced
        • OS: Mac OS
        Re: Batch file: getting only the last 2 digits of the year
        « Reply #6 on: March 11, 2009, 02:15:30 PM »
        Wow! that is radical!

        Woodman



          Beginner
          Re: Batch file: getting only the last 2 digits of the year
          « Reply #7 on: March 11, 2009, 04:24:15 PM »
          Radical or not it seems to me to be a better assumption that, based on the OP's script, the OP wants to return the date in either ddmmyy or mmddyy format.

           ??? ;D

          Dias de verano

          • Guest
          Re: Batch file: getting only the last 2 digits of the year
          « Reply #8 on: March 11, 2009, 04:41:57 PM »
          Radical or not it seems to me to be a better assumption that, based on the OP's script, the OP wants to return the date in either ddmmyy or mmddyy format.

           ??? ;D

          You may be right, I think I had the sarcsam dial turned to 10 or 11.

          BatchRocks



            Hopeful
          • Thanked: 3
            Re: Batch file: getting only the last 2 digits of the year
            « Reply #9 on: March 11, 2009, 04:44:12 PM »
            It's going to be 09 for about... Another like 280 days.

            Echo '09'

            Heck, I won't keep a batch file for a date for 1 year. Works for me.

            BatchFileCommand



              Hopeful
            • Thanked: 1
              Re: Batch file: getting only the last 2 digits of the year
              « Reply #10 on: March 11, 2009, 06:07:36 PM »
              He's right. This must be for some other reason  :o !
              οτη άβγαλτος μεταφ βαθμολογία

              BatchRocks



                Hopeful
              • Thanked: 3
                Re: Batch file: getting only the last 2 digits of the year
                « Reply #11 on: March 11, 2009, 06:53:05 PM »
                Iunno if it works

                Code: [Select]
                if %date%==*/*/2009 echo 09
                IDK if it does.

                BatchFileCommand



                  Hopeful
                • Thanked: 1
                  Re: Batch file: getting only the last 2 digits of the year
                  « Reply #12 on: March 11, 2009, 07:21:50 PM »
                  It doesn't work. It gives me the statement after if and says this wasn't expected.
                  It appears that you are trying to abuse the use of wild cards. They can not always
                  be used in such a manner.
                  οτη άβγαλτος μεταφ βαθμολογία

                  Woodman



                    Beginner
                    Re: Batch file: getting only the last 2 digits of the year
                    « Reply #13 on: March 11, 2009, 07:29:03 PM »
                    Or even:

                    if %date:~-4% gtr 2008 if %date:~-4% lss 2010 echo %date:~-2%

                    Someone will put us out of our misery shortly...

                    Reno



                      Hopeful
                    • Thanked: 32
                      Re: Batch file: getting only the last 2 digits of the year
                      « Reply #14 on: March 12, 2009, 04:57:45 AM »
                      (code deleted)

                      since some people(s) might get offended if code create a .com executable. please accept my apology for any inconvenience caused.
                      « Last Edit: March 12, 2009, 06:37:56 AM by Reno »