-1

i am using batch and i need help formatting this one thing, on saved file the date changes everyday but the "yyyy" is just put down as "yy"

Example: if the year was "2021" the name would be "21"

I havent found anything that could get me the last 2 numbers on the year.

Please help as i need this for work.

Sincerely, Kythe

PyBatch
  • 16
  • 5
  • try dir /4 you question i meaningless without context – Magoo Mar 04 '21 at 14:57
  • 1
    [How do I get current date or time on the Windows command line in a suitable format for usage in a file or folder name?](https://stackoverflow.com/questions/203090/how-do-i-get-current-date-time-on-the-windows-command-line-in-a-suitable-format) – Squashman Mar 04 '21 at 16:59

1 Answers1

0

You might cut out the part you do not need:

set DT=%date%

set dd=%DT:~0,2% 
set mm=%DT:~3,2% 
set yy=%DT:~8,2%

echo %dd%
echo %mm%
echo %yy%

echo "day: %dd%, month: %mm%, year: %yy%"

pause

set variable=%date:~start,length% assigns to the variable all the the characters of date indexed between start (counting from 0) and (start + length-1)


EDIT:
On other machine date might be formatted differently, as @Squashman pointed out.
To properly cut out the part you need, try:

echo %date%

Now set dd, mm and yy in the correct way you need them to be set

Hiderr
  • 90
  • 9
  • 1
    The `%date%` variable is region and locale dependent. Not everyone's computer outputs the `%date%` variable in the same format. – Squashman Mar 04 '21 at 15:19
  • This outputs: "day: Th , month: 0 , year: 4/" – PyBatch Mar 04 '21 at 15:22
  • 1
    @Kythe, the topic of getting the date into a variable has been covered hundreds of times on StackOverFlow. You could have easily found this same information from searching the site. There are plenty of answers on how to get the date variable in a standard format without using the `%date%` variable. The better option would be to call out to `WMIC` or `Powershell`. You will find several answers on how to use both of them. – Squashman Mar 04 '21 at 16:57