Search code examples
datetimenant

Month name in NAnt


How do I get the current month name in NAnt?

I'm using a task like the one below to update our site label, which shows the date / time that the site was built. However, I need the date to be in a specific format, rather than varying depending on the machine format.

<xmlpoke file="\\path\to\server\folder\Web.config" 
    xpath="/configuration/appSettings/add[@key = 'SiteLabel']/@value" 
    value="[SQLServer].[SQLDatabase] - ${datetime::to-string(datetime::now())}" />

The format I need is "MMM DD, YYYY". NAnt doesn't seem to allow me to specify the format, but I can get the individual parts of the date using NAnt functions.

Is there any way I can get the name of the current month, using NAnt functions?
${datetime::get-month(datetime::now())} only returns the month number, not the name.


Solution

  • It may not be elegant, but you could something like this:

    <target name="GetMonth">
    
        <property name="today.month" value="${datetime::get-month(datetime::now())}" />
    
        <if test="${today.month=='1'}">
            <property name="month" value="Janurary" />
        </if>
        <if test="${today.month=='2'}">
            <property name="month" value="Feb" />
        </if>
        <if test="${today.month=='3'}">
            <property name="month" value="March" />
        </if>
        <if test="${today.month=='4'}">
            <property name="month" value="April" />
        </if>
        <if test="${today.month=='5'}">
            <property name="month" value="May" />
        </if>
        <if test="${today.month=='6'}">
            <property name="month" value="June" />
        </if>
        <if test="${today.month=='7'}">
            <property name="month" value="July" />
        </if>
        <if test="${today.month=='8'}">
            <property name="month" value="Aug" />
        </if>
        <if test="${today.month=='9'}">
            <property name="month" value="Sept" />
        </if>
        <if test="${today.month=='10'}">
            <property name="month" value="Oct" />
        </if>
        <if test="${today.month=='11'}">
            <property name="month" value="Nov" />
        </if>
        <if test="${today.month=='12'}">
            <property name="month" value="Dec" />
        </if>
    
        <echo>${month}</echo>
    </target>