Goran
Goran

Reputation: 1837

date format in msbuild script?

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?

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

<PropertyGroup>
  <ZipOutDir>C:\output</ZipOutDir>
  <ZipFileName>Application_$(Year)$(Month)$(Day).zip</ZipFileName>
</PropertyGroup>

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

Upvotes: 14

Views: 8273

Answers (4)

Maslow
Maslow

Reputation: 18746

In msbuild 4 you can now

$([Namespace.Type]::Method(..parameters…))
$([Namespace.Type]::Property)
$([Namespace.Type]::set_Property(value))

so I am using

$([System.DateTime]::Now.ToString(`yyyy.MMdd`))

those ticks around the format are backticks not '

Upvotes: 31

DougP
DougP

Reputation: 1

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

$([System.UInt16]::Parse($(Month)).ToString('00'))

Upvotes: -1

Anton Gogolev
Anton Gogolev

Reputation: 115857

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.

Upvotes: 0

Ruben Bartelink
Ruben Bartelink

Reputation: 61885

You could use the MSBuild extension pack a la:

http://www.msbuildextensionpack.com/help/3.5.3.0/html/9c5401ed-6f55-089e-3918-2476c186ca66.htm

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

MSBuild MSBuildCommunityTasks Task Time

Upvotes: 4

Related Questions