Client Machine Date and Time

Some times we need to find out the client’s machine time or date or both. We may need to display theĀ  date/time in the browser, or need to display the appropriate welcome wish, like “Good Morning” or “Good After Noon” etc.

Getting the Client’s machine date and times is very easy and it all need to write a small JavaScript function.

The following example will display the current date and time in the two lable controls.

<html xmlns=””&gt;
<head runat=”server”>
<title>Date and Time on Page Load</title>
<script language=”javascript” type=”text/javascript”>
function DateTime()
var date=new Date();
var day=date.getDate();
var month=date.getMonth()+1;
var year=date.getYear();
var seconds=date.getSeconds();
var minutes=date.getMinutes();
var hours=date.getHours();
document.getElementById(‘lblDate’).innerHTML=”Current Date: “+day +”/”+ month+”/”+year;
document.getElementById(‘lblTime’).innerHTML=”Current Time: “+hours+”:”+minutes+”:”+seconds;
<body onload=”DateTime()”>
<form id=”form1″ runat=”server”>
<asp:ScriptManager ID=”ScriptManager1″ runat=”server” />
<asp:Label ID=”lblDate” runat=”server”></asp:Label><br />
<asp:Label ID=”lblTime” runat=”server”></asp:Label><br />

Calculate Age from Date-of-Birth

Here is a simple function to calculate the Age, if we have the date of birth.

public static int CalculateAge(DateTime birthDate)
// Take the current time
DateTime now = DateTime.Today;
// Find the difference in years
int years = now.Year – birthDate.Year;
// subtract one year if we’re before the birth day in the current year
if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day))
return years;