Showing posts with label Date. Show all posts
Showing posts with label Date. Show all posts

MySQL: Calculate DATE, TIME difference in SQL statement using MySQL DB

Following examples showing how to calculated DATE, TIME differences in SQL statement in MySQL database.

Functions used in the following examples are TIMEDIFF and DATEDIFF

 select TIMEDIFF('2011-10-31 12:50:56' , '2011-09-23 12:50:21')   
 The above outputs : 838:59:59  
 select TIMEDIFF('15:50:56' , '12:55:21')   
 The above SQL outputs: 02:55:35  
 select DATEDIFF('2011-10-31 12:50:56' , '2011-09-23 12:50:21')   
 The above SQL outputs: 38  
 select DATEDIFF('2011-10-31' , '2011-09-23')   
 The above SQL outputs: 38  

PHP: Today, Yesterday, Tomorrow dates

In PHP, we can generate Today, Yesterday, Tomorrow Dates the following way:
 $today = date('Y-m-d');  
 $tomorrow = date('Y-m-d', strtotime('tomorrow'));  
 $yesterday = date('Y-m-d', strtotime('yesterday'));  

Date formatting in JavaScript

We would be using 3 different methods to get the current system date, month and year.
  • getDate() : Returns the date
  • getMonth(): Returns the month
  • getFullYear(): Returns the year
#1: month/date/year (5/12/2010)

var date = new Date();
var sysDate = date.getDate();
var sysMonth = date.getMonth();
var sysYear = date.getFullYear();

document.write(sysMonth + "/" + sysDate + "/" + sysYear);

#2: date-month-year (12-May-2010)

var date = new Date();
var sysDate = date.getDate();
var sysMonth = date.getMonth();
var sysYear = date.getFullYear();

var monthNames = new Array("January", "February", "March",
               "April", "May", "June", "July", "August", 
               "September", "October", "November", 
               "December");

document.write(sysDate + "-" + monthNames[sysMonth] + "-" + sysYear);

#3: date-month-year (12th May 2010)

var date = new Date();
var sysDate = date.getDate();
var sysMonth = date.getMonth();
var sysYear = date.getFullYear();

var monthNames = new Array("January", "February", "March",
               "April", "May", "June", "July", "August", 
               "September", "October", "November", 
               "December");

var sup = "";

if(sysDate == 1 || sysDate == 21 || sysDate == 31) sup ="st";
else if (sysDate == 2 || sysDate == 22) sup = "nd";
else if (sysDate == 3 || sysDate == 23) sup = "rd";
else sup = "th";

document.write(sysDate + "" + sup + " " + monthNames[sysMonth] + " " + sysYear);

Python contextlib for Timing Python code

If you've ever found yourself needing to measure the execution time of specific portions of your Python code, the `contextlib` module o...