Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. VBScript String Formatting

VBScript String Formatting

Scheduled Pinned Locked Moved Visual Basic
c++csshelpquestion
3 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Justin Cooke
    wrote on last edited by
    #1

    Hi All, I'm generating a fileName based on the current date/time and would like to simply prepend leading zeros to the month, day, hours, minutes, and seconds if they're less than 10. This seems so simple and yet I haven't seen a function to do it (in VBScript). Of course, I could write my own, but I'd rather not if MS provided one that I've just missed. Do any of you know if this already exists? In VB6, I would just write something like: strFinal = Format( nNum, "00" ) In C/C++, something like: sprintf( strFinal, "%02d", nNum ); (Please forgive any synatx errors...it's been a while since I wrote C :)) Any help is greatly appreciated. Thanks! Justin

    D 1 Reply Last reply
    0
    • J Justin Cooke

      Hi All, I'm generating a fileName based on the current date/time and would like to simply prepend leading zeros to the month, day, hours, minutes, and seconds if they're less than 10. This seems so simple and yet I haven't seen a function to do it (in VBScript). Of course, I could write my own, but I'd rather not if MS provided one that I've just missed. Do any of you know if this already exists? In VB6, I would just write something like: strFinal = Format( nNum, "00" ) In C/C++, something like: sprintf( strFinal, "%02d", nNum ); (Please forgive any synatx errors...it's been a while since I wrote C :)) Any help is greatly appreciated. Thanks! Justin

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Yeah, VBScripts formatting leaves much to be desired. But, there is a very easy solution to the problem.

      Function FLZ(length, number)
      Dim temp
      ' Generate a string of zeros the length we need, then append the number
      ' length = 5 and number = 232 would be "00000232"
      temp = String( length, "0" ) & number
      ' Grab the length of the string we need to return
      ' length = 5 would return "00232"
      FLZ = Right( temp, length )
      End Function

      Now all you have to do is call this for each number you want to format:

      Filename = FLZ(2, Month) & FLZ(2, Day) & "-" & FLZ(2, Hour) & FLZ(2, Minute) & FLZ(2, Second)

      RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      J 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Yeah, VBScripts formatting leaves much to be desired. But, there is a very easy solution to the problem.

        Function FLZ(length, number)
        Dim temp
        ' Generate a string of zeros the length we need, then append the number
        ' length = 5 and number = 232 would be "00000232"
        temp = String( length, "0" ) & number
        ' Grab the length of the string we need to return
        ' length = 5 would return "00232"
        FLZ = Right( temp, length )
        End Function

        Now all you have to do is call this for each number you want to format:

        Filename = FLZ(2, Month) & FLZ(2, Day) & "-" & FLZ(2, Hour) & FLZ(2, Minute) & FLZ(2, Second)

        RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        J Offline
        J Offline
        Justin Cooke
        wrote on last edited by
        #3

        Thanks, Dave! That works perfectly and you saved me the hassle (albeit minor :)) of writing it myself. Much appreciated.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups