2013-11-14 51 views
1

我有一個VBScript將dateTimne寫入文件。VBS不正確的日期時間格式

Option Explicit 
Dim fso, path, file, recentDate, recentFile, objFileHandle 
Set fso = CreateObject("Scripting.FileSystemObject") 
Set recentFile = Nothing 
For Each file in fso.GetFolder("\\path\folder").Files 
    If (recentFile is Nothing) Then 
    Set recentFile = file 
    ElseIf (file.DateLastModified < recentFile.DateLastModified) Then 
    Set recentFile = file 
    End If 
Next 

Set objFileHandle = fso.OpenTextFile("\\path\folder\DateTime.Txt", 2, "True") 
objFileHandle.Write(FormatDateTime(recentFile.DateLastModified)) 
objFileHandle.Close 

當我在我的服務器上手動運行它,它給了我MM/DD/YYYY HH的美國格式:MM:SS AM例如,2013年11月14日上午09時20分56秒。

當我在我的筆記本電腦上運行它時,它會讓我回到我實際需要的英國格式 - dd/mm/yyyy hh:mm:ss AM例如14/11/2013 9:20:56 AM。

任何想法這是怎麼回事?

+1

請注意,「True」只是偶然的「True」(因爲任何非空字符串的計算結果都爲True) - 「False」也會起作用。使用實際的布爾值,而不是字符串。 – Tomalak

回答

2

試試這個:

Option Explicit 
Dim fso, path, file, recentDate, recentFile, objFileHandle 
Set fso = CreateObject("Scripting.FileSystemObject") 
Set recentFile = Nothing 
For Each file in fso.GetFolder("folder").Files 
    If (recentFile is Nothing) Then 
     Set recentFile = file 
    ElseIf (file.DateLastModified < recentFile.DateLastModified) Then 
     Set recentFile = file 
    End If 
Next 

' YYYY-MM-DD HH:MM:SS (24h ISO 8601 Format) 
' You can permutate parameters and delemiters the way you want. 

Dim thisday , thistime 
thisday = Date 
thistime = Time 

Set objFileHandle = fso.OpenTextFile("folder\DateTime.Txt", 2, "True") 
objFileHandle.Write(Year(thisday) & "-" & Month(thisday) & "-" & Day(thisday) &" "& Hour(thistime) & ":" & Minute(thistime) & ":" & Second(thistime)) 
objFileHandle.Close 

拷貝和過去到您的.vbs文件,並調整你想要什麼,如果你不與ISO 8601的工作標準

1

VBScript缺乏靈活的日期格式化功能,根據運行腳本的主機的當前語言環境輸出日期。

FormatDateTime() accepts a few constants修改其輸出,但不能傳遞自定義格式字符串。

這意味着您必須編寫自己的函數來生成與區域無關的格式。

在任何情況下:使用ISO 8601而不是任何瘋狂的月球格式,如mm/dd/yyyy

+0

我有框的管理權限 - 我應該改變本地? – Fearghal

+1

@Fearghal - 不,因爲你不知道哪些其他程序/人員依賴於當前的區域設置。 –

+0

不可以。因爲您確實不希望腳本產生不同的輸出,這取決於某些模糊的環境設置。以輸出穩定且可預測的方式編寫腳本,這意味着:使用適當的自定義格式功能。 – Tomalak

0

此Windows批處理腳本使用VBS常規給出可靠的日期變量。

您需要提取VBS腳本並在需要時進行修改。

:: date time using WSH/VBS 
    :: datetime.bat V4.2 
    :::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
    :: 
    :: This uses Windows Scripting Host to set variables to 
    :: the current date/time/day/day_number/week_of_year etc 
    :: for Win9x/ME/NT/W2K/XP/Vista/Win7/Win8 etc 
    :: Thanks go to Todd Vargo for his scripting 
    :: 
    :::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
    @echo off 
    set TmpFile="%temp%.\tmp.vbs" 
    echo> %TmpFile% n=Now 
    echo>>%TmpFile% With WScript 
    echo>>%TmpFile% .Echo "set m1=" + monthname(month(n), true) 
    echo>>%TmpFile% .Echo "set m2=" + monthname(month(n), false) 
    echo>>%TmpFile% .Echo "set woy=" + CStr(datepart("ww", n)) 
    echo>>%TmpFile% .Echo "set year=" + CStr(Year(n)) 
    echo>>%TmpFile% .Echo "set yr=" + Right(Year(n),2) 
    echo>>%TmpFile% .Echo "set month="+ Right(100+Month(n),2) 
    echo>>%TmpFile% .Echo "set day=" + Right(100+Day(n),2) 
    echo>>%TmpFile% .Echo "set hour=" + Right(100+Hour(n),2) 
    echo>>%TmpFile% .Echo "set min=" + Right(100+Minute(n),2) 
    echo>>%TmpFile% .Echo "set sec=" + Right(100+Second(n),2) 
    echo>>%TmpFile% .Echo "set dow=" + WeekDayName(Weekday(n),1) 
    echo>>%TmpFile% .Echo "set dow2=" + WeekDayName(Weekday(n)) 
    echo>>%TmpFile% .Echo "set iso=" + CStr(1 + Int(n-2) mod 7) 
    echo>>%TmpFile% .Echo "set iso2=" + CStr(Weekday(n,2)) 
    echo>>%TmpFile% End With 
    cscript //nologo "%temp%.\tmp.vbs" > "%temp%.\tmp.bat" 
    call "%temp%.\tmp.bat" 
    del "%temp%.\tmp.bat" 
    del %TmpFile% 
    set TmpFile= 
    set stamp=%year%-%month%-%day%.%hour%_%min%_%sec% 

    if not "%~1"=="" goto :EOF 

    echo The year is "%year%" or "%yr%" 
    echo The month is "%month%" "%m1%" "%m2%" 
    echo The day is "%day%" "%dow%" "%dow2%" 
    echo. 
    echo ISO8601 Day-Of-Week number is "%iso%" and week of year is: "%woy%" 

    echo. 
    echo The time in hh:mm:ss is "%hour%:%min%:%sec%" 
    echo The hour is "%hour%" 
    echo The minute is "%min%" 
    echo The second is "%sec%" 
    echo. 

    echo The date and time stamp is "%stamp%" 
    echo. 
    echo date A yyyymmdd "%year%%month%%day%" 
    echo date B mmddyyyy "%month%%day%%year%" 
    echo date C ddmmyyyy "%day%%month%%year%" 
    echo date D yymmdd "%yr%%month%%day%" 
    echo date E mmddyy "%month%%day%%yr%" 
    echo date F ddmmyy "%day%%month%%yr%" 
    pause 
    :: datetime.bat 
    :::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
2

的問題已經回答了,但我不知道爲什麼沒有人提出的區域設置爲正確的日期時間格式:

option explicit 

dim currentLocale 
currentLocale = GetLocale() 

SetLocale 2057  ' 2057 is the EN-GB locale 
msgbox now 
' Output: 15/11/2013 14:50:52 PM  
' Put back the original locale 
SetLocale currentLocale 

而對於不使用的創建日期 - 時間格式區域設置(根據運行腳本的計算機的區域和語言設置配置,有時不顯示AM/PM設置),您始終可以使用.NET stringbuilder並讓.NET框架爲您完成繁重的工作:

Option Explicit 

Dim sb : Set sb = CreateObject("System.Text.StringBuilder") 
sb.AppendFormat "{0:dd/MM/yyyy h:mm:ss tt}", Now() 
Msgbox sb.ToString() 
' Output: 15/11/2013 14:55:10 PM  
+0

Awww好建議 - 那就是我最初的想法,但看到我想要通過分裂成日,月,年大塊和組裝自己來獨立處理它。 謝謝壽! – Fearghal