Reputation: 11447
I have a VBScript that writes a dateTimne to a file.
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
When i run it manually on my server it gives me a US format of mm/dd/yyyy hh:mm:ss AM eg 11/14/2013 9:20:56 AM.
When i run it on my Laptop it gives me back the UK format i actually want - dd/mm/yyyy hh:mm:ss AM eg 14/11/2013 9:20:56 AM.
Any ideas whats going on here?
Upvotes: 1
Views: 5874
Reputation: 7490
The question is already answered, but I am wondered why nobody suggests setting the locale to the correct date time format:
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
And for creating a date-time-format without use of the locale (the AM / PM setting is sometimes not displayed depending on the settings in the region and language settings configuration of the computer running the script) you can always use a .NET stringbuilder and let the .NET framework do the heavy lifting for you:
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
Upvotes: 4
Reputation: 41287
This Windows batch script uses a VBS routine to give reliable date variables.
You'll need to extract the VBS script and modify it where needed.
:: 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
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Upvotes: 0
Reputation: 424
Try This:
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
Copy this and past it into your .VBS File and adjust what you want if you are not working with ISO 8601 Standard
Upvotes: 2
Reputation: 338406
VBScript lacks a flexible date formatting function, dates are output according to the current locale of the host running the script.
FormatDateTime()
accepts a few constants that modify its output, but you cannot pass a custom format string.
This means you must write your own function that produces a locale-independent format.
In any case: please use ISO 8601 and not any crazy moon formats like mm/dd/yyyy
.
Upvotes: 1