1

I want to show the current date on html page through javascript . I tried with it but it is showing currect date in eclips browser but not showing currect date in other browsers like Chrome, IE, Mozilla Firfox . Can you pls help me in this . . and my java script is like this. .

<script type="text/javascript">

<!--today=new Date();
 document.write("Date:",today.getDate(),"/",today.getMonth()+1,"/",today.getYear());
-->
</script>
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
Raghavendra M
  • 199
  • 2
  • 3
  • 8
  • Can you post what the correct and incorrect dates look like? Also, why is your code wrapped in an HTML comment? – Rob M. Nov 29 '13 at 06:51
  • No i just commented it . . bcoz it showed invalid dates . it showed me currect date month but in year it is showing insted of 2013 it showing 113 – Raghavendra M Nov 29 '13 at 07:00

2 Answers2

4

Try this one:

<script type="text/javascript">

 var today = new Date();
 var year = (today.getYear() < 1000) ? (today.getYear()+1900) : today.getYear();
 document.write("Date:"+today.getDate()+"/"+(today.getMonth()+1)+"/"+year);

</script>

Add the var to assign the variable and add + sign to concat the string.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
-1

To use the dd/mm/yy format ( e.g. 29/11/2013 ):

<script type="text/javascript"><!--
var date = new Date();
var d  = date.getDate();
var day = (d < 10) ? '0' + d : d;
var m = date.getMonth() + 1;
var month = (m < 10) ? '0' + m : m;
var yy = date.getYear();
var year = (yy < 1000) ? yy + 1900 : yy;

document.write(day + "/" + month + "/" + year);
 //--></script>

Link

http://www.mediacollege.com/internet/javascript/date-time/

Monika
  • 2,172
  • 15
  • 24