Ice Ice 1, 3 3 gold badges 12 12 silver badges 27 27 bronze badges. Kelly Kelly 1 1 gold badge 2 2 silver badges 8 8 bronze badges. Kees Kees 6 6 silver badges 3 3 bronze badges. This batch script will do exactly what the O. ECHO Generate a custom formatted timestamp string using date and time. ECHO Job is done. Press any key to exit. Works on Windows too.
I'll keep your solution around, I like it : — user Question, I just discovered that after midnight, the trailing "0" gets dropped in the output. Any idea as of why? But after 1 o'clock it works as expected and it will give output That should fix it, I hope : — user Thanks for the feedback user I have fixed the code. I have also modified the layout so that it is easier to read. Does it assume a particular regional setting? According to ss On my system the variables don't match my regional settings.
Case Case 21 2 2 bronze badges. Will Wu Will Wu 3 3 silver badges 15 15 bronze badges. MacGyver MacGyver You may use these Ahmed Ashour 4, 10 10 gold badges 31 31 silver badges 47 47 bronze badges. Khalid Hossain Khalid Hossain 55 2 2 bronze badges.
This answer only works if a specific date format is set in Windows. Therefore it won't work on most Windows systems world-wide. ArtK ArtK 19 2 2 bronze badges. And I don't even want to start the discussion on readability and standard expected code behaviour. Resetting the sTimeFormat time format is useless. Petr Matlas Petr Matlas 11 2 2 bronze badges.
Please note this doesn't meet the exact criteria laid down by the question. Christo Christo 1. The Overflow Blog. Learn more. Windows batch: formatted date into variable Ask Question. Asked 9 years, 7 months ago. Active 1 year, 8 months ago. Viewed k times. Improve this question. Peter Mortensen Maksym Polshcha Maksym Polshcha I'd advise not to use it. This link is helpful to understand the answers below.
Add a comment. Active Oldest Votes. Improve this answer. Joey Joey k 80 80 gold badges silver badges bronze badges. Note that if you run the above line multiple times you won't get a new date for each invocation, as it checks for the variable being undefined. I guess that's what user stumbled over. CharlesWood, at least I strive for robustness in my own batch files, although these days I often use PowerShell instead. How to get 2-digit left padded with zero month and day to date string?
Show 10 more comments. Anup Rav Anup Rav 4 4 silver badges 3 3 bronze badges. This is the closest answer to the asked question.
A simple one-liner was all I needed, not a program. The reason that the other responses look like a program is that this answer, although simple, will fail if your Windows regional settings are not configured in the way that this code expects i. I found that I needed to change this slightly on a windows 7 box.
The tokens needed to be incremented by one Now; Console. WriteLine dt. Mark Mark 2, 4 4 gold badges 23 23 silver badges 29 29 bronze badges. I really liked Joey's method, but I thought I'd expand upon it a bit.
Unfortunately there seems to be no way to tell the date or time commands what format one wants returned. It seems dependent on your regional settings for that specific windows installation. I fear you will end up with a protected mode shell running all the time. This would certainly not be my preference.
Your Batch file would essentially be a loop, looking if the date required has been achieved. If the value comparison if true, it would execute possibly a sub: and then return to the start of the loop.
Here are 2 solutions: 1. The second line formats datetime in the form I needed for what I used it for. You can modify this line to format however suits your needs. Great information here.
I am in need of a way to get the full 24 hr time displayed live. I am looking for a way to make that happen automatically without me hitting enter constantly. The steps in this tutorial can be used in a powerful way. The FOR command can be reused and changed to query to see if a file exists. If so, you can create the folder, etc. Your participation helps us to help others.
By visiting this site, users agree to our disclaimer.
0コメント