日期:2014-05-16  浏览次数:20338 次

求写 js 日期格式化函数
有一个 timestamp类型的 时间  ,如 2014-05-03T 20:50:33   现在想要 的结果 是 如果你是 当天的时间就显示 时和分,如:20:50;不是当天的 就显示 月和日,如: 05-02. 求js高手 教 。先谢谢了 。
------解决方案--------------------

// date.js 
//   Illustrates the use of the Date object by 
//   displaying the parts of a current date and
//   using two Date objects to time a calculation
    
// Get the current date
alert("Start date.js");
      var today = new Date();

// Fetch the various parts of the date

      var dateString = today.toLocaleString();
      var day = today.getDay();
      var month = today.getMonth();
      var year = today.getFullYear();
      var timeMilliseconds = today.getTime();
      var hour = today.getHours();
      var minute = today.getMinutes();
      var second = today.getSeconds();
      var millisecond = today.getMilliseconds();

// Display the parts

      document.write(
        "Date: " + dateString + "<br />",
        "Day: " + day + "<br />",
        "Month: " + month + "<br />",
        "Year: " + year + "<br />",
        "Time in milliseconds: " + timeMilliseconds + "<br />",
        "Hour: " + hour + "<br />",
        "Minute: " + minute + "<br />",
        "Second: " + second + "<br />",
        "Millisecond: " + millisecond + "<br />");

------解决方案--------------------
var t ="2014-05-03T 20:50:33";
function format_t(t){
    var d=new Date(),b;
    if(b=t.match(/\d+/g)){
        return d.getFullYear()==b[0] && (d.getMonth()+1)==b[1] && d.getDate()==b[2] ? b[3]+":"+b[4] : b[1]+"-"+b[2];
    }else{
        return "";
    }
}
console.log(format_t(t));

------解决方案--------------------
var t ="2014-05-03T 20:50:33";
function format_t(t){
    var d=new Date(),b=t.m