如何在Javascript中显示日期的特定格式?

时间:2013-02-18 05:25:08

标签: javascript jquery ajax

如何使用Javascript日期函数在我的HTML页面中显示Monday, February 18, 2013 10:52等日期时间格式?

更新

我正在尝试

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

但是我希望时间应该自动更新而不是页面刷新。我需要使用AJAX吗?

5 个答案:

答案 0 :(得分:1)

我正在使用以下代码以我想要的格式显示日期

<script type="text/javascript">
                function makeArray() 
                {
                    for (i = 0; i<makeArray.arguments.length; i++)
                    this[i + 1] = makeArray.arguments[i];
                }

                var months = new makeArray('January','February','March','April','May',
                'June','July','August','September','October','November','December');
                var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
                var date = new Date();
                var day = date.getDate();
                var month = date.getMonth() + 1;
                var yy = date.getYear();
                var year = (yy < 1000) ? yy + 1900 : yy;
                var hours = date.getHours();
                var minutes = date.getMinutes();
                document.write(days[ date.getDay() ] + ", " + months[month] + " " + day + ", " + year + " " + hours +": " + minutes);
            </script>

答案 1 :(得分:1)

试试这个: - http://jsfiddle.net/2Fj7m/

日期和时间会自动更新。

HTML: -

<body onload="startTime()">
    <div id="txt" ></div>
</body>

JS: -

var months = new Array('January', 'February', 'March', 'April', 'May',
    'June', 'July', 'August', 'September', 'October', 'November', 'December');
var days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');

function startTime() {
    var today = new Date();
    var h = today.getHours();
    var m = today.getMinutes();
    var s = today.getSeconds();
    var weekday = days[today.getDay()];
    var month = months[today.getMonth()];
    var year = today.getFullYear();
    day = today.getUTCDate();

    // add a zero in front of numbers<10

    m = checkTime(m);
    s = checkTime(s);
    document.getElementById('txt').innerHTML =weekday + ", " +month +" "+ day+", " + year+ " "+ h + ":" + m;
    t = setTimeout(function () {
        startTime()
    }, 500);
}

function checkTime(i) {
    if (i < 10) {
        i = "0" + i;
    }
    return i;
}

答案 2 :(得分:0)

如果你加载了jQuery,你可以这样做:

HTML:

<div id='date'/>

JavaScript的:

$('#date').append(Date().toString())

答案 3 :(得分:0)

你应该使用javascript调试器作为firebug或chrome的调试器,它将显示所有可用的本机javascript函数。

您可以使用

var myDate = new Date();

myDate.toLocaleFormat();

OR

myDate.toLocaleString();

两者都会给出

Monday, 18 February, 2013 10:57:46 AM

接近您在问题中提到的格式。

答案 4 :(得分:0)

您可以使用javascript中的.Date()函数

创建此格式
$(document).ready(function() {
      var monthNames = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

      var dayNames = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

      var today = new Date();
      var cDay = today.getDay();
      var cDate = today.getDate();
      var cMonth = today.getMonth();
      var cYear = today.getFullYear();

      var cHour = today.getHours();
      var cMin = today.getMinutes();
      var cSec = today.getSeconds();


      alert(dayNames[cDay]+ "," + " "+ monthNames[cMonth] + " " +cDate  + "," +cYear + " " +cHour+ ":" + cMin+ ":" +cSec );
});