In my msbuild script I'm creating a zip file with year/month/day in the zip filename, but month and day are always written with no leading zero.

Is there a way to add leading zero to my zip filename?

  <Output TaskParameter="Year" PropertyName="Year" />
  <Output TaskParameter="Month" PropertyName="Month" />
  <Output TaskParameter="Day" PropertyName="Day" />


And the result is: 'Application_2010122.zip' (with no leading zero for january, as you can see)


You could use the MSBuild extension pack a la:


Or use the format param to the Time task from community tasks [which you appear to be using]

MSBuild MSBuildCommunityTasks Task Time

  • Thank you... I already found the later link and used it. – Goran Jan 22 '10 at 11:56
  • Good stuff. BTW Always a good idea to mention which task libs you're using / open to using when asking questions of this ilk. – Ruben Bartelink Jan 22 '10 at 12:01
  • It would be good if this accepted answer actually had the answer to the question added to it. That is, how, using the format param in the Time task from community tasks, does one format the date so there are leading 0s on the months? – ssmith Feb 1 '11 at 19:06
  • If you have the specific incantation to hand and you chat, gist or mail it to me, happy to paste it in. A the time of answering it, I personally didnt have a snippet to lift from a production system that I could be confident wouldnt be misleading or incorrect at the time of answering. (Saw your recent blog post so I'm sure you have one (sub'd and also see it via @CSharpFeeds)). Remember, I'm not @Sayed Ibrahim Hashimi that'll just roll up a full working tested example for amusement value :P – Ruben Bartelink Feb 3 '11 at 9:55
  • @ssmith: For some reason I omitted an @ssmith in my last response, Doh! – Ruben Bartelink Feb 6 '11 at 4:29

In msbuild 4 you can now


so I am using


those ticks around the format are backticks not '


It's because MSBuild operates solely with strings. You'll have to either modify existing tasks so that all properties will return strings instead of ints (or whatever integer value they return), or create a separate task which will format year, month and day according to your needs.

  • Given the amount of task libs out there, just wanted to point out that your comment suggests writing a Task instead of finding one that already does it - I'm sure you didnt intend that. (And I'd use a PowerShell task to do tiny formatting hacks like this if I one of the common libs out there didnt already have a task for it) – Ruben Bartelink Jan 22 '10 at 12:00
  • @Ruben: Sure! I just couldn't find any string formatting task for MSBuild. Otherwise I'd definitely suggested using one of those. – Anton Gogolev Jan 22 '10 at 12:03
  • Interesting that there isnt one actually... I'd be interested to know whether most people use a PowerShell task if cornered on something like this? (The best exanmple I can think of is that vdproj files need guids with capital letters in them and I resorted to PS to do a ToUpper as I couldnt find a Task to do it (or produce a captialized Guid)) – Ruben Bartelink Jan 22 '10 at 12:15

Here's a cheap and dirty way to add a leading zero


Your Answer

