首先,你應該知道,表達日期差異在幾年,幾個月和幾天是非常沒有意義的。
年數和月數沒有固定的天數,因此兩次計算的測量持續時間相等的計算時間可能很容易以年,月和日的形式返回不同的結果。
出於同樣的原因,沒有建立的算法來計算年,月和日的日期差異。
所有這一切說,如果你想顯示在年,月,日的經過時間 - 作爲一個例證,例如 - 你可以用下面的方法:
<xsl:template name="date-diff-in-ymd">
<xsl:param name="start-date"/>
<xsl:param name="end-date"/>
<xsl:variable name="start-year" select="year-from-date($start-date)"/>
<xsl:variable name="start-month" select="month-from-date($start-date)"/>
<xsl:variable name="start-day" select="day-from-date($start-date)"/>
<xsl:variable name="end-year" select="year-from-date($end-date)"/>
<xsl:variable name="end-month" select="month-from-date($end-date)"/>
<xsl:variable name="end-day" select="day-from-date($end-date)"/>
<xsl:variable name="elapsed-months" select="12*($end-year - $start-year) + $end-month - $start-month - number($end-day lt $start-day)"/>
<xsl:variable name="birthday" select="$start-date + xs:yearMonthDuration(concat('P0Y', $elapsed-months, 'M'))"/>
<xsl:variable name="years" select="$elapsed-months idiv 12"/>
<xsl:variable name="months" select="$elapsed-months mod 12"/>
<xsl:variable name="days" select="days-from-duration($end-date - $birthday)"/>
<xsl:value-of select="concat($years, ' years, ', $months, ' months and ', $days, ' days')" />
</xsl:template>
例的電話號碼:http://xsltransform.net/a9GiwM
只是爲了確認輸入格式中的'-07:00'部分是什麼時區偏移? –
這就是hh:mm,是的,這是從UTC時間開始的時區偏移 – user3836593
您可以構建一個'xs :date'直接來自你的輸入格式,例如'xs:date('2017-06-01-07:00')',你也可以減去'current-date()div xs:date('2017-06-01 -07:00')'來獲得一個持續時間,但是這是一種不同的格式。 –