Search code examples
asp.net-core.net-coremsbuildcsproj

MSBuild outputting incorrect DateTime.UtcNow format


For my Version tag in csproj I've the following:

    <PropertyGroup>
        <Version>
            $([System.DateTime]::UtcNow.Year).$([System.DateTime]::UtcNow.ToString("MM")).$([System.DateTime]::UtcNow.ToString("dd")).$([System.DateTime]::UtcNow.ToString("HHmm"))
        </Version>
    </PropertyGroup>

But it outputs like so: Vysn.Voice.2020.1.9.309.nupkg for nuget packages and when I get version info from assembly: Assembly.GetExecutingAssembly().GetName().Version.

But when I do

var assembly = Assembly.GetExecutingAssembly();
            var informationalVersionAttribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();

Console.WriteLine(informationalVersionAttribute.InformationalVersion);

It outputs the proper DateTime string: 2020.01.08.1018. I'm not sure if I'm doing something wrong when specifying the version but I want it to output the version properly: 2020.MM.dd.HHmm.


Solution

  • But it outputs like so: Vysn.Voice.2020.1.9.309.nupkg for nuget packages

    This is a behavior by design.

    1. First of all, The Vysn.Voice is so called package identifier, and the 2020.1.9.309 is named package version. As the official docs describes:

      A specific package is always referred to using its package identifier and an exact version number

      • By default, the package version matches the Version. As a result, you can change the package version by custom the <Version/> element in *.csproj. Be aware the package version will NOT affect the package identifier.
      • By default, the package identifier matches the metadata of <PackageId>Vysn.Voice</PackageId> in the *.csproj file. If <PackageId/> is not specified or empty, it will use the AssemblyName. For more details, see Package Id.
    2. Also, note the version number in <Major>.<Minor>.<Build> should be an Integer. And when a project is packaged using NuGet, the leading zeroes in the version numbers are always removed from the Major, Minor, and Build part. So instead of getting 2020.01.09, you'll get 2020.1.9. This behavior is documented here.

    3. Finally, when you invoke

      assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
      

      you're actually trying to get the InformationalVersion which is different from Version or PackageVersion. InformationalVersion might contain any string. This InformationalVersion matches the Version by default and might looks like 2020.01.09.0704 (Note there will be some leading zeros.)