开发者

Month name in NAnt

开发者 https://www.devze.com 2023-04-13 06:38 出处:网络
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 spe

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']/@val开发者_开发问答ue" 
    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.


A more elegant way:

<target name="echo-month">
    <script prefix="utils" language="C#">
         <code>
             <![CDATA[
                 [Function("GetMonth")]
                 public static string GetMonth() {
                    return System.DateTime.Now.ToLongDateString().Split(new Char[]{' '})[1];
                 }
             ]]>
         </code>
   </script>

   <property name="the_month" value="${utils::GetMonth()}"/>
   <echo message="The current month is ${the_month}"/>
</target>


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>


Why not use the method format-to-string? The method is built into the latest version of NANT, so there is no reason to role-your-own.

Month:
${datetime::format-to-string(datetime::now(), 'MMMM')}

Format in question:
${datetime::format-to-string(datetime::now(), 'MMMM d, yyyy')}

Use the following links for more info:

http://nant.sourceforge.net/release/0.92/help/functions/datetime.format-to-string%28System.DateTime,System.String%29.html

http://www.csharp-examples.net/string-format-datetime/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号