programing

동일한 라인에서 날짜 및 시간 가져오기

codeshow 2023. 10. 19. 22:51
반응형

동일한 라인에서 날짜 및 시간 가져오기

사용할 수 있습니다date /t그리고.time /t날짜와 시간을 알 수 있지만 서로 옆에 표시되지 않습니다.그렇게 해서 이렇게 보여주고 싶었습니다.

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

11/27/2013 10:43:05 AM

다음(PowerShell)을 사용해 보십시오.

Get-Date -Format G

27.11.2013 17:10:23

포맷은 시스템의 지역별 설정으로 정의되기 때문에, 저는 이것이 제가 얻는 것이지만, 지역별 날짜/시간 포맷이라면 원하는 대로 표시되어야 합니다.

(Get-Date).ToString()

아마 효과가 있을 겁니다

업데이트:

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

PowerShell에서 이는 사소한 것입니다.

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

cmd좀 복잡하네요

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%

이상한 날짜와 시간 형식을 지원하는 데 많은 코드가 낭비됩니다.필요하다면 0을 유도하는 것도 좋습니다.또한 이 코드는 사용자의 지역 및 언어 설정에 관계없이 작동합니다.예를 들어 사용자의 날짜 형식에 따라 달라지지 않습니다(저는 ISO 8601입니다).

cmd.exe를 가진 마법사는 아니지만 작동합니다.더 쉬운 방법이 있을지도 몰라요!

@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

CMD의 경우 간단한 이유:

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

처음에 그 날을 벗겨내고 싶다면, 여기 조이가 서브스트링을 사용해서 한 것의 압축된 버전이 있습니다.

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

를 사용할 수도 있습니다.NET DateTime in powershell:

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

인라인 날짜와 시간 열 문자열을 찾고 있던 모든 것에 감사드립니다.

#날짜 시간 샘플 Select-Object @{ l='Date시간'; e={ (Get-Date).to 문자열("yyyyMDDHmm")} }

아래의 전체 스크립트입니다.원격 쿼리의 경우

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)

언급URL : https://stackoverflow.com/questions/20246889/get-date-and-time-on-the-same-line

반응형