如何在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())}仅返回月份编号,而不返回名称。
发布于 2011-10-15 03:15:45
它可能不是很优雅,但是你可以这样做:
<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>发布于 2011-10-15 12:33:27
一种更优雅的方式:
<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>发布于 2015-03-07 01:39:29
为什么不使用format-to-string方法呢?该方法已内置于最新版本的NANT中,因此没有理由充当自己的角色。
Month:
${datetime::format-to-string(datetime::now(), 'MMMM')}
Format in question:
${datetime::format-to-string(datetime::now(), 'MMMM d, yyyy')}有关详细信息,请使用以下链接:
https://stackoverflow.com/questions/7771239
复制相似问题