DateAdd () in a special format

I tried all the ways to add a month to a specific date, and then return it in a specific format, but I'm at a loss. Here is my code, but I need to format it:

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

request("date")is in format yyyyy-dd-mm hh:mm:ssand that i need a new date.

0
source share
3 answers

The following should work perfectly:

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

Note the use of the function cDateto explicitly convert a value to a date.

Edit:

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

+1
source

(sub). , , ( "VBScript - - " ), .

replace .

FormatDateTime / .

+ , VBScript, - .Net System.Text.StringBuilder:

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

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

:

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

(. )

+1

:

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)

, 1 .

: ( "" ) , -, , , , , , , .

+1

All Articles