【问题标题】:Javascript to determine date and output either "time" if it is today, "yesterday" for yesterday, and the actual date if it is before then用于确定日期并输出“时间”(如果是今天)、“昨天”(如果是昨天)和实际日期(如果是在此之前)的 Javascript
【发布时间】:2012-12-29 15:11:29
【问题描述】:

我正在创建一个简单的电子邮件客户端,我希望收件箱以以下格式显示收到电子邮件的日期:

今天 13:17

昨天 20:38

1 月 13 日 17:15

2012 年 12 月 21 日 @ 18:12

我正在从数据库中检索数据,将其输出到 xml(因此一切都可以通过 AJAX 完成)并将结果打印为 <ul><li> 格式。

日期和时间分别存储在以下格式中:

Date(y-m-d)

Time(H:i:s)

我发现使用 php 可以实现类似的功能。这里-PHP: date "Yesterday", "Today"

这可以使用 javascript 吗?

【问题讨论】:

    标签: javascript string date time trim


    【解决方案1】:

    我会做这样的事情

    function getDisplayDate(year, month, day) {
        today = new Date();
        today.setHours(0);
        today.setMinutes(0);
        today.setSeconds(0);
        today.setMilliseconds(0);
        compDate = new Date(year,month-1,day); // month - 1 because January == 0
        diff = today.getTime() - compDate.getTime(); // get the difference between today(at 00:00:00) and the date
        if (compDate.getTime() == today.getTime()) {
            return "Today";
        } else if (diff <= (24 * 60 * 60 *1000)) {
            return "Yesterday";
        } else { 
            return compDate.toDateString(); // or format it what ever way you want
        }
    }
    

    你应该能够得到这样的日期:

    getDisplayDate(2013,01,14);
    

    【讨论】:

    • 我可以更改格式以便显示 2012 年 8 月 12 日星期日吗?
    【解决方案2】:

    这是这两个答案的汇编(应该会给你一个很好的开始):

    我建议阅读问题和回答,以更好地了解正在发生的事情。


    function DateDiff(date1, date2) {
        return dhm(date1.getTime() - date2.getTime());
    }
    
    function dhm(t){
        var cd = 24 * 60 * 60 * 1000,
            ch = 60 * 60 * 1000,
            d = Math.floor(t / cd),
            h = '0' + Math.floor( (t - d * cd) / ch),
            m = '0' + Math.round( (t - d * cd - h * ch) / 60000);
        return [d, h.substr(-2), m.substr(-2)].join(':');
    }
    
    var yesterdaysDate = new Date("01/14/2013");
    var todaysDate = new Date("01/15/2013");
    
    // You'll want to perform your logic on this result
    var diff = DateDiff(yesterdaysDate, todaysDate); // Result: -1.00
    

    【讨论】:

      【解决方案3】:
      function getDisplayDate(year, month, day) {
          today = new Date();
          today.setHours(0);
          today.setMinutes(0);
          today.setSeconds(0);
          today.setMilliseconds(0);
          compDate = new Date(year,month-1,day); // month - 1 because January == 0
          diff = today.getTime() - compDate.getTime(); // get the difference between today(at 00:00:00) and the date
          if (compDate.getTime() == today.getTime()) {
              return "Today";
          } else if (diff <= (24 * 60 * 60 *1000)) {
              return "Yesterday";
          } else { 
              //return compDate.toDateString(); // or format it what ever way you want
              year = compDate.getFullYear();
              month = compDate.getMonth();
              months = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
              day = compDate.getDate();
              d = compDate.getDay();
              days = new Array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
      
              var formattedDate = days[d] + " " + day + " " + months[month] + " " + year;
              return formattedDate;
          }
      }
      

      这是@xblitz 的答案,我的格式可以很好地显示日期。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-07
        • 2014-10-26
        • 1970-01-01
        • 1970-01-01
        • 2012-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多