简体   繁体   English

如何使用JQuery prettyDate插件?

[英]How to use JQuery prettyDate plugin?

Please I will like to know, how do I use or invoke John Resig pretty date js script? 请我想知道,我如何使用或调用John Resig pretty date js脚本? I have tried both his and the plugin version hosted on Google code 我已经尝试过他和Google代码上托管的插件版本

Below is the code I have tried: 以下是我尝试过的代码:

// HTML
<div id="block-menu-menu-tour">
    Hello
    <a title="2008-01-28T20:24:17Z"></a>
</div>

// With JQuery
window.alert($("a").prettyDate());

// plain DOM
function prettyLinks(){
  var links = document.getElementsByTagName("a");
  for ( var i = 0; i < links.length; i++ )
    if ( links[i].title ) {
      var date = prettyDate(links[i].title);
      if ( date )
        links[i].innerHTML = date;
    }
}
prettyLinks();
setInterval(prettyLinks, 500);

It was saying undefined because the passed date is 2008, hence undefined. 有人说不确定,因为通过的日期是2008,因此不确定。 I changed it to 2011-07-24T19:18:357Z and I called the function like this: prettyDate([UTC-date]); 我将其更改为2011-07-24T19:18:357Z,然后调用了如下函数: prettyDate([UTC-date]);

// With JQuery
$(function() {
$("a").prettyDate();
    });

I will like to advise that using JR's pretty.js is more flexible than using the jquery plugin. 我想建议使用JR的pretty.js比使用jquery插件更灵活。

For those who need it, I parsed Twitter json created_at like this: 对于那些需要它的人,我像这样解析Twitter json created_at:

/**
 * Converts to UTC date then prettyDate it.
 * 
 * Z = UTC time (To express the time of day in UTC)
 * T = local time (time of day in basic format)
 * 
 * @param holds Twitter date format of Sun, 24 Jul 2011 15:43:36 +0000
 */
liveTweets.parseDate = function(created_at) {
    //twitter_regex_date = /(\w+[,])\s(\d{2})\s(\w+)\s(\d{4})\s(\d{2}[:]\d{2}[:]\d{2})\s([+]\d{4})/;
    var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
    var pattern = /\s/;
    created_at = created_at.split(pattern);
    for (i = 0, len = created_at.length; i < len; i++){
        day_of_week = created_at[0];
        day = created_at[1];
        month_pos = created_at[2];
        month = 0 + months.indexOf(month_pos) + 1; // add 1 because array starts from zero
        year = created_at[3];
        time = created_at[4];
    }
    created_at = year+'-'+month+'-'+day+'T'+time+'Z'; //2011-07-24T19:18:357Z
    created_at = prettyDate(created_at);

    if(created_at != undefined)
    return created_at;
}

Have you included both, jquery and prettyDate scripts into that document? 您是否已将jquery和prettyDate脚本都包含在该文档中? also, have surrounded your jQuery code in .ready() block? 另外,是否在.ready()块中包含了您的jQuery代码?

$(document).ready(function() {});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM