Skip to main content

Some useful js methods

/**
 * remove url from browser address bar without page refresh / works only with html5 enabled browser it seems
 */
    var uri = window.location.toString();

    if (uri.indexOf("?") > 0) {
        var clean_uri = uri.substring(0, uri.indexOf("?"));
        window.history.replaceState({}, document.title, clean_uri);

    }


/**
 * To convert utc to est
 */
function convertUTCtoEST(utcDate) {
var offsetValue = 5;
       var dateTime ="";
       try{
              if(utcDate != undefined && utcDate != null && Trim(utcDate).length > 0){
                     var estDate = new Date(utcDate);
                     estDate.setHours(estDate.getHours() - offsetValue);
                     //formatting date time
                     dateTime = appendZero(estDate.getMonth() + 1) + "/" + appendZero(estDate.getDate()) + "/"
                                  + estDate.getFullYear() + " " + appendZero(estDate.getHours()) + ":"
                                  + appendZero(estDate.getMinutes());
              }
              else{
                     dateTime ="";
              }
       }
       catch(err){
       }
       return utcDate;
}

Convert string to date

//function to split
function ConvertToDate(dateValue) {
    var dateValueDetail = dateValue.split('-');
    var dateYear = dateValueDetail[0];
    var dateMonth = dateValueDetail[1];
    var dateDay = dateValueDetail[2];
    return new Date(dateYear, dateMonth - 1, dateDay);
}

Calculate difference b/w months

 //function to find number of months in between
function monthDiff(d1, d2) {
    var months;
    months = (d2.getFullYear() - d1.getFullYear()) * 12;
    months -= d1.getMonth() + 1;
    months += d2.getMonth();
    //till now months will have only the number of months in between start date and end date
    var diff = d1.getMonth() - d2.getMonth();
    if (diff > 2 || diff < -2) {
        months += 2;
    }
    return months;
}

Get/Set cookie

 //function to set cookie
function setCookie(c_name, value) {
    var c_value = escape(value);
    document.cookie = c_name + "=" + c_value + "; path=/";
}
//function to get cookie
function getCookie(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {
            return unescape(y);
        }
    }
}

Function to read query string

function readQueryString() {
    var qrStr = window.location.search;
    var spQrStr = qrStr.substring(1);
    var arrQrStr = new Array();
    var arr = spQrStr.split('&');
    var queryValue = null;

    for (var i = 0; i < arr.length; i++) {
        var queryvalue = arr[i].split('=');
        if (queryvalue[0] == "ID") { // ID - Query string key
            queryValue = queryvalue[1].replace("%20", " ").toLowerCase();
        }
    }
    if (queryValue != null && queryValue != "") {
        return queryValue;
    }
}

Manual triming of white spaces

function Trim(str, intialValue, lastValue) {
    while (str.substring(0, 1) == ' ') // check for white spaces from beginning
    {
        str = str.substring(1, str.length);
    }
    while (str.substring(str.length - 1, str.length) == ' ') // check white space from end
    {
        str = str.substring(0, str.length - 1);
    }
    if (str == intialValue || str == lastValue) {
        return "";
    }
    else {
        return str;
    }
}

Comments

Popular posts from this blog

Creating multi module project with maven

Creating my first multi module project with maven I got a superb step by step article for maven project creation in eclipse while googling. This article is good for beginners like me :) , no need do too many things, just follow the steps at the end you will see “Hello World” in the browser. Check the below link for the wonderful article. http://skillshared.blogspot.in/2012/11/how-to-create-multi-module-project-with.html Thanks Semika Loku Kaluge

Unable to convert MySQL date/time value to System.DateTime - Solved

Few days back I got an exception like 'Unable to convert Mysql date/time value to system.datetime' but this happened only after deploying and locally everything was working fine. After googling for sometimes found the reason that, it was due to the formatting difference between Mysql and C# also empty date value is causing some issue. Later found the below solution to fix'em. Add an extra property(Convert Zero Datetime = true) to connectino string. e.g server=localhost;User Id=root;password=pwd;database=test; Convert Zero Datetime=true Solution obtained from StackOverflow :)

Converting String to a Executable line in JAVA

I've been searching for converting a string into a executable line so that I could process it... after searching for sometime, as usual I got a solution in the name of BeanShell scripting awesome one... This is another scripting language built with Java, which is powerful and was able to solve my need... This is the link for it : http://www.beanshell.org/manual/bshmanual.html#Download_and_Run_BeanShell Hope this helps you as well... If anyone requires any help on this, a basic one ;) ... sure post a comment, will help if I could...