BIOS,Windows, Linux, Software,Security, Domains, Web Hosting

How to Get Current Date in JavaScript

We get current data in javascript directory, of course, this date will be the client time, it may be not correct. I think you should not use this time as a crucial usage. 

Sample 1:  Add date using a dynamic array

<script language="JavaScript" type="text/JavaScript">
today=new Date();
function initme()
         for(var i=0;i<this.length;i++)  
var d=new initme("Sun","Mon","Tus","Wed","Thr","Fri","Sat");  
document.write( "<font color=#990000> ",today.getFullYear(),"</font> <font color=#990000>",
today.getMonth()+1,"</font> <font color=#990000>",today.getDate(),"</font> <font color=#990000>",d[today.getDay()+1],"</font>" );

Sample 2: Add date using a predefine array

<script language="JavaScript" type="text/JavaScript">
var d=new Array("Sun","Mon","Tus","Wed","Thr","Fri","Sat"); 
document.write( "<font color=#990000> ",today.getFullYear(),"</font> <font color=#990000>",
today.getMonth()+1,"</font> <font color=#990000>",today.getDate(),"</font> <font color=#990000>",d[today.getDay()],"</font>" );

We know the return value of getDay() is in [0,6], So I make it as a index in date array for week identification. If you want to add hour, minute, second, just use

getHours(), getMinutes(), getSeconds()

Some tips of  them:

getFullYear() - return year number (four numbers), eg, 2014. Do not use getYear() again. 
getMonth() - return value[0,11]
getDate() - return value[1,31], no leading 0 if it is smaller than 10.
getHours() - return value[0,23], no leading 0 if it is smaller than 10.
getMinutes() - return value[0,59], no leading 0 if it is smaller than 10.
getSeconds() - return value[0,59], no leading 0 if it is smaller than 10.

More Reading:

Related Articles


Leave a Reply

Subscribe to BootBeta Comments RSS feed to receive notification of latest comments posted.


  • Enter Email Address:

Recent Post

Copyright@ 2010-2014 Bootbeta All Rights Reserved.
HooBlog Ver 1.5 Build 20140530