Sunday, September 6, 2015

Date formatting at Client side using Javascript

Date formatting at Client side using Javascript

Friends, As we know many times we depends upon server side code, to format date, I have one date function which is customized and made in such way that , you can format date to any format as you wish.

For me I found its an awesome & great function, makes life easier, I thought why should not I share it for you..

Lets enjoy.. 

Include it in the master page or in a place from where you can access globally in the website.


 /*Awesome, third Party Date Function - Included By Ilyas*/
/*   Keyword      Definition
            dd           Date
            DDD          Short name of day like ‘Mon’
            DDDD         Full name of day ‘Monday’
            MM           Integer month
            MMM          Short name of month like ‘Jan’
            MMMM         Full name of month like ‘January’
            yy           Short year
            yyyy         Full year
            mm           Minutes
            hh           Hours
            ss           Seconds
            ms           Milleseconds*/
/*Usage: new Date().format("dd-MMM-yy") ==> 09-Sept-15*/

Date.prototype.format=function(format)
{
var mon=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var fullMon=['January','February','March','April','May','June','July','August','September','October','November','December'];
var days=['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
var fullDays=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
if (!format) { format="MM/dd/yyyy"; }
var  regex=new RegExp("DDDD","g");
format=format.replace(regex, fullDays[this.getDay()]);
regex=new RegExp("DDD","g");
format=format.replace(regex, days[this.getDay()]);
regex=new RegExp("dd","g");
format=format.replace(regex,this.getDate());
regex=new RegExp("MMMM","g");
format=format.replace(regex, fullMon[this.getMonth()]);
regex=new RegExp("MMM","g");
format=format.replace(regex, mon[this.getMonth()]);
regex=new RegExp("MM","g");
format=format.replace(regex,this.getMonth()+1);
regex=new RegExp("yyyy","g");
format=format.replace(regex, this.getFullYear());
regex=new RegExp("yy","g");
format=format.replace(regex, this.getFullYear().toString().substr(2,2));
regex=new RegExp("mm","g");
format=format.replace(regex, this.getMinutes());
regex=new RegExp("hh","g");
format=format.replace(regex, this.getHours());
regex=new RegExp("ss","g");
format=format.replace(regex, this.getSeconds());
regex=new RegExp("ms","g");
format=format.replace(regex, this.getMilliseconds());
return format;
}


Usage:

for example I need my Date in dd-MMM-yy format
You can now get it as : new Date().format("dd-MMM-yy") =  09-Sept-15

I have date & I want to modify it in  this format : MMM dd, yyyy
new Date(“9/15/2012”).format(“MMM dd, yyyy”) = Sep 15, 2012

In the similar way you can use it for different date formats, by just changing the keywords..:)
I hope it's useful, don't forget to give suggestions or feedback in the box below :).



No comments: