Forum Discussion

sedens's avatar
sedens
Contributor
3 years ago

Leading zeros are not dropped when using %#d in aqConvert.DateTimeToFormatStr function

I need to remove leading zeros from the day when using date formats e.g. 7/15/1994 instead of 07/15/1994 however when I use the aqConvert.DateTimeToFormatStr function it does not drop the leading zero even though the TestComplete documentation mentions if using the # flag in the format specifier, the leading zeros are removed from the number. (https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqdatetime/date-and-time-format-specifiers.html?sbsearch=leading%20zeros)

 

Here is the code I used (Python): 

DOBField = "DOB - DOD:\n{} - {}".format(aqConvert.DateTimeToFormatStr(Driver.value[3], "%m/%#d/%Y"), aqConvert.DateTimeToFormatStr(Driver.value[6], "%m/%#d/%Y"));

Log.Message(DOBField);

 

The Driver.value[3] and Driver.value[6] is the data that is retrieved from an Excel spreadsheet. The date in these two fields correspond to 7/15/1994 and 6/17/2017. 

 

When I run the script, it logs the following "DOB - DOD:07/15/1994 - 06/17/2017"

 

I expected to get "DOB - DOD:7/15/1994 - 6/17/2017"

 

If I remove the # from the %#d format specifier, it still returns the date with the leading zeros. Can anyone advise what I am doing wrong in my code? Thanks. 

  • Hi sedens!

     

    Pardon any confusion here on my part, but it appears you are looking to drop the zero from the Month in this case? Which means we need to apply the '#' to the Month section as well, instead of just the Day. 

     

    This formatting removes all the zeros from your date in a script I built out;

    aqConvert.DateTimeToFormatStr(myDate, "%#m/%#d/%Y")

     

    I hope this helps!

     

  • Hi sedens!

     

    Pardon any confusion here on my part, but it appears you are looking to drop the zero from the Month in this case? Which means we need to apply the '#' to the Month section as well, instead of just the Day. 

     

    This formatting removes all the zeros from your date in a script I built out;

    aqConvert.DateTimeToFormatStr(myDate, "%#m/%#d/%Y")

     

    I hope this helps!

     

    • sedens's avatar
      sedens
      Contributor

      Ah yes ... it appears I have mixed up my date formats again. I'm an Australian living in the US so I've mixed up the formatting of the dates. In Australia, dates are generally d/m/yyyy while the US has dates as m/d/yyyy which makes it easy to mix them up. 

       

      Thank you for your help. I was using the wrong format specifier. Using #m worked as well as #d if I had made the month a single digit. That should have been my clue I was using the wrong specifier.

       

      Thank you for your help!

      • npaisley's avatar
        npaisley
        Staff

        Hey sedens!

         

        I have often thought that the US should use the same date methods, I think it makes more sense. And I can only imagine the confusion in your head at times, lol! 

         

        Glad to hear that worked, good luck moving forward!