在Javascript中格式化日期字符串

时间:2012-01-16 16:21:19

标签: javascript ajax date format

我有一个日期字符串,如下所示:

  

Thu Feb 09 2012 01:50:00 GMT + 0000(GMT标准时间)

我基本上需要将其重新格式化为2个字符串(一个用于日期,一个用于时间)。

日期格式必须为:

  

m-d-Y(01-16-2012)

时间应该是

  

H:我(10:30)

我对Javascript相当新,所以请原谅我对此的无知。

非常感谢任何帮助!

谢谢!

6 个答案:

答案 0 :(得分:4)

我建议你看一下Datejs。它是一个出色的JavaScript库,用于解析日期。 (利用jQuery实现清洁)

<script type="text/javascript" src="http://www.datejs.com/build/date.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<div id="date"></div>
<div id="time"></div>
<script type="text/javascript">
var d = "Thu Feb 09 2012 01:50:00 GMT+0000";
var dout = Date.parse(d);
$("#date").text(dout.toString('M-d-yyyy')); 
$("#time").text(dout.toString('HH:mm'));     
</script>

答案 1 :(得分:4)

您可能需要查看Moment.js

我相信DateJS扩展了原生的JavaScript Date Object,可能不是你想要的。我很确定MomentJS(受到DateJS的启发)不会扩展本机日期对象。

查看Moment.js的文档,这应该适合你:http://momentjs.com/docs/#/parsing/string+format

var foo = moment(stringDateValue,'MM-DD-YYYY hh:mm');

答案 2 :(得分:2)

我不确定内置的Date对象,但Datejs可以执行此操作。

使用Datejs'toString方法:

var now = new Date();
console.log(now.toString('MM-dd-yyyy')); // 01-16-2012
console.log(now.toString('HH:mm')); // 10:30

DEMO:http://jsfiddle.net/b3wgR/3

编辑:如果您的日期来自字符串,则Datejs可以为您parse

var str = "Thu Feb 09 2012 01:50:00 GMT+0000";
var date = Date.parse(str);
console.log(date.toString('MM-dd-yyyy')); // 02-09-2012
console.log(date.toString('HH:mm')); // 01:50

DEMO:http://jsfiddle.net/RY5jb/

答案 3 :(得分:2)

请使用此代码,我希望这对您有所帮助:

    var td = 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 month_name = new Array(12);
    month_name[0] = "January";
    month_name[1] = "February";
    month_name[2] = "March";
    month_name[3] = "April";
    month_name[4] = "May";
    month_name[5] = "June";
    month_name[6] = "July";
    month_name[7] = "August";
    month_name[8] = "September";
    month_name[9] = "October";
    month_name[10] = "November";
    month_name[11] = "December"; 

   document.getElementById("TodayDate").innerHTML = weekday[td.getDay()] + ", " + month_name[td.getMonth()] + " " + td.getDate() + ", " + td.getFullYear();

现在输出如下: 2014年5月22日,星期四

演示: http://jsfiddle.net/satishverma143/xSAbK/

答案 4 :(得分:1)

您可以使用此功能

//example: dateFormater("D, M n, Y h:ft", new Date(), 2)
//results: a formated date two days from now -> Sunday, February 10, 2013 3:07pm
//today is Friday 2/8/13
function dateFormater (mod, nDate, addDay) {
    var daysFull = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    var daysAbbr = ["Sun.", "Mon.", "Tue.", "Wed.", "Thu.", "Fri.", "Sat."];
    var monthsFull = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    var monthsAbbr = ["Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.", "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."];
    var moddt = "";
    var tmp;

    //add days to date (use -1 to subtract days
    if (typeof addDay !== 'undefined') nDate.setDate(nDate.getDate() + addDay);

    var dateMod = mod.split("");
    for (el in dateMod) {
        switch(dateMod[el]) {
            //Delimiters: ,=, /=/ " "=" " :=:
            case ",":
                moddt += dateMod[el];
                break;
            case "/":
                moddt += dateMod[el];
                break;
            case " ":
                moddt += dateMod[el];
                break;
            case ":":
                moddt += dateMod[el];
                break;

            //Month: M=January m=Jan. J=01 j=1
            case "M":
                moddt += monthsFull[nDate.getMonth()];
                break;
            case "m":
                moddt += monthsAbbr[nDate.getMonth()];
                break;
            case "J":
                tmp = nDate.getDate();
                tmp = tmp.toString();
                if (tmp.length == 1) {
                    moddt += "0" + tmp;
                } else {
                    moddt += tmp;
                }
                break;
            case "j":
                tmp = nDate.getDate();
                moddt += tmp.toString();
                break;

            //Day: D=Monday d=Mon. N=01 n=1
            case "D":
                moddt += daysFull[nDate.getDay()];
                break;
            case "d":
                moddt += daysAbbr[nDate.getDay()];
                break;
            case "N":
                tmp = nDate.getDate();
                tmp = tmp.toString();
                if (tmp.length == 1) {
                    moddt += "0" + tmp;
                } else {
                    moddt += tmp;
                }
                break;
            case "n":
                tmp = nDate.getDate();
                moddt += tmp.toString();
                break;

            //Year: Y=2013 y=13
            case "Y":
                moddt += nDate.getFullYear();
                break;
            case "y":
                tmp = nDate.getFullYear();
                tmp = tmp.toString();
                moddt += tmp.substr(2, 2);
                break;

            //Hour: H=08 h=8 Military: Z=14 z=01
            case "H":
                tmp = nDate.getHours();
                if (tmp > 12) tmp -= 12;
                tmp = tmp.toString();
                if (tmp.length == 1) {
                    moddt += "0" + tmp;
                } else {
                    moddt += tmp;
                }
                    break;
            case "h":
                tmp = nDate.getHours();
                if (tmp > 12) tmp -= 12;
                moddt += tmp.toString();
                break;
            case "Z":
                tmp = nDate.getHours();
                tmp = tmp.toString();
                if (tmp.length == 1) {
                    moddt += "0" + tmp;
                } else {
                    moddt += tmp;
                }
                break;
            case "z":
                tmp = nDate.getHours();
                moddt += tmp.toString();
                break;

            //Minute: f=08
            case "f":
                tmp = nDate.getMinutes();
                tmp = tmp.toString();
                if (tmp.length == 1) {
                    moddt += "0" + tmp;
                } else {
                    moddt += tmp;
                }
                break;

            //Seconds: s=08
            case "s":
                tmp = nDate.getSeconds();
                tmp = tmp.toString();
                if (tmp.length == 1) {
                    moddt += "0" + tmp;
                } else {
                    moddt += tmp;
                }
                break;

            //AM/PM: T=AM/PM t=am/pm q=a/p
            case "T":
                if (nDate.getHours() > 11) {
                    moddt += "PM";
                } else {
                    moddt += "AM";
                }
                break;
            case "t":
                if (nDate.getHours() > 11) {
                    moddt += "pm";
                } else {
                    moddt += "am";
                }
                break;
            case "q":
                if (nDate.getHours() > 11) {
                    moddt += "p";
                } else {
                    moddt += "a";
                }
                break;
        }       
    }
    return moddt;
}

答案 5 :(得分:1)

这是一个新的Date()。格式的聚合填充(“MM-DD-YYYY HH:mm”);

它将返回一个你想要的任何格式的字符串

https://github.com/UziTech/js-date-format