Windows set file date command line

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.

Читайте также:  Сетевой драйвер для windows 10 64 bit для ноутбука huawei

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.

How to get file’s last modified date on Windows command line?

I have been using the following command to get the file date. However, the fileDate variable has been returning blank value ever since we moved to a different server (Windows Server 2003).

Is there any other more reliable way to get the file date?

9 Answers 9

Change % to %% for use in batch file, for %

ta syntax enter call /?

0,10% – Ricky Supit Jan 25 ’10 at 19:33

ta or %a is – FistOfFury Jan 6 ’15 at 22:01

t is only precise to the minute. Powershell also gives seconds via the two part command: $myFileInfo = Get-Item C:\myPath\myFile.txt $myFileInfo.LastWriteTime – BeatriceThalo Dec 9 ’19 at 20:43

Useful reference to get file properties using a batch file, included is the last modified time:

To get the last modification date/time of a file in a locale-independent manner you could use the wmic command with the DataFile alias:

Regard that the full path to the file must be provided and that all path separators (backslashes \ ) must be doubled herein.

This returns a standardised date/time value like this (meaning 12 th of August 2019, 13:00:00, UTC + 120′):

To capture the date/time value use for /F , then you can assign it to a variable using set :

The second for /F loop avoids artefacts (like orphaned carriage-return characters) from conversion of the Unicode output of wmic to ASCII/ANSI text by the first for /F loop (see also this answer).

You can then use sub-string expansion to extract the pure date or the time from this:

Читайте также:  Zadig windows 10 ошибка

To get the creation date/time or the last access date/time, just replace the property LastModified by CreationDate or LastAccessed , respectively. To get information about a directory rather than a file, use the alias FSDir instead of DataFile .

For specifying file (or directory) paths/names containing both , and ) , which are usually not accepted by wmic , take a look at this question.

Check out also this post as well as this one about how to get file and directory date/time stamps.

How to set date from cmd after retrieving it

I have the following code

So how can i get the date in the format dd/mm/yy

9 Answers 9

You can get the date format of dd/mm/yy by using wmic command. This command allows you to get current date without getting affected by regional settings.

You can save it as date.bat and run this batch file by executing the following in command prompt:

Hope this helps.

Two more ways that do not depend on the time settings (both taken from :How get data/time independent from localization:).And both also get the day of the week and none of them requires admin permissions!:

1.MAKECAB — will work on EVERY windows system (fast but creates a small temp file ) (the foxidrive script):

2. ROBOCOPY — it’s not native command for windows xp and win 2003 but can be downloaded from microsoft site .But is built-in in everything from Vista and above:

And three more ways that uses other windows script languages.They will give you more flexibility e.g. you can get week of the year, time in milliseconds and so on.

3.JSCRIPT/BATCH hybrid (need to be saved as .bat ).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):

4.VSCRIPT/BATCH hybrid (Is it possible to embed and execute VBScript within a batch file without using a temporary file?) same case as jscript , but hybridization is not so perfect:

5.POWERSHELL — can be installed on every machine that has .net — download from Microsoft (v1 , v2 , v3 (only for win7 and above)).Installed by default on everything form Win7/Win2008 and above :

Batch command date and time in file name

I am compressing files using WinZip on the command line. Since we archive on a daily basis, I am trying to add date and time to these files so that a new one is auto generated every time.

I use the following to generate a file name. Copy paste it to your command line and you should see a filename with a Date and Time component.

However, my issue is AM vs PM. The AM time stamp gives me time 9 (with a leading blank space) vs. 10 naturally taking up the two spaces.

I guess my issue will extend to the first nine days, first 9 months, etc. as well.

How do I fix this so that leading zeroes are included instead of leading blank spaces so I get Archive_20111011_093609.zip ?

15 Answers 15

It will give you (independent of locale settings!):

From here, it is easy:

For Logan’s request for the same outputformat for the «date-time modified» of a file:

It is a bit more complicated, because it works only with full paths, wmic expects the backslashes to be doubled and the = has to be escaped (the first one. The second one is protected by surrounding quotes).

Extract the hour, look for a leading space, if found replace with a zero;

You should search; you can simply replace all spaces with zero set hr=%hr: =0% – jeb Oct 11 ’11 at 14:16

Then use %hr% inside whatever string you are formatting to always get a two-digit hour.

(Jeb’s comment under the most popular answer worked the best for me and is the simplest. I repost it here to make it more obvious for future users.)

Читайте также:  Firefox обновление mac os

0,2: =0% would have been nice. :^( – Michael Feb 28 ’14 at 14:54

As Vicky already pointed out, %DATE% and %TIME% return the current date and time using the short date and time formats that are fully (endlessly) customizable.

One user may configure its system to return Fri040811 08.03PM while another user may choose 08/04/2011 20:30.

It’s a complete nightmare for a BAT programmer.

Changing the format to a firm format may fix the problem, provided you restore back the previous format before leaving the BAT file. But it may be subject to nasty race conditions and complicate recovery in cancelled BAT files.

Fortunately, there is an alternative.

You may use WMIC, instead. WMIC Path Win32_LocalTime Get Day,Hour,Minute,Month,Second,Year /Format:table returns the date and time in a invariable way. Very convenient to directly parse it with a FOR /F command.

So, putting the pieces together, try this as a starting point.

I found the best solution for me, after reading all your answers:

If AM I get 20160915_ 150101 (with a leading space and time).

If PM I get 20160915_2150101 .

21,2% set d=%d: =0% echo hello>»Archive_%d%» – Nesar Mar 12 ’18 at 17:02

You can add leading zeroes to a variable (value up to 99) like this in batch: IF 1%Var% LSS 100 SET Var=0%Var%

So you’d need to parse your date and time components out into separate variables, treat them all like this, then concatenate them back together to create the file name.

However, your underlying method for parsing date and time is dependent on system locale settings. If you’re happy for your code not to be portable to other machines, that’s probably fine, but if you expect it to work in different international contexts then you’ll need a different approach, for example by reading out the registry settings:

(That last one controls whether there is a leading zero on times, but not dates as far as I know).

This takes MB5L.txt and compresses it to ziptest20120204.zip if run on 4 Feb 2012

From the answer above, I have made a ready-to-use function.

Validated with french local settings.

As others have already pointed out, the date and time formats of %DATE% and %TIME% (as well as date /T and time /T ) are locale-dependent, so extracting the current date and time is always a nightmare, and it is impossible to get a solution that works with all possible formats since there are hardly any format limitations.

But there is another problem with a code like the following one (let us assume a date format like MM/DD/YYYY and a 12 h time format like h:mm:ss.ff ap where ap is either AM or PM and ff are fractional seconds):

Each instance of %DATE% and %TIME% returns the date or time value present at the time of its expansion, therefore the first %DATE% or %TIME% expression might return a different value than the following ones (you can prove that when echoing a long string containing a huge amount of such, preferrably %TIME% , expressions).

You could improve the aforementioned code to hold a single instance of %DATE% and %TIME% like this:

But still, the returned values in %DATE% and %TIME% could reflect different days when executed at midnight.

The only way to have the same day in %CURRDATE% and %CURRTIME% is this:

Of course the occurrence of the described problem is quite improbable, but at one point it will happen and cause strange unexplainable failures.

The described problem cannot occur with the approaches based on the wmic command as described in the answer by user Stephan and in the answer by user PA., so I strongly recommend to go for one of them. The only disadvantage of wmic is that it is way slower.

Оцените статью