Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

James Robertson http://www.r0bertson.co.uk

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Increment a date using Ruby

This Ruby code converts a string into a date and increments the day, week, month, quarter or year.

def date_add(sdate='', unit='',i=0)

  sdate[/(\d+)\/(\d+)\/(\d+)\s(\d+):(\d+):(\d+)/]
  iyear = $3.to_i; imonth = $2.to_i; iday = $1.to_i; ihour = $4.to_i; imin = $5.to_i; isec = $6.to_i
  
  case  unit
    when 'days'
      t1 = Time.local(iyear,imonth,iday,ihour,imin,isec)
      t1 += (60 * 60 * 24 * i)
    when 'weeks'
      t1 = Time.local(iyear,imonth,iday,ihour,imin,isec)
      t1 += (60 * 60 * 24 * 7 * i) 
    when 'months'
      imonth += i
      if imonth < 12 then
        t1 = Time.local(iyear,imonth+i,iday,ihour,imin,isec)
      else
        t1 = Time.local(iyear+=1,imonth -12,iday,ihour,imin,isec)
      end
    when 'quarter'
      imonth += 3
      if imonth <= 12 then
        t1 = Time.local(iyear,imonth,iday,ihour,imin,isec)
      else
        t1 = Time.local(iyear+=1,imonth - 12,iday,ihour,imin,isec)
      end    
    when 'years'
      t1 = Time.local(iyear+i,imonth,iday,ihour,imin,isec)
    else
      raise 'not a valid date unit'
  end
  t1
end

date_add("17/03/2008 17:48:00",'months',2)

output: Sat May 17 17:48:00 +0100 2008

Formatting a date in XSLT

This template formats a date ie 060208171320 -> 06-Feb-08T17:13:20

  <xsl:template name="FormatDate">
    <xsl:param name="DateTime" />
    <!-- new date format 2006-01-14T08:55:22 -->
    <xsl:variable name="mo">
      <xsl:value-of select="substring($DateTime,3,2)" />
    </xsl:variable>
    <xsl:variable name="day">
      <xsl:value-of select="substring($DateTime,5,2)" />
    </xsl:variable>
    <xsl:variable name="year">
      <xsl:value-of select="substring($DateTime,1,2)" />
    </xsl:variable>
    <xsl:variable name="hh">
      <xsl:value-of select="substring($DateTime,7,2)" />
    </xsl:variable>
    <xsl:variable name="mm">
      <xsl:value-of select="substring($DateTime,9,2)" />
    </xsl:variable>
    <xsl:variable name="ss">
      <xsl:value-of select="substring($DateTime,11,2)" />
    </xsl:variable>
    <xsl:if test="(string-length($day) &lt; 2)">
      <xsl:value-of select="0"/>
    </xsl:if>
    <xsl:value-of select="$day"/>
    <xsl:value-of select="'-'"/>
    <xsl:choose>
      <xsl:when test="$mo = '01'">Jan</xsl:when>
      <xsl:when test="$mo = '02'">Feb</xsl:when>
      <xsl:when test="$mo = '03'">Mar</xsl:when>
      <xsl:when test="$mo = '04'">Apr</xsl:when>
      <xsl:when test="$mo = '05'">May</xsl:when>
      <xsl:when test="$mo = '06'">Jun</xsl:when>
      <xsl:when test="$mo = '07'">Jul</xsl:when>
      <xsl:when test="$mo = '08'">Aug</xsl:when>
      <xsl:when test="$mo = '09'">Sep</xsl:when>
      <xsl:when test="$mo = '10'">Oct</xsl:when>
      <xsl:when test="$mo = '11'">Nov</xsl:when>
      <xsl:when test="$mo = '12'">Dec</xsl:when>
    </xsl:choose>
    <xsl:value-of select="'-'"/>

    <xsl:value-of select="$year"/>
    <xsl:value-of select="'T'"/>
    <xsl:value-of select="$hh"/>
    <xsl:value-of select="':'"/>
    <xsl:value-of select="$mm"/>
    <xsl:value-of select="':'"/>
    <xsl:value-of select="$ss"/>
  </xsl:template>


copied from http://snipr.com/1z833 [geekswithblogs.net] and modified to suit the date input format I used.
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS