![]() The following batch file uses wmic to retrieve the date and (local) time, so doesn't suffer the disadvantage of a solution using ?te%. Using wmic, on the other hand, works independently of OS Locale, Language or the user's chosen date format (Control Panel/Regional).Using ?te% to provide a solution is, as used in the other answer, dependent on the OS Locale, Regional, and Language settings and may provide wrong or missing information.įor example, the short date format on my PC does not output the weekday at all. I need to be displayed with this format dd/mm/yyyy How can I get the date in a batch file in a locale independent format? But if you want to use solely batch for this, then there it is. It's a good idea to know batch and to use it a bit but to install some other scripting language like Ruby or Python or Golang or NodeJS. And while in this case batch is in one sense, neat, sometimes it can be like banging sticks together. I would generally recommend against batch, because for example what if a new revision of the OS comes along and the output of a command is a bit different and your code presumes a particular format in its parsing. So you can say set ukdate=%second%/%first%/%third% and you can say echo %ukdate%Īnd that non-powershell method only works if you know the format of the date on the system, and know that it's US. So you really should be able to see how to do it set first=?te:~0,2%Ĭ:\Users\tod>echo %first%/%second%/%third%Ĭ:\Users\tod>echo %second%/%first%/%third% If you wanted to not use powershell and you don't mind that this will only work for US formatted dates. So you can set whatever variable to equal the year, month, date in month. Once you have that date in the format you kmow, and I suggest yyyy/mm/dd format, you can do C:\Users\tod>echo ?tetime:~0,4%Īnd so on. 2018 first so you know it's year first and then you know that after the year is the month and then the day. ![]() No worries about if it's dd/mm/yy like uk. yyyy/mm/dd format is very nice as it is easily human-readable in both the UK and USA. C:\Users\tod>for /f %a in ('C:\Windows\System32\WindowsPowershell\v1.0\powershell -Command "Get-Date -format yyyy_MM_dd"') do set datetime=%aĪnd by the way that Year/Month/Day i.e. C:\Users\tod>for /f %a in ('C:\Windows\System32\WindowsPowershell\v1.0\powershell -Command "Get-Date -format yyyy_MM_dd_HH_mm_ss"') do set datetime=%aĬ:\Users\tod>set datetime=2018_01_22_09_15_33 Better to get the date in a specific format, even if that means not pure batch but invoking powershell. NET - download from Microsoft ( v1, v2, v3 (only for Windows 7 and above)).It sounds like a bad idea to be parsing a date that could be in various formats which format you don't know. PowerShell - can be installed on every machine that has. WScript.echo Replace(FormatDateTime(Date,1),", ","-") '3 = vbLongTime - Returns time: hh:mm:ss PM/AM '2 = vbShortDate - Returns date: mm/dd/yy '1 = vbLongDate - Returns date: weekday, monthname, year Returns date: mm/dd/yy and time if specified: hh:mm:ss PM/AM. Makecab /D RptFileName=~.rpt /D InfFileName=~.inf /f nul >nulįor /f "tokens=3-7" %%a in ('find /i "makecab"^nul 2>
0 Comments
Leave a Reply. |