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>
2
  • Can you post what the correct and incorrect dates look like? Also, why is your code wrapped in an HTML comment? Commented Nov 29, 2013 at 6: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 Commented Nov 29, 2013 at 7:00

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

yes in eclipse browser it is displaying currectly but not in Chrome & MF it is showing year as 113
@RaghavendraM I have updated my code check that.
ya it worked . . Can u pls tell me why it is like that
@RaghavendraM After the years 2000 there is the problem to identify the years to the unix timestamp added the 100 before the years 2000. that's why it is showing like this. so to get the exact year you need to sum 1900 with current year. Also mark the answer as accepted.
Thanks for your reply. and how to accept the ans .
-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/

2 Comments

Thank u ... it worked . . Can you pls tell why it is like that. .?
Most of your answers seem to be nothing but copy and paste. You should include some of your own content.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.