Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

date format

Options
  • 28-05-2008 10:28am
    #1
    Closed Accounts Posts: 1,788 ✭✭✭


    I have this in a batch file..

    for /F "tokens=2-4 delims=/ " %%I in ('date /t') do set datetag=%%J%%I%%K

    works for mm--dd-yy date format , but doesn't work for dd-mm-yy format ,
    how do i change it to work for dd-mm-yy ?


Comments

  • Closed Accounts Posts: 1,788 ✭✭✭jackdaw


    or can someone link me to what these %%J %%I crap means ??

    google has been useless here ...


  • Registered Users Posts: 6,465 ✭✭✭MOH


    Is this a Windows batch file?

    They're variables (if you google "DOS variables" you should set some helpful hits).

    Seems to be calling DATE /T to return the date, parsing this by slashes(/), in %%I, %%J, %%K

    What do you mean by "works for mm--dd-yy date format , but doesn't work for dd-mm-yy format"? Are you trying to get the output in dd-mm-yy format?

    Try just replacing =%%J%%I%%K with =%%I%%J%%K

    [edit]
    Actually, that doesn't work for me.
    Do you mean it doesn't work when your system date format is dd-mm-yyyy rather than mm-dd-yyyy?

    Change it to:
    for /F "tokens=1-3 delims=/ " %%I in ('date /t') do set datetag=%%I%%J%%K

    That'll put the day portion in %%I, the month in %%J and the year in %%K.
    Then just rearrange datetag to have them in the order you want.


  • Registered Users Posts: 6,465 ✭✭✭MOH


    Been years since I looked at a DOS batch file, but from opening a command prompt and getting help on the FOR command with 'FOR /?', what this is doing is:

    - FOR /f usually processes a file one line at a time, but in this case it's processing the results of DATE /t, which returns one line containing the system date

    - "tokens=2-4 delims=/" tells it how to parse the line: each token is delimited by a slash, and we only want the second, third and fourth tokens
    [This is the problem - don't see how this would work on any date format, but for dd/mm/yyyy you're basically ignoring the first token, which is the dd part]

    - the first token will go into a variable called %%I; the subsequent tokens will automatically go into successive variable names (%%J and %%K)

    - datetag then gets built up from %%J, %%I and %%K concatenated.

    (sorry for double post, but think this explanation is better separated from the solution)


Advertisement