Wednesday, 2 October 2013

Java Script usefull method

1. JavaScript for Date and Time Formating
//function for the getting the formatted date and time 
// current output June 26, 2012 4:04 PM

function formattedUTCDateTime(d) {

    // code for the year
    var 
m_names = new Array("January", "February", "March",
    "April", "May", "June", "July", "August", "September",
    "October", "November", "December");

//    var 
d = new Date();
    var 
curr_date = d.getUTCDate();
    var 
sup = "";


else if (
curr_date == 3 || curr_date == 23) {
        
sup = "rd";
    }

    if (
curr_date == 1 || curr_date == 21 || curr_date == 31) {
        
sup = "st";
    }
    else if (
curr_date == 2 || curr_date == 22) {
        
sup = "nd";
    }
 
    else {
        
sup = "th";
    }

    var 
curr_year = d.getUTCFullYear();
    var 
curr_month = d.getUTCMonth();


    //code for the time  
    var 
hours = d.getUTCHours()
    var 
minutes = d.getUTCMinutes()

    var 
suffix = "AM";
    if (
hours >= 12) {
        
suffix = "PM";
        
hours = hours - 12;
    }
    if (
hours == 0) {
        
hours = 12;
    }

    if (
minutes < 10)
        
minutes = "0" + minutes;

    var 
datetimevalue = (m_names[curr_month] + " " + curr_date + ", " + curr_year + "  " + hours + ":" + minutes + " " + suffix);
    return datetimevalue;

}



No comments:

Post a Comment