2011-10-14 131 views
1

如何在NAnt中获取当前的月份名称?NAnt的月份名称

我正在使用类似下面的任务来更新我们的网站标签,该标签显示网站的建立日期/时间。但是,我需要将日期设置为特定格式,而不是根据数据的不同而不同。

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

我需要的格式是"MMM DD, YYYY"。 NAnt似乎不允许我指定格式,但我可以使用NAnt函数获取日期的各个部分。

有没有什么办法可以使用NAnt函数获取当前月份的名称?
${datetime::get-month(datetime::now())}只返回月份编号,而不是名称。

回答

1

它可能不是优雅,但你可以是这样的:

<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> 
+0

谢谢,工程就像一个魅力,但一个'Choose' - >'When'说法是有点漂亮。 –

3

更优雅的方式:

<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> 
0

为什么不使用方法format-to-string?该方法内置于最新版本的NANT,因此没有理由扮演你自己的角色。

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

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

使用下面的链接,了解更多信息:

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/