1

I am executing a VBS file that returns the modified date of another file:

Set objFS=CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
strFile= objArgs(0)
WScript.Echo objFS.GetFile(strFile).DateLastModified

However I want the date to be formatted to dd/MM/yyyy before it is returned.

DMB
  • 13
  • 1
  • 3

3 Answers3

2

As demonstrated here and https://stackoverflow.com/a/21280396/603855, vou can use a .Net StringBuilder to solve all your formatting problem without depending on locales or doing extra/specific work:

Option Explicit

Class cFormat
  Private m_oSB
  Private Sub Class_Initialize()
    Set m_oSB = CreateObject("System.Text.StringBuilder")
  End Sub ' Class_Initialize
  Public Function formatOne(sFmt, vElm)
    m_oSB.AppendFormat sFmt, vElm
    formatOne = m_oSB.ToString()
    m_oSB.Length = 0
  End Function ' formatOne
  Public Function formatArray(sFmt, aElms)
    m_oSB.AppendFormat_4 sFmt, (aElms)
    formatArray = m_oSB.ToString()
    m_oSB.Length = 0
  End Function ' formatArray
End Class ' cFormat

Dim oFmt : Set oFmt = New cFormat
WScript.Echo oFmt.FormatOne("Today: {0:dd\/MM\/yyyy}", Date())

output:

Today: 08/02/2014
Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
0

The value returned by objFS.GetFile(strFile).DateLastModified is a date/time value. You can format this into a string using a formatting or conversion function:

dateLastModified = objFS.GetFile(strFile).DateLastModified
WScript.Echo FormatDateTime(dateLastModified, 2)

This will format the date/time value into a short date. How this is done depends on your current regional settings. E.g. in some cultures you will have "MM/dd/yyyy" and in other cultures "dd-MM-yyyy" etc.

You can also get complete control over the formatting by extracting the day, month and year part and putting them together into a string:

dateLastModified = objFS.GetFile(strFile).DateLastModified
WScript.Echo Day(dateLastModified) & "/" & Month(dateLastModified) & "/" & Year(dateLastModified)
Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
-1

You could just take the date modified of the file and pick it apart a bit..

So if the last modified date was 11/30/2014, this would return 30/11/2014 regardless of regional PC settings (which FormatDateTime() ignores)

WScript.Echo convertDate(objFS.GetFile(strFile).DateLastModified)

Function convertDate(strDate)
  convertDate = DatePart("d", strDate) & "/" & DatePart("m", strDate) & "/" & DatePart("yyyy", strDate)
End Function
JustSomeQuickGuy
  • 933
  • 1
  • 10
  • 21
  • Thanks, this was easy to implement. – DMB Feb 08 '14 at 08:37
  • `FormatDateTime` does __not__ ignore regional PC settings. In fact, with my settings todays date is formatted as "08-02-2014" and not "02/08/2014". – Martin Liversage Feb 08 '14 at 08:43
  • 1
    Please note, this will return 8/2/2014 and not 08/02/2014 and is not according the specification dd/MM/yyyy. To keep it simple: You can _object chain_ the Stringbuilder Object given by Ekkerhard.Horner to `wscript.echo CreateObject("System.Text.StringBuilder").AppendFormat("Today: {0:dd\/MM\/yyyy}", now).ToString()`. This will display `10/02/2014` – AutomatedChaos Feb 10 '14 at 15:35