0

在 HTML 如何更改日期的格式?使用 date() 我得到了今天的日期。问题是我不知道如何更改日期的格式。我必须显示这样的日期格式“2013 年 5 月 28 日”。谁能帮我 ???

    <html position="0,0,0,0" canExport="false">
    <![CDATA[
        <script>
            var currentTime = new Date();
            var month = currentTime.getMonth() + 1;
            var monthname ;
            if (month == 1)
                monthname = "Jan";
            else if (month == 2)
                monthname = "Feb";
            else if (month == 3)
                monthname = "Mar";
            else if (month == 4)
                monthname = "Apr";
            else if (month == 5)
                monthname = "May";
            else if (month == 5)
                monthname = "June";
            else if (month == 7)
                monthname = "July";
            else if (month == 8)
                monthname = "Aug";
            else if (month == 9)
                monthname = "Sep";
            else if (month == 10)
                monthname = "Oct";
            else if (month == 11)
                monthname = "Nov";
            else 
                monthname = "Dec";

            var day = currentTime.getDate();
            var dayname;
            if (day == 2 ||day == 22 )
                dayname = "nd";
            else if (day == 3 ||day == 23 )
                dayname = "rd";
            else if (day == 1 ||day == 21||day == 31 )
                dayname = "st";
            else 
                dayname = "th";

            var year = currentTime.getFullYear();
            s = currentTime.toDateString();
            alert(s);
            $('[name=DateDynamic]').text(monthname+" "+day+""+dayname+" "+ year);

        </script>
    ]]>
</html>

目前我正在遵循上述大方法。我知道应该有一些简单的方法。

4

5 回答 5

0

根据您的示例,我对其进行了一些清理。您还getDate()为您使用“day”变量而不是getDay().

<script type="text/javascript">
        var currentTime = new Date();

        var month = currentTime.getMonth() + 1;
        var months = ["Dec","Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct","Nov",];
        var monthname = months[month];

        var day = currentTime.getDay();
        var dayname;
        if (day == 2 ||day == 22 )
            dayname = "nd";
        else if (day == 3 ||day == 23 )
            dayname = "rd";
        else if (day == 1 ||day == 21||day == 31 )
            dayname = "st";
        else 
            dayname = "th";

        var year = currentTime.getFullYear();
        alert(monthname+" "+day+""+dayname+" "+ year);
        $('[name=DateDynamic]').text(monthname+", "+day+""+dayname+" "+ year);

    </script>
于 2013-05-31T16:05:55.207 回答
0
var myDate = new Date();

var formattedDate = myDate.toDateString().split(' ')
formattedDate[0] = '';


if (formattedDate[2][0] != 1 && formattedDate[2][1] == 1) {
    formattedDate[2] = formattedDate[2] + 'st';
} else if (formattedDate[2][0] != 1 && formattedDate[2][1] == 2) {
    formattedDate[2] = formattedDate[2] + 'nd';    
} else if (formattedDate[2][0] != 1 && formattedDate[2][1] == 3) {
    formattedDate[2] = formattedDate[2] + 'rd';
} else {
    formattedDate[2] = formattedDate[2] + 'th';
}

formattedDate = formattedDate.join(' ');

今天的输出:2013 年 5 月 31 日
http://jsfiddle.net/VHqpU/2/

于 2013-05-31T13:44:01.953 回答
0

我相信你想要这个用于javascript?不是吗?假设你的标签

尝试类似:

Javascript代码:

var currentDate = new Date()
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
month = getMonthName(month);
var year = currentDate.getFullYear()
document.write("<b>" +month + " " + day  + " " + year + "</b>")

function getMonthName(monthNumber)
{
    var monthName = "";
     switch (monthNumber)
     {
         case 5:
         monthName = "May";
             break;
         default:
             monthName ="not implemented yet";
             break;
     }
    return monthName;
}

编辑--->我看到你已经改变了你的标签。我在这里为任何人撒谎!

于 2013-05-31T13:33:46.617 回答
0

您可以使用不同的语言来做到这一点,但我猜如果您想在页面加载时显示一个简单的日期,您想在 Javascript 中使用 Date 对象。

以下是关于使用 Javascript 格式化日期的多种方法的教程:http: //www.w3schools.com/js/js_obj_date.asp

于 2013-05-31T13:34:49.210 回答
0

我认为 Javascript 方法toDateString可能需要您正在寻找:

文档

 var now = new Date();
 x = now.toDateString();
 //Fri May 31 2013

 //Additional Formatting
 var patt = /(\w+) (\w+ [0-9]{1,2}) ([0-9]{4})/;
 x = x.replace(patt, '$2, $3');
 //May 31, 2013

我整理了一个小提琴来处理您的“日后缀”问题。

小提琴的输出:

 //May 31st, 2013
于 2013-05-31T13:35:24.057 回答