Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the difference between two dates in a .csproj file?

I saw some code like this, in a csproj file

$([System.DateTime]::UtcNow.ToString(mmff))

to autoincrement the assembly version:

<VersionSuffix>2.0.0.$([System.DateTime]::UtcNow.ToString(mmff))</VersionSuffix>
<AssemblyVersion Condition=" '$(VersionSuffix)' == '' ">0.0.0.1</AssemblyVersion>

What kind of language/script is that? How do I use it to get the difference between two dates?

I tried to do something like this:

<VersionMajor>2</VersionMajor>
<VersionMinor>1</VersionMinor>
<DaysFromLastRelease>$(([System.DateTime]::UtcNow - new [System.DateTime](2021,1,1))::TotalDays)</DaysFromLastRelease>

but it does not work :)

like image 842
serge Avatar asked May 12 '21 18:05

serge


People also ask

How to find difference between two dates in C#?

The difference between two dates can be calculated in C# by using the substraction operator - or the DateTime. Subtract() method. The following example demonstrates getting the time interval between two dates using the - operator.

How to subtract dates in C#?

This method is used to subtract the specified date and time from this instance. Syntax: public TimeSpan Subtract (DateTime value); Return Value: This method returns a time interval that is equal to the date and time represented by this instance minus the date and time represented by value.

What is a Csproj file?

Files with CSPROJ extension represent a C# project file that contains the list of files included in a project along with the references to system assemblies. When a new project is initiated in Microsoft VIiual Studio, you get one . csproj file along with the main solution (. sln) file.


1 Answers

.csproj files are basically MSBuild files (XML). The embedded syntax you are referring to is called a Property Function.

It appears that subtraction using the minus (-) might not be supported. There is a Subtract() property function in Property Functions.

Perhaps this could be the base for a solution. I have not tried it!

<Now>$([System.DateTime]::UtcNow.DayOfYear)</Now>

<January>$([System.DateTime]::new(2021,1,1)).DayOfYear</January>
<!-- or... (not sure about the below)
<January>$([System.DateTime]::Parse("1/1/2021").DayOfYear)</January>
 -->

<DaysFromLastRelease>$([MSBuild]::Subtract($(Now), $(January)))</DaysFromLastRelease>

Other possibilities

  • calculate the date difference by writing an MSBuild task
  • call out to a simple program you write
  • somehow use an external program to set an environment variable, and then reference that variable in your .csproj
like image 151
Kit Avatar answered Sep 28 '22 11:09

Kit