Javascript:如何格式化给定的日期时间?

时间:2011-12-09 23:53:25

标签: javascript

鉴于我的数据是:

2011-12-31 01:00:00

我可以使用什么简单快捷的脚本来简单地进行exctract:" DEC 31" ?

5 个答案:

答案 0 :(得分:6)

创建以下辅助函数:

function getMonthName(d) {
    var m = ['January','February','March','April','May','June','July',
        'August','September','October','November','December'];
    return m[d.getMonth()];
}

function getShortMonthName(d) {
    return getMonthName(d).substring(0, 3).toUpperCase();
}

并像这样使用它们:

var s = "2011-12-31 01:00:00".split(/-|\s+|:/);
// new Date(year, month, day [, hour, minute, second, millisecond ])
var d = new Date(s[0], s[1] - 1, s[2], s[3], s[4], s[5]);

getShortMonthName(d) + " " + d.getDate();

输出:

"DEC 31"

答案 1 :(得分:4)

http://www.datejs.com/很适合这个

使用它代码就像(测试和工作)

Date.parse('2011-12-31 01:00:00').toString("MMM d"); // "Dec 31"

这个解决方案很棒,因为datajs是一个非常灵活的库。

答案 2 :(得分:3)

这可以做到。只需将字符串作为参数传递给日期对象并拆分dateString即可。连接并且你已经完成了:)

var n = new Date("2011-12-31 01:00:00");
var d = n.toDateString().split(" ");
var formattedDate = d[1].toUpperCase() + " " + d[2];

或可选地作为函数

function getFormattedDate(dateString) {
    var n = new Date(dateString);
    var d = n.toDateString().split(" ");
    return d[1].toUpperCase() + " " + d[2];
}

var formattedDate = getFormattedDate("2011-12-31 01:00:00"); // returns "DEC 31"

答案 3 :(得分:0)

一个能完全满足您要求的功能(仅此而已):

function toMonthAndDay(dateString) {
    var months = ['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC'];
    var dateParts = dateString.split(/[- ]/);
    return months[+dateParts[1]] + " " + dateParts[2];
}

但是,为了获取任何日期并以任何自定义格式输出,我写了一个松散地基于.Net DateTime format strings的函数:

Date.prototype.format = function (format)
{
    var MMMM = ["\u0000", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    var MMM = ["\u0001", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    var dddd = ["\u0002", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    var ddd = ["\u0003", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
    function ii(i, len) { var s = i + ""; len = len || 2; while (s.length < len) s = "0" + s; return s; }

    var y = this.getFullYear();
    format = format.replace(/yyyy+/g, y);
    format = format.replace(/yy/g, y.toString().substr(2, 2));
    format = format.replace(/y/g, y);

    var M = this.getMonth() + 1;
    format = format.replace(/MMMM+/g, MMMM[0]);
    format = format.replace(/MMM/g, MMM[0]);
    format = format.replace(/MM/g, ii(M));
    format = format.replace(/M/g, M);

    var d = this.getDate();
    format = format.replace(/dddd+/g, dddd[0]);
    format = format.replace(/ddd/g, ddd[0]);
    format = format.replace(/dd/g, ii(d));
    format = format.replace(/d/g, d);

    var H = this.getHours();
    format = format.replace(/HH+/g, ii(H));
    format = format.replace(/H/g, H);

    var h = H > 12 ? H - 12 : H == 0 ? 12 : H;
    format = format.replace(/hh+/g, ii(h));
    format = format.replace(/h/g, h);

    var m = this.getMinutes();
    format = format.replace(/mm+/g, ii(m));
    format = format.replace(/m/g, m);

    var s = this.getSeconds();
    format = format.replace(/ss+/g, ii(s));
    format = format.replace(/s/g, s);

    var f = this.getMilliseconds();
    format = format.replace(/fff+/g, ii(f, 3));
    f = Math.round(f / 10);
    format = format.replace(/ff/g, ii(f));
    f = Math.round(f / 10);
    format = format.replace(/f/g, f);

    var T = H < 12 ? "AM" : "PM";
    format = format.replace(/TT+/g, T);
    format = format.replace(/T/g, T.charAt(0));

    var t = T.toLowerCase();
    format = format.replace(/tt+/g, t);
    format = format.replace(/t/g, t.charAt(0));

    var day = this.getDay() + 1;
    format = format.replace(new RegExp(dddd[0], "g"), dddd[day]);
    format = format.replace(new RegExp(ddd[0], "g"), ddd[day]);

    format = format.replace(new RegExp(MMMM[0], "g"), MMMM[M]);
    format = format.replace(new RegExp(MMM[0], "g"), MMM[M]);

    return format;
};

用法:

new Date("2011-12-31 01:00:00".replace(/-/g, "/")).format("MMM d"); // returns Dec 31

请注意,IE不会将“2011-12-31 01:00:00”识别为有效日期字符串。你必须用斜线替换破折号。要获得DEC而不是Dec,您必须致电.toUpperCase()

与.Net自定义日期格式字符串的区别在于:

  • 您可以分别使用TTtt来制作AM / PM大写或小写
  • 使用\作为转义字符尚未实现。

答案 4 :(得分:-1)

这应该有效:

var date = "2011-12-31 01:00:00";

var day = date.substring(8, 10);

var month = parseInt(date.substring(5, 7));
switch(month) {
    case 1:     month="JAN";break;
    case 2:     month="FEB";break;
    case 3:     month="MAR";break;
    case 4:     month="APR";break;
    case 5:     month="MAY";break;
    case 6:     month="JUN";break;
    case 7:     month="JUL";break;
    case 8:     month="AUG";break;
    case 9:     month="SEP";break;
    case 10:    month="OCT";break;
    case 11:    month="NOV";break;
    case 12:    month="DEC";break;
}


alert(month + " " + day);