- How to get date / time in batch file
- How to get date and time in a batch file
- Get date from command line
- How to get only the date in MM/DD/YYYY format?
- Get time from command prompt
- Get date and time
- Find command date windows
- Syntax
- Parameters
- Remarks
- Examples
- Дата date
- Синтаксис Syntax
- Параметры Parameters
- Примеры Examples
- Find out Windows Installation Date using various methods
- Windows Installation Date
- 1] Windows folder Properties
- 2] Check the Windows Registry
- 3] Use the systeminfo command-line tool
- 4] Use a PowerShell command
- Find files on Windows modified after a given date using the command line
- 7 Answers 7
How to get date / time in batch file
This post explains how to get current date and time from command prompt or in a batch file.
How to get date and time in a batch file
Below is a sample batch script which gets current date and time
Datetime.cmd
When we run the above batch file
Get date from command line
To print today’s date on the command prompt, we can run date /t .
Just running date without any arguments prints the current date and then prompts to enter a new date if the user wants to reset it.
In addition to date command, we also have an environment variable using which we can find today’s date.
How to get only the date in MM/DD/YYYY format?
You may want to exclude the day (like ‘Sun’ in the above example) and print only the date in MM/DD/YYYY format. The below command works for the same.
Get time from command prompt
Similar to date command, we have the command time which lets us find the current system time. Some examples below.
As you can see, the command prints the time in different formats. It prints in 12 hour format when /t is added and in 24 hours format without /t
We can also get the current time from environment variables.
Get date and time
Sir I want to get all outdated drivers in our pc through command prompt please help and
reply
Thanks
That last part is so helpful and outstanding! Thank you so much!! 😀
-Matthew
I want the last week date from the current date :-This is the script,I am using for the getting the current date.
for /F “tokens=2” %i in (‘date /t’) do echo %i
05/14/2015
these comments were helpful but how do you make a real time updating clock in 12 hour format in a batch file?
Create a file called realtimeClock.bat.
This is the contents of realtimeClock.bat:
——
@echo off
:getTime
echo The current time is %time%
cls
goto :getTime
——
Run, and enjoy.
Excellent guideline. If the hour (time) is less than 10 then %time% return a space before the hour, so I prefer to use the ‘time /t’ approach.
You can solve that by:
echo %TIME: =0%
(there’s a space between : and =). That will replace the space with a 0
Let’s say you wanted to use the variable to create a filename or log based on the current time:
rem Extract the hour and minute from the time
set TM=%TIME:
3,2%
rem Zero-pad the hour if it is before 10am
set TM=%TM: =0%
echo %TM%
output is:
0803
for 8:03am
I need to get the files based on current date. How ya the script look like. Can someone assist me
how to do this with date and time of a file ?
Why doesn’t it work with “ftime, fdate” ?
please any hint how to do this would be great !
Reply to markus’ question: How to do this with date and time of a file ?
To read the date+time of a file, call DIR in a FOR loop, like so:
FOR /F “tokens=1,2,3,4,*” %%a in (‘DIR “filename.ext”/4 ^| find “/”‘) do set “filedatetime=%%a %%b %%c” & set “filesize=%%d” & set “filename=%%e” & REM Do whatever you want here
Note: There is an apostrophe (single-quote) between the double-quote and the right-paren.
You can, of course, use any switches you want in the DIR command to refine your selection criteria. Type DIR /? for more info.
The FIND command filters the output of DIR to eliminate the header and footer. (DIR /B only lists the filenames, not the dates.)
“tokens=1,2,3,4,*” parses the output into separate variables. The asterisk at the end puts the entire filename (including spaces) into the fifth variable.
how to output date /T and time /T?
like sun 1/1/2017 3:52 PM
Hello, I would need a batch file, with does the Reset function in Date and Time/Change Date and Time/change calendar settings/Reset.
Is it possible to get the date and then use it as an input string in a for loop so that the day can increment
I need a .bat script to change the system date one day ahead, (change it to tomorrow’s date). That’s it.
hello
I need to run a file in a certain date without using wndows Task Scheduler.
how can I do that using batch file?
please help me.
hello
I need to run a file in a certain date without using windows Task Scheduler.
how can I do that using batch file?
please help me.
please answer me through my G-mail
thanks alot
All of the above scripts which call both time and date (or %time% and %date%) suffer from one minor problem: time and date are called separately, so if the script runs just before midnight (a very narrow window, to be sure – at most a few milliseconds), the date could roll over between the two calls and your result would be 24 hours early (i.e. just after midnight on the date that the command started).
Here are 2 solutions:
1.
for /f “tokens=2 delims==” %%I in (‘wmic os get localdatetime /format:list’) do set datetime=%%I
set datetime=%datetime:
8,6%
wmic gets the date + time in an atomic operation, so no rollover is possible.
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.
2.
:timeloop
set mydate=%date%
set mytime=%time%
if mydate NEQ %date% goto :timeloop
If the date rolls over between the two calls to %date%, just go back and try again.
Find command date windows
Searches for a string of text in a file or files, and displays lines of text that contain the specified string.
Syntax
Parameters
Parameter | Description |
---|---|
/v | Displays all lines that don’t contain the specified . |
/c | Counts the lines that contain the specified and displays the total. |
/n | Precedes each line with the file’s line number. |
/i | Specifies that the search is not case-sensitive. |
[/off[line]] | Doesn’t skip files that have the offline attribute set. |
Required. Specifies the group of characters (enclosed in quotation marks) that you want to search for. | |
[ :][ |
]
Remarks
If you don’t use /i, this command searches for exactly what you specify for string. For example, this command treats the characters a and A differently. If you use /i, however, the search becomes non-case-sensitive, and it treats a and A as the same character.
If the string you want to search for contains quotation marks, you must use double quotation marks for each quotation mark contained within the string (for example, «»This string contains quotation marks»»).
If you omit a file name, this command acts as a filter, taking input from the standard input source (usually the keyboard, a pipe (|), or a redirected file) and then displays any lines that contain string.
You can type parameters and command-line options for the find command in any order.
You can’t use wildcards (* and ?) in file names or extensions that you specify while using this command. To search for a string in a set of files that you specify with wildcards, you can use this command within a for command.
If you use /c and /v in the same command line, this command displays a count of the lines that don’t contain the specified string. If you specify /c and /n in the same command line, find ignores /n.
This command doesn’t recognize carriage returns. When you use this command to search for text in a file that includes carriage returns, you must limit the search string to text that can be found between carriage returns (that is, a string that is not likely to be interrupted by a carriage return). For example, this command doesn’t report a match for the string tax file if a carriage return occurs between the words tax and file.
Examples
To display all lines from pencil.ad that contain the string pencil sharpener, type:
To find the text, «The scientists labeled their paper for discussion only. It is not a final report.» in the report.doc file, type:
To search for a set of files, you can use the find command within the for command. To search the current directory for files that have the extension .bat and that contain the string PROMPT, type:
To search your hard disk to find and display the file names on drive C that contain the string CPU, use the pipe (|) to direct the output of the dir command to the find command as follows:
Because find searches are case-sensitive and dir produces uppercase output, you must either type the string CPU in uppercase letters or use the /i command-line option with find.
Дата date
Отображает или задает системную дату. Displays or sets the system date. Если используется без параметров, Дата отображает текущий параметр системной даты и предлагает ввести новую дату. If used without parameters, date displays the current system date setting and prompts you to enter a new date.
Чтобы использовать эту команду, необходимо быть администратором. You must be an administrator to use this command.
Синтаксис Syntax
Параметры Parameters
Параметр Parameter | Описание Description |
---|---|
Устанавливает указанную дату, где Month — месяц (одна или две цифры, включая значения от 1 до 12), день — день (одна или две цифры, включая значения от 1 до 31), а year — год (две или четыре цифры, включая значения от 00 до 99 или от 1980 до 2099). Sets the date specified, where month is the month (one or two digits, including values 1 through 12), day is the day (one or two digits, including values 1 through 31), and year is the year (two or four digits, including the values 00 through 99 or 1980 through 2099). Необходимо разделить значения для месяца, дня и года с точками (.), дефисами (-) или знаками косой черты (/). You must separate values for month, day, and year with periods (.), hyphens (-), or slash marks (/). |
Примечание. Имейте в виду, что если для представления года используется 2 цифры, то значения 80-99 соответствуют 1980 – 1999. Note: Be aware that if you use 2 digits to represent the year, the values 80-99 correspond to 1980 through 1999.
Примеры Examples
Если расширения команд включены, для вывода текущей системной даты введите: If command extensions are enabled, to display the current system date, type:
Чтобы изменить текущую системную дату на 3 августа 2007, можно ввести любой из следующих элементов: To change the current system date to August 3, 2007, you can type any of the following:
Чтобы отобразить текущую системную дату, после чего появится запрос на ввод новой даты, введите: To display the current system date, followed by a prompt to enter a new date, type:
Для сохранения текущей даты и возврата в командную строку нажмите клавишу Ввод. To keep the current date and return to the command prompt, press ENTER. Чтобы изменить текущую дату, введите новую дату и нажмите клавишу Ввод. To change the current date, type the new date and then press ENTER.
Find out Windows Installation Date using various methods
There may come a time when you may want to or need to know your Windows installation date or the date and time when your Windows 10 OS was installed on your computer. There are several ways to find this out, which we will discuss in this post.
Windows Installation Date
To find the installation date of your Windows 10 OS:
- You may see the Windows folder Properties
- Check the Windows Registry
- Use the systeminfo command-line tool
- Use a PowerShell command.
1] Windows folder Properties
The easiest way to find out when your Windows OS was installed, would be to right-click the Windows folder, select Properties and see the entry in front of Created under the General tab. Here you will see the time and date.
But if you have upgraded your Windows version to a higher version, you will see the date when the earlier version was installed and not when the latest version was installed or upgraded. In my case, it shows the day I first installed Windows 8 – and not the Windows 8.1 date.
2] Check the Windows Registry
The Windows Installation Date is also stored in the Windows Registry in the following key, as UNIX time, ie as a 32-bit value displaying the time in the number of seconds since 1st Jan 1970.
My Windows was installed 1382359164 seconds since 1st Jan 1970, so you need to do some calculations to arrive at the figure.
3] Use the systeminfo command-line tool
The easiest and the most convenient way of finding the Windows Installation Date would be to use the systeminfo tool, which we have already talked about. To use this built-in tool, open a command prompt window, type the following, and hit Enter:
You will be able to see the Original Install Date.
4] Use a PowerShell command
The Windows install date is also stored in the property InstallDate of the WMI class Win32_OperatingSystem. You can use PowerShell to get the date and time of installation.
To find out the date, open Windows PowerShell as Administrator, change the path to C drive, type the following, and hit Enter:
The date and time your Windows was installed will be displayed!
Find files on Windows modified after a given date using the command line
I need to search a file on my disk modified after a given date using the command line.
7 Answers 7
You can use PowerShell to do this. Try:
The forfiles command works without resorting to PowerShell. The article is here:
Microsoft Technet documentation: forfiles
For the example above:
- /P The starting path to search
- /S Recurse into sub-directories
- /D Date to search, the «+» means «greater than» or «since»
I was after the size of the files changed and did not have PowerShell. I used the following, but the clue came from other posts:
I had the same problem, so I created a list using XCOPY and the modified-by date I was looking for, used a for loop to traverse the list, and added the date/time information I needed for each file to a log:
It resulted in something like the following, which is exactly what I wanted.
You can search files modified after a given date using XCOPY as in this example, looking for files since last day of 2018:
In this example, you are in the directory where your search begins.
C:\temp*.* is only a syntax requisite, and nothing will be copied there.
/D:12-31-2018 specifies the files modified date you are looking for, including the specified date.
/L: Shows the filenames, including drive and path, also makes XCOPY do not copy any file.
/S : Search in subdirectories.
If you decide to use PowerShell, this will also work with time and date ranges:
To use a programmatic date or a Locale agnostic date time:
Where date and time are entered in increasing time specificity order:
Year, Month, Day, Hour, Minute, Second
I had a similar challenge. I used forfiles , however I noticed that it gave >= rather than just >. The input date was a variable, and so I didn’t want to go through a bunch of hoops/loops to calculate date + 1 day (think about logic for last day of month or last day of year).
I created a function that ran forfiles for a particular date. That gives us all files with a modification date that is >= _date_ . And for each file I check if the modification date is _date_ , and print the output to a file if and only if it is not equal. That means the output file only has entries for files that are > _date_ .
I can then check for existence of the output file to determine if any files in the current directory are greater than the input date. Moreover, if I wanted to know which files are newer, I can view the contents of the output file.
Finally, my input was parsed from a file that used a date format of MM/DD/YYYY, meaning it would be 07/01/2019 for July first. However, the date from FORFILES does not use leading zeros so I need to translate by dropping leading zeros.
My first attempt was to use /A thinking it drops the leading zeros. It doesn’t it reads the value as octal — don’t repeat the mistake of my first attempt. So I just created a little helper function :getNum that drops leading zeros.