NAnt 中的月份名称

发布于 2024-12-09 21:52:18 字数 533 浏览 0 评论 0 原文

如何在 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())} 仅返回月份数字,而不返回名称。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

獨角戲 2024-12-16 21:52:19

更优雅的方式:

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

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>
困倦 2024-12-16 21:52:19

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

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

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>
丢了幸福的猪 2024-12-16 21:52:19

为什么不使用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/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文