Friday, January 23, 2009

JavaScript : Get date in format (dd/mm/yyyy). Get today's date, tomorrow's date, yesterday's date, first and last date of week & Month

Here in this code u can get Date in format dd/mm/yyyy, Aslo u can customize its default view accordingly. Get today's date, tomorrow's date, yesterday's date, Also u can get first and last date of current week and first and last date of current month.


<HTML>
<HEAD>
<TITLE>Java Script Date Example</TITLE>
<script type="text/javascript">
function showDate()
{
var type = document.myForm.type.value;//get request type

var today = new Date();//get today's date

var yesterday = new Date();;
yesterday.setDate(today.getDate() - 1);//get yesterday's date

var tomorrow = new Date();
tomorrow.setDate(today.getDate() + 1);//get tomorrow's date

//get first and last day of week
var day = today.getDay();//returns 0-6 accordingly
var fistDayOfWeek;
var lastDayOfWeek;

//assumeing monday is first day and sunday is last day
switch (day)
{
case 0:
fistDayOfWeek = new Date();
fistDayOfWeek.setDate(today.getDate() + 7);
lastDayOfWeek = new Date();
//lastDayOfWeek.setDate(today.getDate() + 0);
break;
case 1:
fistDayOfWeek = new Date();
//fistDayOfWeek.setDate(today.getDate() - 0);
lastDayOfWeek = new Date();
lastDayOfWeek.setDate(today.getDate() + 6);
break;
case 2:
fistDayOfWeek = new Date();
fistDayOfWeek.setDate(today.getDate() - 1);
lastDayOfWeek = new Date();
lastDayOfWeek.setDate(today.getDate() + 5);
break;
case 3:
fistDayOfWeek = new Date();
fistDayOfWeek.setDate(today.getDate() - 2);
lastDayOfWeek = new Date();
lastDayOfWeek.setDate(today.getDate() + 4);
break;
case 4:
fistDayOfWeek = new Date();
fistDayOfWeek.setDate(today.getDate() - 3);
lastDayOfWeek = new Date();
lastDayOfWeek.setDate(today.getDate() + 3);
break;
case 5:
fistDayOfWeek = new Date();
fistDayOfWeek.setDate(today.getDate() - 4);
lastDayOfWeek = new Date();
lastDayOfWeek.setDate(today.getDate() + 2);
break;
case 6:
fistDayOfWeek = new Date();
fistDayOfWeek.setDate(today.getDate() - 5);
lastDayOfWeek = new Date();
lastDayOfWeek.setDate(today.getDate() + 1);
break;
}

//get first and last day of month
var month = today.getMonth();
var year = today.getYear();

var fdtm = new Date(year, month, 1);//first day of month

var ldtm = new Date(year, month + 1, 0);//last day of month

//Show Result depending upon type
if(type == 'today')
{
document.getElementById("displayResult").innerHTML = "Today's Date : "+today.defaultView();
}
else if(type == 'yesterday')
{
document.getElementById("displayResult").innerHTML = "Yesterday's Date : "+yesterday.defaultView();

}
else if(type == 'tomorrow')
{
document.getElementById("displayResult").innerHTML = "Tomorrow's Date : "+tomorrow.defaultView();
}
else if(type == 'week')
{
document.getElementById("displayResult").innerHTML = "Ist Day of Week : "+fistDayOfWeek.defaultView()+" Last Day of week : "+lastDayOfWeek.defaultView();
}
else if(type == 'month')
{
document.getElementById("displayResult").innerHTML = "Ist Day of Month : "+fdtm.defaultView()+" Last Day of Month : "+ldtm.defaultView();
}
}

//customize function used for default view
Date.prototype.defaultView=function()
{
var dd=this.getDate();
if(dd<10)dd='0'+dd;
var mm=this.getMonth()+1;
if(mm<10)mm='0'+mm;
var yyyy=this.getFullYear();

var y = yyyy % 100;
y += (y < 38) ? 2000 : 1900;
//return y;


//alert(y);
return String(dd+"\/"+mm+"\/"+y)
}
</script>
</HEAD>
<BODY>
<CENTER>
<h3>Simple Java Script Date Example</h3>
<form action="#" name="myForm">
Select
<select name="type" onchange="showDate()">
<option value="today">today</option>
<option value="yesterday">yesterday</option>
<option value="tomorrow">tomorrow</option>
<option value="week">this week</option>
<option value="month">this month</option>
</select>
</br></br>
<div id="displayResult">
</div>
</form>
</CENTER>
</BODY>
</HTML>

No comments:

Post a Comment