'Get date and time on the same line

I can use date /t and time /t to get the date and time, but it doesn't display it next to each other. I wanted to make it do that and display like this:

Current Date & Time
-------------------

11/27/2013 10:43:05 AM


Solution 1:[1]

Try the following (PowerShell):

Get-Date -Format G

27.11.2013 17:10:23

The format is defined with the system's regional settings, so for me this is what I get, but if you're regional date/time format is what you want, it should show up like you want.

(Get-Date).ToString()

would probably also work.

UPDATE:

"Date and time is: $((Get-Date).ToString())"

Solution 2:[2]

In PowerShell this is trivial:

(Get-Date).ToString('MM/dd/yyyy hh:mm:ss tt')

In cmd it's somewhat complicated:

rem Get the date and time in a locale-agnostic way
for /f %%x in ('wmic path win32_localtime get /format:list ^| findstr "="') do set %%x
rem Leading zeroes for everything that could be only one digit
set Month=0%Month%
set Day=0%Day%
rem Hours need special attention if one wants 12-hour time (who wants that?)
if %Hour% GEQ 12 (set AMPM=PM) else (set AMPM=AM)
set /a Hour=Hour %% 12
if %Hour%==0 (set Hour=12)
set Hour=0%Hour%
set Minute=0%Minute%
set Second=0%Second%
set Month=%Month:~-2%
set Day=%Day:~-2%
set Hour=%Hour:~-2%
set Minute=%Minute:~-2%
set Second=%Second:~-2%
rem Now you can just create your output string
echo %Month%/%Day%/%Year% %Hour%:%Minute%:%Second% %AMPM%

Note that lots of code is wasted on supporting that weird date and time format. And leading zeroes if necessary. Note also that this code works regardless of the user's regional and language settings. It does not depend on the user's date format, for example (which for me is ISO 8601 anyway).

Solution 3:[3]

I am no wizard with cmd.exe, but this works. There may be an easier way!

@echo off
setlocal enableextensions
for /f "tokens=*" %%a in ('date /t') do (
   set d=%%a
)
for /f "tokens=*" %%a in ('time /t') do (
   set t=%%a
)
REM Add those puppies together
set "dt=%d% %t%"
echo %dt%
27/11/2013  16:04

Solution 4:[4]

You can use as well the .NET DateTime in powershell:

Write-Host "Current datetime is $([System.Datetime]::Now.ToString("dd/MM/yy HH:mm:ss"))"

Solution 5:[5]

For CMD, Why not the simple:

C:\temp\test>ECHO %DATE% %TIME%
Mon 06/24/2019 18:00:17.63

If you want to strip off the Day at the beginning, here's a condensed version of what Joey did, using substrings.

for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set timestamp=%dt:~0,4%/%dt:~4,2%/%dt:~6,2% %dt:~8,2%:%dt:~10,2%:%dt:~12,2%
ECHO %timestamp%

2019/06/24 17:56:43

2019/06/24 17:56:43

Solution 6:[6]

thank you all i was looking for inline date and time column string.

#date time sample Select-Object @{ l='DateTime'; e={ (Get-Date).tostring("yyyyMMddHHmm")} }

here is the full script below. for remote query

do {
$MHEdate = (Get-Date).tostring("yyyyMMddHHmmss") # example output 20161122. 
$OutputDir = "C:\tools\pshell\LOG\"
$logoname = 'getcoonections-filter-' + $MHEdate1 + '.csv'
$OutputFile1 = Join-Path $OutputDir $logoname

Invoke-Command -ComputerName ServerName {

#output to csv 
get-nettcpconnection | select local*,remote*,state,@{Name="Process";Expression={(Get-Process -Id $_.OwningProcess).ProcessName}},@{Name="ProcessID";Expression={(Get-Process -Id $_.OwningProcess).Id}}| Where-Object {($_.RemoteAddress -notcontains '0.0.0.0') -and ($_.RemoteAddress -notcontains '127.0.0.1') -and ($_.RemoteAddress -notcontains '::') -and ($_.RemoteAddress -notcontains '::1')} #|Where-Object{ $_.remoteaddress -Contains '168.135.115.248'} 

 # start-sleep -Seconds 15
 
 }| Select-Object @{ l='DateTime'; e={ (Get-Date).tostring("yyyyMMddHHmm")} },PSComputerName,LocalAddress,LocalPort,RemoteAddress,RemotePort,State,Process,ProcessID | Export-Csv -Path $OutputFile1 -NoTypeInformation -Append
 #Import-Csv $OutputFile1 |Out-GridView

 write-host -ForegroundColor Green $Time "query output file is ---> " $OutputFile1
 Start-Sleep 120
}until($infinity)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Mortensen
Solution 2
Solution 3 Bill_Stewart
Solution 4 Alexandru-Codrin Panaite
Solution 5 Mike Schlueter
Solution 6 HandyManny