Home > Java Scripts Examples and Tutorials > Write the current date using JavaScript
Maldives president resigns amid protests
UNP also protest at Hulftsdorp
Indian Coast Guard nabs 5 Lankan fishing boats
SL wins toss
SF brought to court
Get well soon, SLC tells Yuvi
We need democracy plus; Ranil
White flag appeal postponed
UNP DNA join hands in protest
India still ‘world class’ insists Mathews
SL to market Ayurveda drugs
Nine hand grenades found near Aluth Kade Court
Hirunika to join active politics
IPL teams splash the cash for Mahela and Murali
Anoma to lead protest to free SF
US diplomats heading to Sri Lanka
CID probing double murder in Kahawatta
Dr. R.H.S. Samaratunga appointed Sec to Petroleum Ministry
A/L fiasco - Probe team’s mandate limited
Three including police officer injured in Maligawatta shootout
Are you in Love?
10 Top Sex Secrets Men won't Tell
Top 10 — Female Turn Ons
10 Commandments for good wives
How to live a happy and satisfied Life
Did I marry the right person?
Are You Happy
Alcohol makes men BETTER in the bedroom, scientists claim
Too busy for Love Making?
Have you gone off sex?
Love Isn't Blind
Are You A Lazy Lover?
5 Secret Relationship Resolutions
Are You Getting Attracted To Another Person?
Do you feel loved?
Write the current date using JavaScript
Using the date() object in JavaScript, we can print the current date in a variety of popular formats.
The JavaScript date() object
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
As shown in this example, the output of the date() object is not very "clean." In most cases, we would want to output the date in a more user-friendly format. To achieve this, we initialize the date() object (as done above), then modify the output as needed using built-in methods of the date() object.
Write the day using JavaScript
To write the day using JavaScript, we need to initialize the date() object, use the getDay method to return a numerical value corresponding to the current day, then output the day in whatever format we choose. This is actually much simpler than it sounds.
<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>
Write the date using JavaScript
We might also need to write the date. This is accomplished by initializing the date() object, getting the current date in numerical form by using the getDate method, than outputting the date in whatever format we choose. In this example, I want to write the date in number form, but I also want to append the appropriate suffix corresponding to the date (i.e. "th", "st", "nd", etc.).
<script type="text/javascript">
today=new Date(); // Initialize Date in raw form
date=today.getDate(); // Get the numerical date
// 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");
document.write (date + suffix); //Write the date
</script>
Write the month using JavaScript
We’re only missing two components of a complete date: the month and year.
To write the month, we need to initialize the date() object, get the current month in raw form (0,1,2,3,4,etc.) by using the getMonth method, then output the date in whatever form we like.
<script type="text/javascript">
today=new Date(); // Initialize Date in raw form
month=today.getMonth()+1; // Get the month
// 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");
document.write (monthName); //Write the month
</script>
Write the year using Javascript
To write the current year, we need to initialize the date() object, then use the getYear method to return the year. This is the easiest part of writing the date in JavaScript.
<script type="text/javascript">
today=new Date(); // Initialize Date in raw form
year=today.getYear(); // Get the year
document.write (year);
</script>
Write the full date using JavaScript
So far, we’ve learned to get the day, date, month, and year in JavaScript. To write the full date, all we need to do is combine the examples above. The following code will output the date in the form of "Sunday, November 27th, 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>
Guidelines: You must register with a social media account such as Facebook, Twitter, Yahoo, etc. to comment on this story. Click on the "Login" button below to choose your login account of choice. We welcome your thoughts, but this is not an open forum. For the sake of all readers, please refrain from the use of obscenities, personal attacks or racial slurs. All comments must remain on topic and cyber bullying will not be tolerated. All comments are subject to our terms of service. Comments that do not comply may be removed. Repeat offenders will lose commenting privileges.
News Categories
Accidents and Incidents
Art and Society
Business and Finance
Education and Employment
Environment and Development
Help and Aid
Science and Technology
Security and Politics
Share Market and Exchange Rates
Sports and Entertainment
Tourism and Foreign Affairs
Women and Health
Information
Women and Health
Food Recipes
Love and Relationship
Web and IT
AJAX Examples and Tutorials
Java Scripts Examples and Tutorials
PHP Scripts Examples and Tutorials
Men want good-looking women
Making sex exciting
Duminda unmasked by Anarkali
Too busy for Love Making?
Srikanth Vandana got a Baby Boy-Father and Son Born on Same Day!
Anjali’s days with Bharathiraja and K. Balachandar
Have you gone off sex?
Sex and Shoes
Dirty Talk!
Happy Birthday Surya
Alcohol makes men BETTER in the bedroom, scientists claim
Sexless Marriages
Are You A Lazy Lover?
5 Secret Relationship Resolutions
Outsider Played Actress Sneha Hot Sexy Hip Cheap Photos?

































