Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I retrieve the seconds part of TIME in cmd?

Tags:

batch-file

cmd

I'm trying to write a cmd script that gets the current date and time and formats it into a way that sqlserver can input it as a datetime.

So far, I have:

@echo off for /F "tokens=1-4 delims=/ " %%i in ('date /t') do ( set dow=%%i set mon=%%j set day=%%k set yr=%%l set mydate=%%j/%%k/%%l ) 

This prints out 10/22/2010

I have not been able to figure out how to get the time into a usable format. I tried working with time /t, but it only gives the hours and minutes, and I need the seconds also.

like image 897
chama Avatar asked Nov 22 '10 17:11

chama


People also ask

How do I show time stamp in CMD?

You can use $T for time and $D for date, as well as several other expansions. See also: https://ss64.com/nt/prompt.html. https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/prompt.

What Is REM command in CMD?

REM [comment] Purpose: Provides a way to insert remarks (that will not be acted on) into a batch file. Discussion. During execution of a batch file, DOS will display (but not act on) comments which are entered on the line after the REM command. You cannot use separators in the comment except the space, tab, and comma.

What is the command @echo off?

echo off. When echo is turned off, the command prompt doesn't appear in the Command Prompt window. To display the command prompt again, type echo on. To prevent all commands in a batch file (including the echo off command) from displaying on the screen, on the first line of the batch file type: @echo off.

What is the time CMD?

In computing, TIME is a command in DEC RT-11, DOS, IBM OS/2, Microsoft Windows and a number of other operating systems that is used to display and set the current system time. It is included in command-line interpreters (shells) such as COMMAND.COM , cmd.exe , 4DOS, 4OS2 and 4NT.


1 Answers

Use the %TIME% pseudo-variable.

You can also use %DATE% in above script.

Both have the same limitations, in that they depend on your locale. The code is not portable and you'll be up for surprises in other environments. But that's how date and time in cmd works.

like image 141
Joey Avatar answered Oct 24 '22 20:10

Joey