Datetime VBS不正确的日期时间格式

Datetime VBS不正确的日期时间格式,datetime,vbscript,regional-settings,Datetime,Vbscript,Regional Settings,我有一个VBScript,可以将日期信息写入文件 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

我有一个VBScript,可以将日期信息写入文件

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
选项显式
Dim fso、路径、文件、recentDate、recentFile、objFileHandle
设置fso=CreateObject(“Scripting.FileSystemObject”)
Set recentFile=Nothing
对于fso.GetFolder(“\\path\folder”).Files中的每个文件
如果(recentFile为Nothing),则
设置recentFile=file
ElseIf(file.datelastmedited
当我在我的服务器上手动运行它时,它会给我一个美国格式的mm/dd/yyyy hh:mm:ss AM,例如11/14/2013 9:20:56 AM

当我在笔记本电脑上运行它时,它会返回我真正想要的英国格式-dd/mm/yyyy hh:mm:ss AM eg 14/11/2013 9:20:56 AM


有什么想法吗?

VBScript缺少灵活的日期格式功能,日期是根据运行脚本的主机的当前区域设置输出的

修改其输出,但不能传递自定义格式字符串

这意味着您必须编写自己的函数来生成与区域设置无关的格式


在任何情况下:请不要使用任何疯狂的moon格式,如
mm/dd/yyyy

VBScript缺少灵活的日期格式功能,日期是根据运行脚本的主机的当前区域设置输出的

修改其输出,但不能传递自定义格式字符串

这意味着您必须编写自己的函数来生成与区域设置无关的格式

在任何情况下:请不要使用任何疯狂的moon格式,如
mm/dd/yyyy

尝试以下方法:

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
选项显式
Dim fso、路径、文件、recentDate、recentFile、objFileHandle
设置fso=CreateObject(“Scripting.FileSystemObject”)
Set recentFile=Nothing
对于fso.GetFolder(“folder”).Files中的每个文件
如果(recentFile为Nothing),则
设置recentFile=file
ElseIf(file.datelastmedited
如果不使用ISO 8601标准,请将其复制到.VBS文件中,然后调整所需内容

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
选项显式
Dim fso、路径、文件、recentDate、recentFile、objFileHandle
设置fso=CreateObject(“Scripting.FileSystemObject”)
Set recentFile=Nothing
对于fso.GetFolder(“folder”).Files中的每个文件
如果(recentFile为Nothing),则
设置recentFile=file
ElseIf(file.datelastmedited

如果您不使用ISO 8601标准,请将其复制到.VBS文件中,并调整所需内容。此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
  ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

此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
  ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

这个问题已经得到了回答,但我想知道为什么没有人建议将区域设置为正确的日期-时间格式:

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 framework为您完成繁重的工作:

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    

这个问题已经得到了回答,但我想知道为什么没有人建议将区域设置为正确的日期-时间格式:

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 framework为您完成繁重的工作:

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    

请注意,
“True”
只是偶然的
True
(因为任何非空字符串的计算结果都是
True
-
“False”
也同样有效)。使用实际的布尔值,而不是字符串。请注意,
“True”
只是意外地
True
(因为任何非空字符串的计算结果都是
True
-
“False”
)。使用实际的布尔值,而不是字符串。@Fearghal-否,因为您不知道哪些其他程序/人员依赖于当前的区域设置。否。因为您确实不希望脚本根据某些模糊的环境设置生成不同的输出。以一种输出稳定且可预测的方式编写脚本,这意味着:使用适当的自定义格式功能。我不确定如何使用ISO8601实现这一点,你能提供帮助吗?好的,我更改了本地设置。这是一个PoC系统,所以没有危险。我注意到手动运行此脚本现在可以按预期工作,但当它作为SQL server作业运行时,它仍然执行美国版本。@Fearghal-否,因为您不知道哪些程序/人员依赖于当前的区域设置。否。因为你