【问题标题】:calculate age in xslt from birthday从生日计算 xslt 中的年龄
【发布时间】:2012-02-06 05:21:54
【问题描述】:

我有一个名为Birthday 的变量,其值类似于1/1/1970。我需要根据该值计算一个人的年龄。它基本上是来自currentdate()-year 的一年,来自Birthday 变量。但是我如何在 XSLT 中实现这一点?它与 sharepoint DateView webpart 相关。有一个Birthday 类型的datetime 字段。我需要使用该字段提取年龄。谁能指出我正确的方向?

【问题讨论】:

  • 1.0,因为它是 SharePoint,不幸的是
  • 预期结果是什么,源 XML 文档是什么?请提供。目前尚不清楚您对“年龄”的定义是什么——年,年月,还是……?

标签: sharepoint xslt date web-parts dataview


【解决方案1】:

我最喜欢的 XSLT 资源是 Dave Pawson's website。一节涉及date manipulations in XSLT 2.0,它似乎对如何做到这一点有非常明确的答案。

基本上,您必须确保日期格式正确,然后只需使用日期减法:

xs:dateTime(actual-arrival) - xs:dateTime(xs:date(due-date))

(注意:我没有测试过。)

XSLT 1 常见问题解答是 at this link

【讨论】:

    【解决方案2】:

    在 XSLT 1.0 中你需要的东西有点复杂,如果你更容易使用 javascript,你可以这样做:

    <div id="mydate"></div>
    
    <script type="text/javascript">
    <![CDATA[
    
          function datejs(datexslt) {
            var date = datexslt.split('T')[0].split('-');
            var time = datexslt.split('T')[1].split(':');
            return new Date(date[1] + '/' + date[2] + '/' + date[0] + ' ' + time[0] + ':' + time[1] + ':' + time[2].substr(0, 2) + ' UTC'); 
          }
    
          var utc = datejs(']]><xsl:value-of select="@PublishedDate" /><![CDATA[');
    
          //$('#mydate').text(utc);
    
    ]]>
    </script>
    

    【讨论】:

      【解决方案3】:
      <xsl:variable name="Age">
          <xsl:choose>
              <xsl:when test="month-from-date(current-date()) > month-from-date($Birthday) or month-from-date(current-date()) = month-from-date($Birthday) and day-from-date(current-date()) >= day-from-date($Birthday)">
                  <xsl:value-of select="year-from-date(current-date()) - year-from-date($Birthday)" />
              </xsl:when>
              <xsl:otherwise>
                  <xsl:value-of select="year-from-date(current-date()) - year-from-date($Birthday) - 1" />
              </xsl:otherwise>
          </xsl:choose>
      </xsl:variable>
      

      【讨论】:

        猜你喜欢
        • 2014-04-04
        • 2018-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多