VBScript Date Formatting For Filenames

Below is just a code fragment that creates a date useful for putting on a filename. It’s not particular elegant, and there are other ways to do the same thing (for example using the PAD function). But it works and it’s easy to read and change.

dtYear  = year(date) 
dtMonth = Right(String(2,"0") & Month(date), 2)
dtDay   = Right(String(2,"0") & Day(date), 2)
dtHour  = Right(String(2,"0") & Hour(now()), 2)
dtMin   = Right(String(2,"0") & Minute(now()), 2)
dtSec   = Right(String(2,"0") & Second(now()), 2)
dtYYYYMMDDHHMM = dtYear & dtMonth & dtDay & dtHour & dtMin 
dtYYYYMMDDHHMM2 = dtYear & "_" & dtMonth & "_" & dtDay & "_" & dtHour & "_" &  dtMin 
dtYYYYMMDDHHMMSS = dtYear & dtMonth & dtDay & dtHour & dtMin & dtSec
dtYYYYMMDDHHMMSS2 = dtYear & dtMonth & dtDay & "_" & dtHour & dtMin & dtSec

'example of how the above can be used to insert a date in a filename.
DTSOutFilenameWithDate = replace(DTSOutFilename, "_.csv", "_" & dtYYYYMMDDHHMM  & ".csv")


See also related VBScript blog: Deleting files in a folder over x days old (in VBScript)

Uncategorized  

Leave a Reply