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

« Newer Snippets
Older Snippets »
Showing 1-8 of 8 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

Calculate last day of current Month

'Determines what the next month is based on today and subtracts 1 day from first day of next month.

'VB.NET
Dim NextMonth As Integer
Dim RptYear As Integer
'Determine next month
NextMonth = DatePart(DateInterval.Month, DateAdd(DateInterval.Month, +1, today))
'Determine the year of the next month, in case you are going from Dec to Jan
RptYear = DatePart(DateInterval.Year, DateAdd(DateInterval.Month, +1, today))
'Subtract 1 day from the first day of next month to get this months last day
Return DateAdd(DateInterval.Day, -1, DateValue(NextMonth.ToString & "/1/" & RptYear.ToString))

isWorkingDay?

// description of your code here

  public boolean isWorkingDay(Date date) {
        Calendar cal = GregorianCalendar.getInstance();
        cal.setTime(date);
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); 
        if ((dayOfWeek  Calendar.SATURDAY) || (dayOfWeek  Calendar.SUNDAY)) {
            return false;
        } 
        return true;
   }

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.

days in month

// returns number of days in specified month

def days_in_month(month)
  (Date.new(Time.now.year,12,31).to_date<<(12-month)).day
end

batch set day / date / time variables

// set day / day / time variables in batch
cd %temp%
echo.|date>datevar.bat
echo.|time>timevar.bat
echo set DATEvar=%%4>current.bat
call datevar
echo set DAYvar=%%3>current.bat
call datevar
echo set TIMEvar=%%3>current.bat
call timevar
echo %DAYvar%
echo %DATEvar%
echo %TIMEvar%

Determining the Number of Days in a Month with Javascript

Essentially, writing some code to determine the number of days in a given month of a given year with javascript is not the worlds most difficult task. It is the type of exercise that one would expect to be given as a newbie developer during a lab or lecture. The solution normally involves determining if the month is February, an month with 30 days or a month with 31 days, then (if February) checking if the year is a leap year. All these tests add up, however, and add several lines of code to your .js file. They are also unnecessary!

Apparently, the javascript Date function allows you to overflow the day number parameter that you pass, creating a date in the next month. Deliberately overflowing the day parameter and checking how far the resulting date overlaps into the next month is a quick way to tell how many days there were in the queried month. Here is a function that does this:

function daysInMonth(iMonth, iYear)
{
	return 32 - new Date(iYear, iMonth, 32).getDate();
}


N.B.: iMonth is zero based, so 0 represents January, 1 represents February, 2 represents March and 11 represents December. iYear is not zero based, this is the actual calendar year number. (2006 is actually 2006)

Pray that the browser developers know the correct way to determine whether a year is a leap year! (It's more complicated than a simple mod 4 == 0) Here is a quote from Wikipedia's page on leap years: "The Gregorian calendar, the current standard calendar in most of the world, adds a 29th day to February in all years evenly divisible by 4, except for centennial years (those ending in '00'), which receive the extra day only if they are evenly divisible by 400. Thus 1600, 2000 and 2400 are leap years but 1700, 1800, 1900 and 2100 are not."

To test this function, February in the years 2100, 2005, 2004, 2003, 2001, 2000 and 1999 should be checked. All of these should return 28, except for 2004 and 2000.

<button onclick="alert(daysInMonth(1, 2100));">February 2100</button>
<button onclick="alert(daysInMonth(1, 2005));">February 2005</button>
<button onclick="alert(daysInMonth(1, 2004));">February 2004</button>
<button onclick="alert(daysInMonth(1, 2003));">February 2003</button>
<button onclick="alert(daysInMonth(1, 2001));">February 2001</button>
<button onclick="alert(daysInMonth(1, 2000));">February 2000</button>
<button onclick="alert(daysInMonth(1, 1999));">February 1999</button>


How does this function work? It is quite simple. When the Date() function is given a day number that is greater than the number of days in the given month of the given year, it wraps the date into the next month. The getDate() function returns the day of the month, starting from the beginning of the month that the date is in. So, day 32 of March is considered to be day 1 of April. Subtracting 1 from 32 gives the correct number of days in March!

DateTime: generic date and time script in perl

### begin_: file metadata
    ### <region-file_info>
    ### main:
    ###   - name : DateTime.pl
    ###     desc : DateTime: generic date and time script in perl
    ###     date : created="Thu 2005-12-01 10:04:52"
    ###     last : lastmod="Thu 2005-12-01 10:04:59"
    ### </region-file_info>

### begin_: initialize perl (optional)
    use strict;
    use warnings;

### begin_: initialize DateTime values
    my %dttime = ();
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

### begin_: initialize DateTime number formats
    $dttime{year }  = sprintf "%04d",($year + 1900);  ## four digits to specify the year
    $dttime{mon  }  = sprintf "%02d",($mon + 1);      ## zeropad months
    $dttime{mday }  = sprintf "%02d",$mday;           ## zeropad day of the month
    $dttime{wday }  = sprintf "%02d",$wday + 1;       ## zeropad day of week; sunday = 1;
    $dttime{yday }  = sprintf "%02d",$yday;           ## zeropad nth day of the year
    $dttime{hour }  = sprintf "%02d",$hour;           ## zeropad hour
    $dttime{min  }  = sprintf "%02d",$min;            ## zeropad minutes
    $dttime{sec  }  = sprintf "%02d",$sec;            ## zeropad seconds
    $dttime{isdst}  = $isdst;

### begin_: xnpDate print iso8601 version date
    print "$dttime{year}-$dttime{mon}-$dttime{mday}\n";

### begin_: xnpNow show system time
    print "$dttime{year}-$dttime{mon}-$dttime{mday} $dttime{hour}:$dttime{min}:$dttime{sec} \n";



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