Monday, April 18, 2016

Useful date calculations for Javascript

Custom Date Functions for Javascript

Date.daysBetween01 = function( date1, date2 ) {
    //Get 1 day in milliseconds    var one_day=1000*60*60*24;

    // Convert both dates to milliseconds    var date1_ms = date1.getTime();
    var date2_ms = date2.getTime();

    // Calculate the difference in milliseconds    var difference_ms = date2_ms - date1_ms;

    // Convert back to days and return    return Math.round(difference_ms/one_day);
}

Working with PHP Dates

 // Calculate date from two datetime  
function days_between($str_date1, $str_date2)
{
    //Get 1 day in milliseconds
    $one_day = 1000 * 60 * 60 * 24;
    $date1 = strtotime($str_date1);
    $date2 = strtotime($str_date2);

    $difference = $date1 - $date2;
    $days =  $difference/$one_day;
    return $days;
}
 // Calculate hours from two datetime  
 function hours_between($str_date1, $str_date2)  
 {  
   //Get 1 day in milliseconds  
   $one_day = 1000 * 60 * 60 * 24;  
   $date1 = strtotime($str_date1);  
   $date2 = strtotime($str_date2);  
 //  Log::info($date1);  
 //  Log::info($date2);  
   $difference = $date1 - $date2;  
   $hours = $difference/3600;  
   Log::info($hours);  
   return $hours;  
 }  
 //Format mongo date to displayable date string  
 function parse_mongo_date($mongo_date)  
 {  
   if(is_a($mongo_date, 'MongoDate'))  
     $new_date =date('d-m-Y H:i:s',$mongo_date->sec);  
   else  
     $new_date = $mongo_date;  
   return $new_date;  
 }  
 function convert_to_mongo_date($date)  
 {  
   return new MongoDate(strtotime(date(DATE_ISO8601, $date->sec)));  
 //  new MongoDate(strtotime(date(DATE_ISO8601, $mongo_date->sec) - "30 days"));  
 }  

//Adding days to current date
$new_expiry_date =  strtotime(date("Y-m-d", strtotime("+30 days")));

Saturday, April 16, 2016

Javascript useful checks

Check whether the specific value is existed


if(typeof val.images != 'undefined' && val.images.hasOwnProperty('poster_url_300') != null) {
}