How do I get the current day month and year from inside a Windows cmd script? I need to get each value into a separate variable.
相关问题
- Date with SimpleDateFormat in Java
- Does specifying the encoding in javac yield the sa
- String Manipulation with case sensitivity
- VBA local timezone adjustment
- mv wrapped inside an if in shell scripting
相关文章
- 在vscode如何用code runner打开独立的控制台窗口,以及设置好调试模式时窗口的编码?
- Is there a non-java, cross platform way to launch
- MYSQL: How can I find 'last monday's date&
- Calculate number of working days in a month [dupli
- Get file created date in node
- Temporal Extraction (i.e. Extract date/time entiti
- Postgres String to Date EXAMPLE 10Apr77 to 10/04/1
- Command line escaping single quote for PowerShell
I think that Andrei Coscodan answer is the best when you can't make many assumptions. But sometimes having a one-liner is nice if you can make some some assumptions. This solution assumes that 'date \t' will return one of two formats. On WindowsXP 'date /t 'returns "11/23/2011", but on Windows7 it returns "Wed 11/23/2011".
Thanks to Andrei Consodan answer to help me with this one-line solution.
LANGUAGE INDEPENDENCY:
The Andrei Coscodan solution is language dependent, so a way to try to fix it is to reserve all the tags for each field: year, month and day on target languages. Consider Portugese and English, after the parsing do a final set as:
Look for the year setting, I used both tags from English and Portuguese, it worked for me in Brazil where we have these two languages as the most common in Windows instalations. I expect this will work also for some languages with Latin origin like as French, Spanish, and so on.
Well, the full script could be:
I hope this helps someone that deal with diferent languages.
Extract Day, Month and Year
The highest voted function and the accepted one do NOT work locale-independently since the DATE command is subject to localization too. For example (the accepted one): In English you have YYYY for year and in Holland it is JJJJ. So this is a no-go. The following script takes the users' localization from the registry, which is locale-independent.
Extract only the Year
For a script I wrote I wanted only to extract the year (locale-independent) so I came up with this oneliner as I couldn't find any solution. It uses the 'DATE' var, multiple delimiters and checks for a number greater than 31. That then will be the current year. It's low on resources in contrast to some of the other solutions.
To get the year, month, and day you can use the
%date%
environment variable and the:~
operator.%date%
expands to something like Thu 08/12/2010 and:~
allows you to pick up specific characters out of a variable:Use
%time%
in similar fashion to get what you need from the current time.set /?
will give you more information on using special operators with variables.The following batch code returns the components of the current date in a locale-independent manner and stores day, month and year in the variables
CurrDay
,CurrMonth
andCurrYear
, respectively:There are two nested
for /F
loops to work around an issue with thewmic
command, whose output is in unicode format; using a single loop results in additional carriage-return characters which impacts proper variable expansion.Since day and month may also consist of a single digit only, I prepended a leading zero
0
in the loop construct. Afterwards, the values are trimmed to always consist of two digits.A variant of script that works locale-independently. Put it in a text file with .cmd extension and run.
The variables %dd%, %mm% and %yy% will keep the day('DD' format), the month('MM' format) and the year('YYYY' format) respectively.