How to show date in HTML?

I want the date to be sn in my page, I used this fn for the date using Javascript:

    function Return() 
        {
           var d = new Date();
           var n = d.();
           var to ="";

           if (n == 0)
            to = 'Sun';
           else if (n == 1)
            to = 'Mon';
           else if (n == 2)
            to = 'Tues';
           else if (n == 3)
            to = 'Wednes';
           else if (n == 4)
            to = 'Thurs';
           else if (n == 5)
            to = 'Fri'
           else if (n == 6)
            to = 'Satur';
           else
           alert('To is No');

          return to;

}

      

On the page, I used this line to call it, but it doesn't work:

    <input id="DateAndTime" type="text" value ="Return()"></input>

      

Do you know I can display it on the page? And if you know the best (but simple) ideas for singing dates in the pages, please let me know.

Many thanks:)

+3


source to share


8 answers


You can do the following.

Your javascript should look like below

    <script>
       function ReturnDay() 
    {
       var d = new Date();
       var n = d.getDay();
       var today ="";

       if (n == 0)
        today = 'Sunday';
       else if (n == 1)
        today = 'Monday';
       else if (n == 2)
        today = 'Tuesday';
       else if (n == 3)
        today = 'Wednesday';
       else if (n == 4)
        today = 'Thursday';
       else if (n == 5)
        today = 'Friday'
       else if (n == 6)
        today = 'Saturday';
       else
       alert('Today is NoDay');

        document.getElementById("DateAndTime").value = today;
    }
ReturnDay();
    </script>

      



and your html

 <input id="DateAndTime" type="text" value ="" />

      

Link to jsfiddle: http://jsfiddle.net/mr2rr/

+5


source


Have a look at the javascript date object.



You can also use PHP using date function

+2


source


You can just call this function and it will set the date for you.

function SetDay() 
{
    var d = new Date();
    var n = d.getDay();
    var today ="";

    if (n == 0)
        today = 'Sunday';
    else if (n == 1)
        today = 'Monday';
    else if (n == 2)
        today = 'Tuesday';
    else if (n == 3)
        today = 'Wednesday';
    else if (n == 4)
        today = 'Thursday';
    else if (n == 5)
        today = 'Friday'
    else if (n == 6)
        today = 'Saturday';
    else
       alert('Today is NoDay');

   // Notice this set the day in your field with the id "DateAndTime"
   document.getElementById("DateAndTime").value = today;

   // Or if you want to set it via jQuery
   // $('#DateAndTime').val(today);
}

      

+2


source


Don't rely on client side code to generate any time related data.

That is, if you care, then, by all means, rely on the fact that the time of the client device is set on earth; -)

+2


source


You can use this inline code if you like:

<html>
<body>

<script type="text/javascript">

var d=new Date();
document.write(d);

</script>

</body>
</html>

      

See W3 Schools for almost all web developer needs: http://www.w3schools.com/js/js_obj_date.asp

+1


source


This is a more elegant way to replace operators if-else

:

function dayOfWeek() {
  var now = new Date();
  var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
  return dayNames[now.getDay()];
}

      

+1


source


As you think you want to enter a date input field, maybe you can use something existing.

The jquery UI library has a datepicker which might just be what you're looking for:

$( "#yourinputid" ).datepicker();

      

See this link on jquery UI page

+1


source


function SetDayOfTheWeek(){
    var d=new Date();
    var weekday=new Array(7);
    weekday[0]="Sunday";
    weekday[1]="Monday";
    weekday[2]="Tuesday";
    weekday[3]="Wednesday";
    weekday[4]="Thursday";
    weekday[5]="Friday";
    weekday[6]="Saturday";

    var today = weekday[d.getDay()];
    document.getElementById("DateAndTime").value = today;
}

SetDayOfTheWeek();

      

+1


source







All Articles