Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

timestamp question

Hi,

I have a Activex Script in DTS package. It should save in file in mmddyy format.
when it is 10-09-2007 it should save as 100907 but instead it is saving as 10907. what should i change in my code. Below is the code.

Thanks in advance,
nk

Option Explicit

Function Main()

      Dim oRS
      Set oRS = DTSGlobalVariables("RSTables").Value
      
      oRS.MoveFirst()

    Const ForReading = 1
    Const ForWriting = 2
dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")

    Dim ResultFile, objResult
      
             dim strSourceFile
             dim strFilePath
             
             strFilePath = DTSGlobalVariables("FilePath").Value            
      ResultFile = strFilePath   & Month(Date) & day(date-1) & right(year(date),2) & DTSGlobalVariables("SourceFileName").Value    
   
Set objResult = fso.OpenTextFile(ResultFile, ForWriting, True)
      do while not oRS.EOF             
             objResult.WriteLine       oRS.Fields(0).Value            
            oRS.MoveNext
      loop

        objResult.Close

        Set objResult = Nothing

    Set fso = Nothing

      Set oRS = Nothing

      Main = DTSTaskExecResult_Success
End Function
0
NK9
Asked:
NK9
1 Solution
 
cupCommented:
A method of printing numbers with leading zeros is to add a factor of 10 more than the number of zeros.  For instance, if you wanted up to 3 zeros it would be something like

right (cstr (1000 + n), 3)

In your case, you wanted 2 leading zeros, so add 100 and take the rightmost 2 digits.

Try this

ResultFile = strFilePath   & _
    right(cstr(100+Month(Date)), 2) & _
    right(cstr(100+day(date-1)), 2) & _
    right(year(date),2) & DTSGlobalVariables("SourceFileName").Value

Also test on single digit months and days.  You will probably find that the original source on 6 May would read 5607 instead of 050607.
0
 
RobSampsonCommented:
Hi, change this line:
ResultFile = strFilePath & Month(Date) & day(date-1) & right(year(date),2) & DTSGlobalVariables("SourceFileName").Value

to this:
ResultFile = strFilePath & Right("00" & Month(Date), 2) & Right("00" & Day(Date-1), 2) & Right(year(date),2) & DTSGlobalVariables("SourceFileName").Value

What I have done there is add two "Right" functions to the Month and Day functions.  This gets the two right most characters from a string like:
0010 for October, being 10, and
009 for the ninth day, being 09

Regards,

Rob.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now