Posted by: prajapatinilesh | December 18, 2007

Get Date using javascript

var now = new Date();

var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
var monthnumber = now.getMonth();
var monthday = now.getDate();
var year = now.getFullYear();

——————————————

<script type=”text/javascript”>
var d=new Date();
document.write(“<br>”+”The date is “+ d.toLocaleDateString()+”<br>”);
document.write(“and the time is “+ d.toLocaleTimeString());
</script>

——————————————–

Write the day using JavaScript

<script type="text/javascript">
today=new Date(); // Initialize Date in raw form
day = today.getDay(); // Get the day in number form (0,1,2,3,etc.)

// Make day number value correspond to actual day name
var dayName=new Array(7)
dayName[0]=”Sunday”;
dayName[1]=”Monday”;
dayName[2]=”Tuesday”;
dayName[3]=”Wednesday”;
dayName[4]=”Thursday”;
dayName[5]=”Friday”;
dayName[6]=”Saturday”;

document.write (dayName[day]); // Write the day
</script>

———————————–

In JavaScript, the date() object stores the current date in raw form. We can write the date in its raw form by using the following code:

<script type="text/javascript">document.write(date());</script>

The output looks like this:

Sun Nov 27 14:10:51 EST 2005

<script type="text/javascript">
today=new Date(); // Initialize Date in raw form
date=today.getDate(); // Get the numerical date
year=today.getYear(); // Get the year
day = today.getDay(); // Get the day in number form (0,1,2,3,etc.)
month=today.getMonth()+1; // Get the month

// Make day number value correspond to actual day name
var dayName=new Array(7)
dayName[0]=”Sunday”;
dayName[1]=”Monday”;
dayName[2]=”Tuesday”;
dayName[3]=”Wednesday”;
dayName[4]=”Thursday”;
dayName[5]=”Friday”;
dayName[6]=”Saturday”;

// Add suffix to date (1st, 2nd, 4th, etc.)
if (date==1) suffix=(“st”);
else if (date==2) suffix=(“nd”);
else if (date==3) suffix=(“rd”);
else if (date==21) suffix=(“st”);
else if (date==22) suffix=(“nd”);
else if (date==23) suffix=(“rd”);
else if (date==31) suffix=(“st”);
else suffix=(“th”);

// Make month number correspond to month name
if (month==1) monthName=(“January”);
else if (month==2) monthName=(“February”);
else if (month==3) monthName=(“March”);
else if (month==4) monthName=(“April”);
else if (month==5) monthName=(“May”);
else if (month==6) monthName=(“June”);
else if (month==7) monthName=(“July”);
else if (month==8) monthName=(“August”);
else if (month==9) monthName=(“September”);
else if (month==10) monthName=(“October”);
else if (month==11) monthName=(“November”);
else monthName=(“December”);

// Write date
document.write(dayName[day] + “, ” + monthName + ” ” + date + suffix + “, ” + year);
</script>

—————————–

Method Description Example
getFullYear () Retrieves the year component of the date as a 4-digit number. y = x.getFullYear ()
getMonth() Retrieves the month component of the date as a number from 0 to 11 (0=January, 1=February, etc) y = x.getMonth()
getDate() Retrieves the day-of-month component of the date as a number from 1 to 31 y = x.getDate()
getDay() Retrieves the day of the week component of the date as a number from 0 to 6 (0=Sunday, 1=Monday, etc) y = x.getDay()
getHours() Retrieves the hours component of the date as a number from 0 to 23 y = x.getHours()
getMinutes() Retrieves the minutes component of the date as a number from 0 to 59 y = x.getMinutes()
getSeconds() Retrieves the seconds component of the date as a number from 0 to 59 y = x.getSeconds()
getMilliseconds() Retrieves the milliseconds component of the date as a number from 0 to 999 y = x.getMilliseconds()
getTimezoneOffset() Retrieves the time difference, in minutes, between the computer’s local time and GMT
Method Description Example
getUTCFullYear () Retrieves the year component of the date as a 4-digit number. y = x.getUTCFullYear ()
getUTCMonth() Retrieves the month component of the date as a number from 0 to 11 (0=January, 1=February, etc) y = x.getUTCMonth()
getUTCDate() Retrieves the day-of-month component of the date as a number from 1 to 31 y = x.getUTCDate()
getUTCDay() Retrieves the day of the week component of the date as a number from 0 to 6 (0=Sunday, 1=Monday, etc) y = x.getUTCDay()
getUTCHours() Retrieves the hours component of the date as a number from 0 to 23 y = x.getUTCHours()
getUTCMinutes() Retrieves the minutes component of the date as a number from 0 to 59 y = x.getUTCMinutes()
getUTCSeconds() Retrieves the seconds component of the date as a number from 0 to 59 y = x.getUTCSeconds()
getUTCMilliseconds() Retrieves the milliseconds component of the date as a number from 0 to 999 y
Method Description Example
setFullYear() Sets the year component of the date using a 4-digit number. x.setFullYear(2002)
setMonth() Sets the month component of the date using a number from 0 to 11 (0=January, 1=February, etc) x.setMonth(11)
setDate() Sets the day-of-month component of the date using a number from 1 to 31 x.setDate(17)
setHours() Sets the hours component of the date using a number from 0 to 23 x.setHours(20)
setMinutes() Sets the minutes component of the date using a number from 0 to 59 x.setMinutes(45)
setSeconds() Sets the seconds component of the date using a number from 0 to 59 x.setSeconds(0)
setMilliseconds() Sets the milliseconds component of the date using a number from 0 to 999 x.setMilliseconds(500)
Method Description Example
setUTCFullYear() Sets the year component of the date using a 4-digit number. x.setUTCFullYear(2002)
setUTCMonth() Sets the month component of the date using a number from 0 to 11 (0=January, 1=February, etc) x.setUTCMonth(11)
setUTCDate() Sets the day-of-month component of the date using a number from 1 to 31 x.setUTCDate(17)
setUTCHours() Sets the hours component of the date using a number from 0 to 23 x.setUTCHours(20)
setUTCMinutes() Sets the minutes component of the date using a number from 0 to 59 x.setUTCMinutes(45)
setUTCSeconds() Sets the seconds component of the date using a number from 0 to 59 x.setUTCSeconds(0)
setUTCMilliseconds() Sets the milliseconds component of the date using a number from 0 to 999
Method Description Example
parse() Converts a date text string to the “milliseconds since midnight Jan 1, 1970” format. You call it using Date.parse() rather than object.parse(). x = Date.parse
("January 6, 1972")
UTC(y, m, d, h, m, s) Converts the specified date to the “milliseconds since midnight Jan 1, 1970” format. You call it using Date.UTC() rather than object.UTC(). x = Date.UTC(1972, 0, 6)

x = Date.UTC
(1972, 0, 6, 16, 5, 0)

toString() Outputs the value of the Date object as a date text string. y = x.toString()
toUTCString() Outputs the value of the Date object as a date text string, using UTC. y = x.toUTCString()
toLocaleString() Outputs the value of the Date object as a date text string, using the computer’s locale conventions (e.g. D/M/Y for UK computers). y = x.toLocaleString()
toDateString() Outputs the “date” portion of the Date object as a text string. y = x.toDateString()
toTimeString() Outputs the “time” portion of the Date object as a text string. y = x.toTimeString()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: