Tag Archive for date

Format a date as desired in a LINQ query assigned object

Created = String.Format("{0:M/d/yyyy}", cn.Created)

source

ISO 8601 javascript Time String Conversion

/* formatted ISO 8601 timestrings-

2010-06-21T10:41:37.132-04:00
2010-06-21T14:41:37Z
2010-06-21
*/
Date.fromISO= function(s){
	var day, tz, rx=  /^(d{4}-dd-dd([tT][d:.]*)?)([zZ]|([+-])(dd):(dd))?$/, p= rx.exec(s) || [];
	if(p[1]){
		day= p[1].split(/D/).map(function(itm){
			return parseInt(itm, 10) || 0;
		});
		day[1]-= 1;
		day= new Date(Date.UTC.apply(Date, day));
		if(!day.getDate()) return NaN;
		if(p[5]){
			tz= parseInt(p[5], 10)*60;
			if(p[6]) tz += parseInt(p[6], 10);
			if(p[4]== "+") tz*= -1;
			if(tz) day.setUTCMinutes(day.getUTCMinutes()+ tz);
		}
		return day;
	}
	return NaN;
}
if(![].map){
	Array.prototype.map= function(fun, scope){
		var L= this.length, A= Array(this.length), i= 0, val;
		if(typeof fun== 'function'){
			while(i< L){
				if(i in this){
					A[i]= fun.call(scope, this[i], i, this);
				}
				++i;
			}
			return A;
		}
	}
}

source

Java Date Detection

<% java.util.Date today = new java.util.Date(); java.util.Date ad = new java.util.Date("May 25, 2010 12:00 AM");%>
<% if (today.compareTo(ad) > 0){ %>
  Displays after date
<%} else {%>
  Displays before date
<% } %>

source

Add st, nd, rd, th to days :D

function fday($time) {
	$day = date('d', $time);
	if ( substr($day, -1) == 1 ):
		$end = 'st';
	elseif ( substr($day, -1) == 2 ):
		$end = 'nd';
	elseif ( substr($day, -1) == 3 ):
		$end = 'rd';
	else:
		$end = 'th';
	endif;
	
	return date('F, d', $time).$end.date(' Y', $time);;
}

Usage:
$time = time();
echo fday($time);

source

Date difference

Calendar calendar1 = Calendar.getInstance();
				Calendar calendar2 = Calendar.getInstance();
				DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date date = new Date();
				try {
					date = formatter.parse(trailLoginDate);
				} catch (Exception e) {
					// TODO: handle exception
				}
				calendar1.setTime(date);
				calendar2.setTime(new Date());
				long milliseconds1 = calendar1.getTimeInMillis();
				long milliseconds2 = calendar2.getTimeInMillis();
				long diff = milliseconds2 - milliseconds1;
				long diffDays = diff / (24 * 60 * 60 * 1000);
				days = (int)diffDays;

source

Increment a date by a number of business (week) days

incrementBusinessDays <- function(x,n) {
	x <- as.Date(x)
	bWeeks <- floor(n/5)
	bDays <- n %% 5
	x <- (x + bWeeks*7)
	wd <- as.POSIXlt(x)$wday + bDays
	
	# Need to wrap around the weekend
	if(wd < 1) {
		x <- x + bDays - 2
	} else if(wd > 5) {
		x <- x + bDays +2
	} else {
		x <- x + bDays
	}
	
	x
}

source

Convert Shorthand Date to Long Form

list($day, $month, $year) = split('/', $date);
echo date('l, F jS ',  mktime(0, 0, 0, $month, $day, $year));

source

Function to convert dates to and from Mysql

<?php

/**
* function dateconvert
*
* dateconvert is a handy function to take the aches and pains out of mysqls stupidity
* by converting data from a variable (posted from a form or just stored)
* into a format mysql will be able to store and converting the
* database date back into the british standard of date month year.
* The Script accepts day.month.year or day/month/year or day-month-year.
* example:
*
* <code>
* <?php // using type 1
* $date = "19.12.2005";
* $date = dateconvert($date, 1);
* echo $date; // Would echo 2005-12-19 which is the format stored by mysql
* ?>
* </code>
* <code>
* <?php // using type 2
* $date = $row['date']; //your mysql date
* $realdate = dateconvert($date,2);
* echo $realdate; // would display 19/12/2005
* ?>
* </code>
*
* @author Chris McKee <<a href="mailto:pcdevils@gmail.com">pcdevils@gmail.com</a>>
*
* @param string $date - Date to be converted
* @param string $func - which function is to be used (1 for input to mysql, 2 for output from mysql)
*/
function dateconvert($date,$func) {
if ($func == 1){ //insert conversion
list($day, $month, $year) = split('[/.-]', $date);
$date = "$year-$month-$day";
return $date;
}
if ($func == 2){ //output conversion
list($year, $month, $day) = split('[-.]', $date);
$date = "$day/$month/$year";
return $date;
}

}

?>

source

Date and Time Difference calculates in PHP

/**
 * Calculates the differences between two date
 * 
 * @param date $date1
 * @param date $date2
 * @return array
 */
function dateDifference($date1, $date2)
{
    $d1 = (is_string($date1) ? strtotime($date1) : $date1);
    $d2 = (is_string($date2) ? strtotime($date2) : $date2);

    $diff_secs = abs($d1 - $d2);
    $base_year = min(date("Y", $d1), date("Y", $d2));

    $diff = mktime(0, 0, $diff_secs, 1, 1, $base_year);

    return array
    (
        "years"                 => abs(substr(date('Ymd', $d1) - date('Ymd', $d2), 0, -4)),
        "months_total"  => (date("Y", $diff) - $base_year) * 12 + date("n", $diff) - 1,
        "months"                => date("n", $diff) - 1,
        "days_total"    => floor($diff_secs / (3600 * 24)),
        "days"                  => date("j", $diff) - 1,
        "hours_total"   => floor($diff_secs / 3600),
        "hours"                 => date("G", $diff),
        "minutes_total" => floor($diff_secs / 60),
        "minutes"               => (int) date("i", $diff),
        "seconds_total" => $diff_secs,
        "seconds"               => (int) date("s", $diff)
    );
}

source

SimpleDateFormat

java.text.SimpleDateFormat dateFormatter = new java.text.SimpleDateFormat("dd.MM.yyyy");
dateFormatter.format(gebdat);

source