Windows服務器當前時間和本地時間差8個小時的解決辦法

當使用谷歌雲GCP的windows時候,可能服務器的時間和本地時間總是差8個小時,然而更改時區,卻提示沒有權限,因為登錄用戶不是administrator,下面是解決辦法

================================

2024.6.5更新新的方法,調整時間的同時會禁用掉自動更新時間,這樣防止系統會自動更新時間而導致時間又變了。

這樣就可以不用設置開機重啟了,只需要運行一次即可。

同時代碼後面還更新了恢復自動更新時間的代碼,這樣後續需要調整,也可以再恢復。

下面是更新時間並禁用自動更新時間的代碼

@echo off
setlocal

:: 獲取當前日期和時間
for /f "tokens=2 delims==" %%a in ('wmic os get localdatetime /value') do set datetime=%%a
set year=%datetime:~0,4%
set month=%datetime:~4,2%
set day=%datetime:~6,2%
set hours=%datetime:~8,2%
set minutes=%datetime:~10,2%
set seconds=%datetime:~12,2%

:: 計算新的小時數
set /a "newhours=hours + 8"

:: 檢查是否跨過午夜
if %newhours% geq 24 (
    set /a "newhours-=24"
    set /a "day+=1"
)

:: 檢查月份天數,以確定是否需要進位到下一個月
call :AdjustDate %year% %month% %day%

:: 格式化時間以確保符合time命令的要求
if %newhours% lss 10 set newhours=0%newhours%

:: 設置系統時間和日期,這裡假設你的系統使用YYYY-MM-DD格式
cmd /c date %year%-%month%-%day%
cmd /c time %newhours%:%minutes%:%seconds%

:: 禁用自動時間同步
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient" /v Enabled /t REG_DWORD /d 0 /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config" /v UpdateInterval /t REG_DWORD /d 0 /f

goto :eof

:AdjustDate
setlocal enableextensions enabledelayedexpansion
set /a "year=%1", "month=%2", "day=%3"
set "monthdays=31"

:: 調整2月的天數
if "%month%"=="02" (
    set /a "leapyear=year %% 4"
    if !leapyear! equ 0 (
        if "%year% %% 100" neq "0" or "%year% %% 400" eq "0" set "monthdays=29" else set "monthdays=28"
    ) else (
        set "monthdays=28"
    )
)

:: 調整小月的天數
if "%month%"=="04" set "monthdays=30"
if "%month%"=="06" set "monthdays=30"
if "%month%"=="09" set "monthdays=30"
if "%month%"=="11" set "monthdays=30"

if %day% gtr %monthdays% (
    set /a "day=1"
    set /a "month+=1"
    if %month% gtr 12 (
        set /a "month=1"
        set /a "year+=1"
    )
)
endlocal & (
    set "year=%year%"
    set "month=%month%"
    set "day=%day%"
)
goto :eof

下面代碼是恢復自動更新時間

@echo off
setlocal
chcp 65001

:: 啟用NtpClient以自動同步時間
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient" /v Enabled /t REG_DWORD /d 1 /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config" /v UpdateInterval /t REG_DWORD /d 3600 /f

:: 啟動Windows Time服務
sc start w32time
sc config w32time start= auto

echo 自動時間同步已恢復。
pause

================================

直接在任意位置創建一個bat文件,比如:UpdateTime.bat

將下面的代碼放入bat中

@echo off
setlocal

:: 獲取當前日期和時間
for /f "tokens=2 delims==" %%a in ('wmic os get localdatetime /value') do set datetime=%%a
set year=%datetime:~0,4%
set month=%datetime:~4,2%
set day=%datetime:~6,2%
set hours=%datetime:~8,2%
set minutes=%datetime:~10,2%
set seconds=%datetime:~12,2%

:: 計算新的小時數
set /a "newhours=hours + 8"

:: 檢查是否跨過午夜
if %newhours% geq 24 (
    set /a "newhours-=24"
    set /a "day+=1"
)

:: 檢查月份天數,以確定是否需要進位到下一個月
call :AdjustDate %year% %month% %day%

:: 格式化時間以確保符合time命令的要求
if %newhours% lss 10 set newhours=0%newhours%

:: 設置系統時間和日期,這裡假設你的系統使用YYYY-MM-DD格式
cmd /c date %year%-%month%-%day%
cmd /c time %newhours%:%minutes%:%seconds%
goto :eof

:AdjustDate
setlocal enableextensions enabledelayedexpansion
set /a "year=%1", "month=%2", "day=%3"
set "monthdays=31"

:: 調整2月的天數
if "%month%"=="02" (
    set /a "leapyear=year %% 4"
    if !leapyear! equ 0 (
        if "%year% %% 100" neq "0" or "%year% %% 400" eq "0" set "monthdays=29" else set "monthdays=28"
    ) else (
        set "monthdays=28"
    )
)

:: 調整小月的天數
if "%month%"=="04" set "monthdays=30"
if "%month%"=="06" set "monthdays=30"
if "%month%"=="09" set "monthdays=30"
if "%month%"=="11" set "monthdays=30"

if %day% gtr %monthdays% (
    set /a "day=1"
    set /a "month+=1"
    if %month% gtr 12 (
        set /a "month=1"
        set /a "year+=1"
    )
)
endlocal & (
    set "year=%year%"
    set "month=%month%"
    set "day=%day%"
)
goto :eof

重要說明

  • 在使用該腳本之前,請確保了解你的Windows系統的日期和時間格式。你可以通過在命令提示符下運行date /ttime /t來查看當前系統的日期和時間格式。
  • 如果你的系統使用的是其他日期格式,如MM-DD-YYYYDD-MM-YYYY,請在腳本中相應地調整cmd /c date命令行的日期部分。

然後使用管理員權限運行該bat文件,即可將服務器時間直接加8

還可以將該bat文件設置為開機啟動,這樣無需每次手動運行

但是有個問題,該bat需要管理員權限才能夠正常運行,所以一般的開機啟動不行。

這裡可以使用任務計劃程序

這是推薦的方法,因為它允許你指定任務以管理員權限執行,即使你登錄的用戶不是管理員。

  1. 打開任務計劃程序:在開始菜單搜索“任務計劃程序”並打開。
  2. 創建新任務
    • 在任務計劃程序的右側,點擊“創建基本任務…”來啟動嚮導。
    • 輸入任務的名稱和描述。
    • 選擇“當計算機啟動時”作為觸發器。
    • 在“操作”步驟中,選擇“啟動程序”,然後瀏覽到你的批處理文件。
    • 在設置選項時,確保勾選“使用最高權限運行”。
  3. 保存並退出:完成嚮導並確保任務已啟用。

然後可以嘗試重啟下服務器,進行測試。

到此為止。

發布者:彬彬筆記,轉載請註明出處:https://www.binbinbiji.com/zh-hant/windows/3389.html

(0)
彬彬筆記彬彬筆記
上一篇 2024年5月12日
下一篇 2024年6月7日

相關推薦

發表回復

登錄後才能評論
蜀ICP備14017386號-13