To give proper credit, I merged the concepts from Peter Mortensen Jun 18 '14 at and opello Aug 25 '11 at You can write this much shorter, but this long version makes reading and understanding the code easy. I like the short version on top of The lorax, but for other language settings it might be slightly different. For example, in german language settings with natural date format: dd.
This is my 2 cents for a datetime string. Then double click it. If there is a 0 in the first 2 digits of the time, Windows ignores the rest of the file name of the LOG file. I tried the accepted answer and it works pretty well.
CS, and the missing 0 on the front was causing parsing problems before 10 am. To get over this hurdle and also allow parsing of most any of the world time formats, I came up with this simple routine that appears to work quite well.
The nice thing with this routine is that you pass in the time string as the first parameter and the name of the environment variable you want to contain the time in centiseconds as the second parameter.
For example:. In situations like this use a simple, standard programming approach: Instead of expending a huge effort parsing an unknown entity, simply save the current configuration, reset it to a known state, extract the info and then restore the original state. Use only standard Windows resources.
Reg is the console registry editor included with all Windows versions. Elevated privileges are not required to modify the HKCU key. For reasons I don't understand, resetting the "sShortDate" value takes effect immediately in a console window but resetting the very similar "sTimeFormat" value does NOT take effect until a new console window is opened.
However, the only thing changeable is the delimiter - the digit positions are fixed. Likewise the "HH" time token is supposed to prepend leading zeros but it doesn't. Fortunately, the workarounds are easy.
So most of the previous answers did not work for me. If you are not picky about the exact format and just want a timestamp to differentiate the files you can do this:. This should work for most locales. How are we doing?
Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Format date and time in a Windows batch script Ask Question. Asked 12 years, 5 months ago. Active 3 months ago.
Viewed k times. All logos and trademarks in this site are property of their respective owner. Windows Batch File. Jimmy Selix is an early adopter that loves to be one of the first on the block to have the latest and greatest in technology and gadgets.
Another love of his is being able to share his knowledge to others seeking it. Feel free to drop any comments or questions that you may have. NOTE: Copying date. And both also get the day of the week and none of them requires admin permissions! More information about get-date function.
But is built-in in everything from Windows Vista and above:. And three more ways that uses other Windows script languages. They will give you more flexibility e. JScript is available on every system form NT and above, as a part of Windows Script Host though can be disabled through the registry it's a rare case :.
PowerShell - can be installed on every machine that has. NET - download from Microsoft v1 , v2 , v3 only for Windows 7 and above. Self-compiled jscript. NET, so I think this is a pretty portable :. Logman This cannot get the year and day of the week.
It's comparatively slow and also creates a temporary file and is based on the time stamps that logman puts on its log files. It will work on everything from Windows XP and above. It probably will be never used by anybody - including me - but is one more way One more way with WMIC which also gives week of the year and the day of the week, but not the milliseconds for milliseconds check foxidrive's answer :.
Bear in mind that JavaScript's Date object properties involving month values are numbered from 0 to 11, not 1 to So a value of 9 means October.
The quotes yield a token albeit empty for the left-hand side of the assignment statement. Amazing the stuff you can find on Wikipedia. Another way credit :. Note that both my answers here are still reliant on the order of the day and month as determined by regional settings - not sure how to work around that. This isn't really briefer but might be a more flexible way credit :.
In the US this will output "yyyy-mm-dd hhmm". If you want a different format, modify the echo statement by rearranging the tokens or using different or no separators. Matthew Johnson's one-liner solution to get the one-liner date and time is eloquent and useful.
I changed the answer with the batch file from vMax so it works with the Dutch language too. It would be nice if some people can check this against other Windows locales as well, and report back the results. Upside: Region independent.
Downside: Only system administrators can run wmic. Note that function parameters are out-Parameters - that is, you must supply variable names instead of values. All parameters are optional, so call:Now Y M is a valid call if you only want to get year and month. I had a similar problem. I have an automatic daily download from an FTP server of an encrypted file. I wanted to decrypt the file using gpg, rename the file to the current date YYYYMMDD format and drop the decrypted file into a folder for the correct department.
I went through several suggestions for renaming the file according to date and was having no luck until I stumbled upon this simple solution. Look inside the ZIP file for something called "Date. What could still be optimized: Other processes might get confused if using the date format in the short period while it is modified. So parsing the output according to the existing format string could be 'safer' - but will be more complicated.
I used date. PowerShell Try the code below. It will create the file or folder varible with the date as ddmmyyhhmm in 24hour time. Given a known locality, for reference in functional form. I know that there are numerous ways mentioned already. But here is my way to break it down to understand how it is done. Hopefully, it is helpful for someone who like step by step method.
My solution is for the UK though. How are we doing? Please help us improve Stack Overflow. Keep in mind that booting from an MS-DOS diskette is not going to allow you to have access to the files used with other operating systems. However, if you're erasing everything and starting over, this would allow you to delete all pre-existing information and start over. Notice For some commands and options to work in the Windows 10 command line, you must run the command line as administrator.
Notice For some commands and options to work in the Windows Vista and 7 command line, you must run the command line as administrator.
0コメント