我注意到JavaScript的新Date()函数在接受多种格式的日期方面非常聪明。

Xmas95 = new Date("25 Dec, 1995 23:15:00")
Xmas95 = new Date("2009 06 12,12:52:39")
Xmas95 = new Date("20 09 2006,12:52:39")

调用new Date()函数时,我在任何地方都找不到显示所有有效字符串格式的文档。

这用于将字符串转换为日期。如果我们从相反的方面来看,即将日期对象转换为字符串,直到现在,我的印象是JavaScript没有将日期对象格式化为字符串的内置API。

编者按:以下方法是询问者在特定浏览器上的尝试,但通常不起作用;请参阅本页上的答案以了解一些实际解决方案。

今天,我在date对象上使用了toString()方法,令人惊讶的是,它可以将日期格式化为字符串。

var d1 = new Date();
d1.toString('yyyy-MM-dd');       //Returns "2009-06-29" in Internet Explorer, but not Firefox or Chrome
d1.toString('dddd, MMMM ,yyyy')  //Returns "Monday, June 29,2009" in Internet Explorer, but not Firefox or Chrome

在这里,我也找不到任何关于将日期对象格式化为字符串的方法的文档。

列出Date()对象支持的格式说明符的文档在哪里?


当前回答

如果您希望只显示两位数的时间,这可能会帮助您:

var now = new Date();
var cHour = now.getHours();
var cMinuts = now.getMinutes();
var cSeconds = now.getSeconds();

var outStr = (cHour <= 0 ? ('0' + cHour) : cHour) + ':' + (cMinuts <= 9 ? ('0' + cMinuts) : cMinuts) + ':' + (cSeconds <= 9 ? '0' + cSeconds : cSeconds);

其他回答

这是一个我经常使用的函数。结果为yyyy-mm-dd hh:mm:ss.nnn。

function date_and_time() {
    var date = new Date();
    //zero-pad a single zero if needed
    var zp = function (val){
        return (val <= 9 ? '0' + val : '' + val);
    }

    //zero-pad up to two zeroes if needed
    var zp2 = function(val){
        return val <= 99? (val <=9? '00' + val : '0' + val) : ('' + val ) ;
    }

    var d = date.getDate();
    var m = date.getMonth() + 1;
    var y = date.getFullYear();
    var h = date.getHours();
    var min = date.getMinutes();
    var s = date.getSeconds();
    var ms = date.getMilliseconds();
    return '' + y + '-' + zp(m) + '-' + zp(d) + ' ' + zp(h) + ':' + zp(min) + ':' + zp(s) + '.' + zp2(ms);
}

设置日期格式以返回“2012-12-29”的正确方法是使用JavaScript日期格式中的脚本:

var d1 = new Date();
return d1.format("dd-m-yy");

此代码不起作用:

var d1 = new Date();
d1.toString('yyyy-MM-dd');      

这个问题的具体答案在以下两行中找到:

//拉今年的最后两位数console.log(newDate().getFullYear().toString().substr(2,2));

格式化完整日期时间示例(MMddyy):jsFiddle

JavaScript:

//将日期格式化为MMddyy的函数函数格式日期(d){//得到这个月var month=d.getMonth();//把握好这一天var day=d.getDate();//获得年度最佳成绩var year=d.getFullYear();//拉今年的最后两位数year=year.toString().substr(2,2);//由于索引为0,每月递增1月=月+1;//将月份转换为字符串月=月+“”;//如果月份是1-9,请用0向右填充两位数如果(月长度==1){月=“0”+月;}//将日期转换为字符串day=天+“”;//如果日期在1-9之间,则用0表示两位数如果(日长度==1){天=“0”+天;}//返回字符串“MMddyy”回归月+日+年;}var d=新日期();console.log(formatDate(d));

在浏览了其他答案中提供的几个选项后,我决定编写自己的有限但简单的解决方案,其他人可能也会觉得有用。

/**
* Format date as a string
* @param date - a date object (usually "new Date();")
* @param format - a string format, eg. "DD-MM-YYYY"
*/
function dateFormat(date, format) {
    // Calculate date parts and replace instances in format string accordingly
    format = format.replace("DD", (date.getDate() < 10 ? '0' : '') + date.getDate()); // Pad with '0' if needed
    format = format.replace("MM", (date.getMonth() < 9 ? '0' : '') + (date.getMonth() + 1)); // Months are zero-based
    format = format.replace("YYYY", date.getFullYear());
    return format;
}

示例用法:

console.log("The date is: " + dateFormat(new Date(), "DD/MM/YYYY"));

动量.js

它是一个(轻量级)*JavaScript日期库,用于解析、处理和格式化日期。

var a = moment([2010, 1, 14, 15, 25, 50, 125]);
a.format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Sunday, February 14th 2010, 3:25:50 pm"
a.format("ddd, hA");                       // "Sun, 3PM"

(*)轻量级意味着在尽可能小的设置中缩小9.3KB+gzip(2014年2月)