0

I've tried all the ways I see to add a month to a certain date then return that in a specific format but I'm at a loss. Here's my code but I need to format it:

replace( formatdatetime( dateadd( "m" , 1 , request("date") ), 0 ) , "/" , "-" ) & "' )

request("date") is in yyyyy-dd-mm hh:mm:ss format and that's how I need the new date.

3 Answers 3

1

The following should work perfect:

replace( formatdatetime( dateadd( "m" , 1 , cDate(request("date")) ), 0 ) , "/" , "-" )

Notice the use of the cDate function to convert a value to a date explicitly.

Edit:

I removed last part of your code & "' ), it gave me an error otherwise.

1
  • I accidentally included part of the SQL I was making this a part of; that's what the & "' ) is about. May 17, 2012 at 8:20
1

When working with dates, it's especially important to take care of the proper data (sub)types. Feeding a string to a function that expects a date (and relying on 'VBScript - and your local settings - will do the right thing') is dangerous.

Using replace will never change the order of the date parts.

FormatDateTime depends on the local/regional settings and should be avoided as a sure path to disaster.

One way to solve this problem + most of all other problems concerning fancy formatting in VBScript is to use a .Net System.Text.StringBuilder:

Given Lib.vbs:

' Lib.vbs - simple VBScript library/module
' use
'  ExecuteGlobal goFS.OpenTextFile(<PathTo\Lib.vbs>).ReadAll()
' to 'include' Lib.vbs in you main script

Class ToBeAShamedOf
  Public a
  Public b
End Class ' ToBeAShamedOf

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

and main.vbs:

' main.vbs - demo use of library/module Lib.vbs

' Globals
Dim gsLibDir : gsLibDir = ".\"
Dim goFS     : Set goFS = CreateObject("Scripting.FileSystemObject")

' LibraryInclude
ExecuteGlobal goFS.OpenTextFile(goFS.BuildPath(gsLibDir, "Lib.vbs")).ReadAll()

WScript.Quit demoDateFormat()
WScript.Quit main()

Function main()
  Dim o : Set o = New ToBeAShamedOf
  o.a = 4711
  o.b = "whatever"
  WScript.Echo o.a, o.b
  main = 1 ' can't call this a success
End Function ' main

Function demoDateFormat()
  Dim sD   : sD       = "2012-05-16 01:02:03" ' near future; not yyyyy!
  Dim dtD  : dtD      = CDate(sD)
  Dim dtDM : dtDM     = DateAdd("m", 1, dtD)
  Dim oFmt : Set oFmt = New cFormat
  WScript.Echo oFmt.formatArray( _
      "   sD: {1}{0}  dtD: {2}{0} dtDM: {3}{0}dtDM': {4}" _
    , Array(vbCrLf, sD, dtD, dtDM, oFmt.formatOne("{0:yyyy-MM-dd hh:mm:ss}", dtDM)))
  demoDateFormat = 0 ' seems to be decent
End Function ' demoDateFormat

you'll get:

cscript main.vbs
   sD: 2012-05-16 01:02:03
  dtD: 16.05.2012 01:02:03
 dtDM: 16.06.2012 01:02:03
dtDM': 2012-06-16 01:02:03

(to be seen in the context of this answer)

2
  • This is ASP Classic, not just VBScript. WScript is not available and CreateObject needs the Server prefix. May 16, 2012 at 11:29
  • Unfortunately, .NET is not an option. May 16, 2012 at 15:20
1

This may help:

FormatDateTime(DateAdd("M",1,DateSerial(Left(request("date"),4),Mid(request("date"),9,2),Mid(request("date"),6,2))) & " " & Mid(request("date"),12,8),d,0)

It basically converts the string to a valid date in the native format, adds the 1 requested month and then rebuilds the string.

NOTE: request("date") looks as though it returns the current datetime so running it in this way may generate a final value that is a second or so out, if that's a problem then you will be better storing a static value in a variable, otherwise this should hopefully be ok.

3
  • Where does this Format(Value,FormatString) function come from? May 16, 2012 at 11:01
  • @Ekkehard.Horner I'm confused, are you saying this is not recognised as a vbs command. Format is available as part of VBA/VB6 and thus should be equally available in VBS? May 16, 2012 at 11:05
  • @Ekkehard.Horner Yep, my mistake, most are avail but this one is replaced with FormatDateTime, I've amended my post, cheers ^_^ May 16, 2012 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.