0

I am trying to reformat a date from a short date format (e.g. 07/31/2018) to this custom format MMDDYY. So if the date is 07/31/2018, I would like to make it 073118.

I'm really at a loss on how to do this. The code below lets me extract the first two digits on the left of the first slash. But I'm unsure how to extract the rest as well as work around the extraction with two slashes. Could somebody please lead me in the right direction to accomplish this?

If InStr(strRDate, "/") > 0 Then
    strRDateL = Left(strRDate, InStr(strRDate, "/")-1)
    If Len(strRDateL)=2 Then
        subMoveCursor 11, 10
        subEnterData strRDateL
    End If
End If
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Possible duplicate of [Format current date and time in VBScript](https://stackoverflow.com/questions/22574092/format-current-date-and-time-in-vbscript) – user692942 Jul 31 '18 at 16:37
  • Also [Example of parsing a Date string](https://stackoverflow.com/a/48243660/692942) – user692942 Jul 31 '18 at 20:24

4 Answers4

1

The problem is not one of date formatting, but one of string manipulation. You have a string that represents a date in a specific format; the task is to drop some unwanted characters:

  Dim aT : aT = Split("07/31/2018,073118 01/01/0001,010101")
  Dim sP, aP, sL, sExp, sAct
  For Each sP in aT ' get pair
      aP = Split(sP, ",") ' split into long anf short parts
      sL = aP(0)
      sExp = aP(1)
      sAct = Left(sL, 2) & Mid(sL, 4, 2) & Right(sL, 2)
      WScript.Echo sL, sAct, CStr(sAct = sExp) 
  Next

output:

cscript 51617746.vbs
07/31/2018 073118 Wahr
01/01/0001 010101 Wahr
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
0

You need to write your own date to string function.

The easiest way is likely to see if your string is recognized as a date by using the IsDate() function. Then you can cast it to a Date object using the CDate() function.

Then you can use other built-in date functions to get the Month, Day, and Year portions of the date.

strDate = "07/31/2018"

WScript.Echo FormatDate(strDate)
WScript.Echo FormatDateString(strDate)

Function FormatDate(strDate)
    If IsDate(strDate) Then 
        dtmDate = CDate(strDate)
        return = Right("0" & Month(dtmDate),2) & Right("0" & Day(dtmDate),2) &  Right(Year(dtmDate),2)
    Else
        return = strDate
    End If 
    FormatDate = return
End Function 

If you don't want to do it this way, the other option is to format the incoming string directly. This would require you know for sure the input string will always be in the same format.

Function FormatDateString(strDate)
    'Add more error checking?
    FormatDateString = Split(strDate,"/")(0) & Split(strDate,"/")(1) & Right(strDate,2)
End Function 

7-31-2018 would fail in FormatDateString, but succeed in FormatDate, for example.

langstrom
  • 1,652
  • 11
  • 14
0

If you can be absolutely certain the format is what you showed (since it is a date, it almost never is, but still), just treat it as a string and do something like:

Dim dt : dt = "07/31/2018" 'define "date" as a string
Dim a : a = Split(dt, "/") 'split string to array
WScript.Echo a(0) & a(1) & Right(a(2),2) ' emit to console

This does no checking of whether the passed value is actually a date of course, but is the shortest way I can think of without resorting to Mid().

Rno
  • 784
  • 1
  • 6
  • 16
0

This is how I got it to work.

If InStr(strRDate, "/") > 0 Then
    strRDateSplit = Split(strRDate, "/")
    subMoveCursor 11, 10
    subEnterData strRDateSplit(0) & strRDateSplit(1) & Right(strRDateSplit(2),2)
Else
    subMoveCursor 11, 10
    subEnterData rw.Cells(3).Value
End If