Date to DMY/YMD converter
Converts JavaScript Date into DMY/YMD formats and vide versa
by Stjepan Brbot
JavaScript
function pad2(n)
{
return (n < 10 ? '0' : '') + n;
}
Date.prototype.toYMD=function()
{
return String(this.getFullYear()+"-"+pad2(this.getMonth()+1)+"-"+pad2(this.getDate()));
};
Date.prototype.toDMY=function()
{
return String(pad2(this.getDate())+"."+pad2(this.getMonth()+1)+"."+this.getFullYear());
};
Date.prototype.fromYMD=function(ymd)
{
var t=ymd.split(/[- :]/);
return new Date(t[0],t[1]-1,t[2],t[3]||0,t[4]||0,t[5]||0);
};
Date.prototype.fromDMY=function(dmy)
{
var t=dmy.split(/[. :]/);
return new Date(t[2],t[1]-1,t[0],t[3]||0,t[4]||0,t[5]||0);
};
function DateFromYMD(ymd)
{
return (new Date()).fromYMD(ymd);
}
function DateFromDMY(dmy)
{
return (new Date()).fromDMY(dmy);
}
alert(new DateFromYMD('2016-07-24'));
alert(new DateFromDMY('24.07.2016'));
alert((new Date()).toYMD());
alert((new Date()).toDMY());