js 中的相对时间函数,格式为 YYYY-MM-DDTHH:MM:SS+0000

发布于 2024-09-11 08:08:33 字数 3050 浏览 3 评论 0原文

我想知道是否有人知道我如何解析这种类型的日期格式: 2010-07-26T18:02:46+0000

转换为相对时间,例如“30 秒前”

我已经有一个函数可以执行类似的操作,但不同的时间格式:

    function relative_time(time_value, is_relative) {
        var values = time_value.split(" "),
            parsed_date = Date.parse(values[1] + " " + values[2] + ", " + values[5] + " " + values[3] + " UTC"),
            date = new Date(parsed_date),
            relative_to = new Date(),
            r = '',
            delta = parseInt((relative_to.getTime() - date.getTime()) / 1000);

        var seconds = {
          'from' : {
            'minutes' : function(v) { return v * 60; },
            'hours'   : function(v) { return this.minutes(v) * 60; },
            'days'    : function(v) { return this.hours(v) * 24; },
            'weeks'   : function(v) { return this.days(v) * 7; },
            'months'  : function(v) { return this.weeks(v) * 4.34812141; },
            'years'   : function(v) { return this.months(v) * 12; }
          },
          'to' : {
            'minutes' : function(v) { return v / 60; },
            'hours'   : function(v) { return this.minutes(v) / 60; },
            'days'    : function(v) { return this.hours(v) / 24; },
            'weeks'   : function(v) { return this.days(v) / 7; },
            'months'  : function(v) { return this.weeks(v) / 4.34812141; },
            'years'   : function(v) { return this.months(v) / 12; }
          }
        };

        if (!is_relative)
          return formatTime(date) + ' ' + formatDate(date);

        if (delta < 30) 
          return 'less than a minute ago';
        var minutes = parseInt(seconds.to.minutes(delta)+0.5);
        if (minutes <= 1) 
          return 'about a minute ago';
        var hours = parseInt(seconds.to.hours(delta)+0.5);
        if (hours < 1) 
          return minutes + ' minutes ago';
        if (hours == 1) 
          return 'about an hour ago';
        var days = parseInt(seconds.to.days(delta)+0.5);
        if (days < 1) 
          return hours + ' hours ago';
        if (days==1) 
          return formatTime(date) + ' yesterday';
        var weeks = parseInt(seconds.to.weeks(delta)+0.5);
        if (weeks < 2) 
          return formatTime(date) + ' ' + days + ' days ago';
        var months = parseInt(seconds.to.months(delta)+0.5);
        if (months < 2) 
          return weeks + ' weeks ago';
        var years = parseInt(seconds.to.years(delta)+0.5);
        if (years < 2) 
          return months + ' months ago';
        return years + ' years ago';

        function formatTime(date) {
            var hour = date.getHours(),
                min = date.getMinutes() + "",
                ampm = 'AM';

            if (hour >= 12) ampm = 'PM';
            if (hour > 12) hour -= 12;

            if (min.length == 1) {
                min = '0' + min;
            }

            return hour + ':' + min + ' ' + ampm;
        }
};

此函数的格式为:Fri Nov 06 02:53:43 +0000

我如何更改此脚本以使用新的时间格式?在这个阶段,这有点超出了我的能力,我很想学习。

谢谢

I was wondering if anyone knew how I could parse this type of date format: 2010-07-26T18:02:46+0000

into the relative time such as "30 seconds ago"

I already have a function which does it for a similar but different time format:

    function relative_time(time_value, is_relative) {
        var values = time_value.split(" "),
            parsed_date = Date.parse(values[1] + " " + values[2] + ", " + values[5] + " " + values[3] + " UTC"),
            date = new Date(parsed_date),
            relative_to = new Date(),
            r = '',
            delta = parseInt((relative_to.getTime() - date.getTime()) / 1000);

        var seconds = {
          'from' : {
            'minutes' : function(v) { return v * 60; },
            'hours'   : function(v) { return this.minutes(v) * 60; },
            'days'    : function(v) { return this.hours(v) * 24; },
            'weeks'   : function(v) { return this.days(v) * 7; },
            'months'  : function(v) { return this.weeks(v) * 4.34812141; },
            'years'   : function(v) { return this.months(v) * 12; }
          },
          'to' : {
            'minutes' : function(v) { return v / 60; },
            'hours'   : function(v) { return this.minutes(v) / 60; },
            'days'    : function(v) { return this.hours(v) / 24; },
            'weeks'   : function(v) { return this.days(v) / 7; },
            'months'  : function(v) { return this.weeks(v) / 4.34812141; },
            'years'   : function(v) { return this.months(v) / 12; }
          }
        };

        if (!is_relative)
          return formatTime(date) + ' ' + formatDate(date);

        if (delta < 30) 
          return 'less than a minute ago';
        var minutes = parseInt(seconds.to.minutes(delta)+0.5);
        if (minutes <= 1) 
          return 'about a minute ago';
        var hours = parseInt(seconds.to.hours(delta)+0.5);
        if (hours < 1) 
          return minutes + ' minutes ago';
        if (hours == 1) 
          return 'about an hour ago';
        var days = parseInt(seconds.to.days(delta)+0.5);
        if (days < 1) 
          return hours + ' hours ago';
        if (days==1) 
          return formatTime(date) + ' yesterday';
        var weeks = parseInt(seconds.to.weeks(delta)+0.5);
        if (weeks < 2) 
          return formatTime(date) + ' ' + days + ' days ago';
        var months = parseInt(seconds.to.months(delta)+0.5);
        if (months < 2) 
          return weeks + ' weeks ago';
        var years = parseInt(seconds.to.years(delta)+0.5);
        if (years < 2) 
          return months + ' months ago';
        return years + ' years ago';

        function formatTime(date) {
            var hour = date.getHours(),
                min = date.getMinutes() + "",
                ampm = 'AM';

            if (hour >= 12) ampm = 'PM';
            if (hour > 12) hour -= 12;

            if (min.length == 1) {
                min = '0' + min;
            }

            return hour + ':' + min + ' ' + ampm;
        }
};

Format for this function would be: Fri Nov 06 02:53:43 +0000

How could I change this script to work with the new time format? It's a bit beyond me at this stage and I am keen to learn.

Thank You

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

轻拂→两袖风尘 2024-09-18 08:08:34

我不知道你是否想使用 jQuery,但是:

有一个 jQuery 插件。

I don't know if you want to use jQuery but:

There is a jQuery plugin for it.

破晓 2024-09-18 08:08:34

看看这是否有效...

var fuzzyFacebookTime = (function(){

  fuzzyTime.defaultOptions={
    // time display options
    relativeTime : 48,
    // language options
    monthNames : ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    amPm : ['AM', 'PM'],
    ordinalSuffix : function(n) {return ['th','st','nd','rd'][n<4 || (n>20 && n % 10<4) ? n % 10 : 0]}
  }

  function fuzzyTime (timeValue, options) {

    var options=options||fuzzyTime.defaultOptions, 
        date=parseDate(timeValue),
        delta=parseInt(((new Date()).getTime()-date.getTime())/1000),
        relative=options.relativeTime,
        cutoff=+relative===relative ? relative*60*60 : Infinity;

    if (relative===false || delta>cutoff)
      return formatTime(date, options)+' '+formatDate(date, options);

    if (delta<60) return 'less than a minute ago';
    var minutes=parseInt(delta/60 +0.5);
    if (minutes <= 1) return 'about a minute ago';
    var hours=parseInt(minutes/60 +0.5);
    if (hours<1) return minutes+' minutes ago';
    if (hours==1) return 'about an hour ago';
    var days=parseInt(hours/24 +0.5);
    if (days<1) return hours+' hours ago';
    if (days==1) return formatTime(date, options)+' yesterday';
    var weeks=parseInt(days/7 +0.5);
    if (weeks<2) return formatTime(date, options)+' '+days+' days ago';
    var months=parseInt(weeks/4.34812141 +0.5);
    if (months<2) return weeks+' weeks ago';
    var years=parseInt(months/12 +0.5);
    if (years<2) return months+' months ago';
    return years+' years ago';
  }

  function parseDate (str) {
    var v=str.replace(/[T\+]/g,' ').split(' ');
    return new Date(Date.parse(v[0] + " " + v[1] + " UTC"));
  }

  function formatTime (date, options) {
    var h=date.getHours(), m=''+date.getMinutes(), am=options.amPm;
    return (h>12 ? h-12 : h)+':'+(m.length==1 ? '0' : '' )+m+' '+(h<12 ? am[0] : am[1]);
  }

  function formatDate (date, options) {
    var mon=options.monthNames[date.getMonth()],
        day=date.getDate(),
        year=date.getFullYear(),
        thisyear=(new Date()).getFullYear(),
        suf=options.ordinalSuffix(day);

    return mon+' '+day+suf+(thisyear!=year ? ', '+year : '');
  }

  return fuzzyTime;

}());

See if this works...

var fuzzyFacebookTime = (function(){

  fuzzyTime.defaultOptions={
    // time display options
    relativeTime : 48,
    // language options
    monthNames : ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    amPm : ['AM', 'PM'],
    ordinalSuffix : function(n) {return ['th','st','nd','rd'][n<4 || (n>20 && n % 10<4) ? n % 10 : 0]}
  }

  function fuzzyTime (timeValue, options) {

    var options=options||fuzzyTime.defaultOptions, 
        date=parseDate(timeValue),
        delta=parseInt(((new Date()).getTime()-date.getTime())/1000),
        relative=options.relativeTime,
        cutoff=+relative===relative ? relative*60*60 : Infinity;

    if (relative===false || delta>cutoff)
      return formatTime(date, options)+' '+formatDate(date, options);

    if (delta<60) return 'less than a minute ago';
    var minutes=parseInt(delta/60 +0.5);
    if (minutes <= 1) return 'about a minute ago';
    var hours=parseInt(minutes/60 +0.5);
    if (hours<1) return minutes+' minutes ago';
    if (hours==1) return 'about an hour ago';
    var days=parseInt(hours/24 +0.5);
    if (days<1) return hours+' hours ago';
    if (days==1) return formatTime(date, options)+' yesterday';
    var weeks=parseInt(days/7 +0.5);
    if (weeks<2) return formatTime(date, options)+' '+days+' days ago';
    var months=parseInt(weeks/4.34812141 +0.5);
    if (months<2) return weeks+' weeks ago';
    var years=parseInt(months/12 +0.5);
    if (years<2) return months+' months ago';
    return years+' years ago';
  }

  function parseDate (str) {
    var v=str.replace(/[T\+]/g,' ').split(' ');
    return new Date(Date.parse(v[0] + " " + v[1] + " UTC"));
  }

  function formatTime (date, options) {
    var h=date.getHours(), m=''+date.getMinutes(), am=options.amPm;
    return (h>12 ? h-12 : h)+':'+(m.length==1 ? '0' : '' )+m+' '+(h<12 ? am[0] : am[1]);
  }

  function formatDate (date, options) {
    var mon=options.monthNames[date.getMonth()],
        day=date.getDate(),
        year=date.getFullYear(),
        thisyear=(new Date()).getFullYear(),
        suf=options.ordinalSuffix(day);

    return mon+' '+day+suf+(thisyear!=year ? ', '+year : '');
  }

  return fuzzyTime;

}());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文