17

I have a MSBuild project and I want the current date to be added to a zip file that I am creating.

I am using the MSBuildCommunityTasks.

<!-- Import the CommunityTasks Helpper -->
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />

On the website http://msbuildtasks.tigris.org/ I can see a task called time. I have not been able to find doc on how to use Time.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Eric Brown - Cal
  • 14,135
  • 12
  • 58
  • 97
  • 4
    The MSBuild tasks included a CHM file in the directory you installed it in. Whenever I have to use MSBuildCommunityTasks I always keep that file open. – Min May 18 '09 at 15:31

3 Answers3

42

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 '

Maslow
  • 18,464
  • 20
  • 106
  • 193
  • 4
    More Date/time tostring formats here: http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm – riffrazor Dec 12 '11 at 19:40
21
<?xml version="1.0" encoding="utf-8"?>

<Project DefaultTargets="Deploy" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>

  <!-- Include MSBuild tasks here -->

  <ItemGroup>     
      <DefaultExclude Include="****" />           
  </ItemGroup>


 <Target Name="Deploy" >

    <Time Format="yyyy-MM-dd">
    <Output TaskParameter="FormattedTime" PropertyName="buildDate" />
    </Time>

    <Message Text="Deploying ...."></Message>   

    <Copy  SourceFiles="@(DeploymentFiles)" DestinationFolder="C:\CCNET\$(buildDate)\bin\" />

</Target>

</Project>
abmv
  • 7,042
  • 17
  • 62
  • 100
2

Maslow's answer is correct (I can't comment on it or I would); I would only add to it that you have to be careful when implicitly calling System.DateTime.Parse.

A parsed string value like $([System.DateTime]::Parse("1970-01-01T00:00:00.0000000Z") doesn't seem to end up with a Kind of DateTimeKind.Utc.

But you can use nested property functions to make it work; like this (to get the Unix timestamp):

$([System.DateTime]::UtcNow.Subtract($([System.DateTime]::Parse("1970-01-01T00:00:00.0000000Z").ToUniversalTime())).TotalSeconds.ToString("F0"))

Dave
  • 332
  • 1
  • 7