Today's the day

向软件大牛炫耀我会焊单片机,向硬件大牛炫耀我会写 Rails,向软硬件大牛炫耀我生物,向软硬件生物大牛炫耀我会折腾期货 -_-bbb

又犯了个愚蠢的错误
又换回了 File Based Session Stroe

Javascript 相对时间

galeki posted @ 2008年12月01日 03:38 in RoR with tags javascript 相对时间 , 6556 阅读

因为相对时间是随时变化的,所以如果在页面里面直接生成相对时间的文本,那么就没法缓存成静态文件,不过用 js 可以解决这个问题。

首先,在页面里面还是用 rails 生成一般的时间,这样即使关掉了 js,也可以显示正常的时间。并套个 span:

<span class="relative_time"><%= @post.created_at %></span>

然后就是 js 的事情了,找出这些 span,并且算出相对时间替换进去:

function show_relative_time(local){
    var times = $$('span.relative_time');
    var now = new Date();
    for(i=0;i<times.length;i++){
        var time = times[i];
        var t = new Date(time.innerHTML);
        var diff = now - t;
        time.innerHTML = relative_time_text(Math.floor(Math.abs(diff/1000/60)), local);
    }
}

其中的 relative_time_text 接收分钟为参数,返回相对时间的文本:

function relative_time_text(m, local){
    var text;
    if(!rtlang[local])
        local = 'en';

    if(m <= 1)
        text = rtlang[local]['less than a minute'];
    else if(m > 1 && m <= 45)
        text = m + rtlang[local][' minutes'];
    else if(m > 45 && m <= 90)
        text = rtlang[local]['about 1 hour'];
    else if(m > 90 && m <= 1440)
        text = Math.round(m/60) + rtlang[local][' hour'];
    else if(m > 1440 && m <= 2880)
        text = rtlang[local]['1 day'];
    else if(m > 2880 && m <= 43200)
        text = Math.round(m/1440)+ rtlang[local][' days'];
    else if(m > 43200 && m <= 86400)
        text = rtlang[local]['about 1 month'];
    else if(m > 86400 && m <= 525600)
        text = Math.round(m/43200) + rtlang[local][' months'];
    else if(m > 525600 && m <= 1051200)
        text = rtlang[local]['about 1 year'];
    else
        text = Math.round(m/525600) + rtlang[local][' years'];

    return text + rtlang[local][' ago'];
}

基本就是照着 rails 里那函数改的,rtlang 就是个 hash,保存多语言信息:

var rtlang = {'zh_CN':{}, 'en':{}};
rtlang['en']['less than a minute'] = 'less than a minute';
rtlang['en'][' minutes'] = ' minutes';
rtlang['en']['about 1 hour'] = 'about 1 hour';
rtlang['en'][' hour'] = ' hour';
rtlang['en']['1 day'] = '1 day';
rtlang['en'][' days'] = ' days';
rtlang['en']['about 1 month'] = 'about 1 month';
rtlang['en'][' months'] = ' months';
rtlang['en']['about 1 year'] = 'about 1 year';
rtlang['en'][' years'] = ' years';
rtlang['en'][' ago'] = ' ago';


rtlang['zh_CN']['less than a minute'] = '小于一分钟';
rtlang['zh_CN'][' minutes'] = ' 分钟';
rtlang['zh_CN']['about 1 hour'] = '大约 1 小时';
rtlang['zh_CN'][' hour'] = ' 小时';
rtlang['zh_CN']['1 day'] = '1 天';
rtlang['zh_CN'][' days'] = ' 天';
rtlang['zh_CN']['about 1 month'] = '大约 1 个月';
rtlang['zh_CN'][' months'] = ' 个月';
rtlang['zh_CN']['about 1 year'] = '大约 1 年';
rtlang['zh_CN'][' years'] = ' 年';
rtlang['zh_CN'][' ago'] = ' 前';

这样如果要显示中文的相对时间,那么只需在页面尾部加上:

<script type="text/javascript">
     show_relative_time("zh_CN");
</script>

就搞定了~

fbpasshack.net 说:
2017年12月28日 17:47

It give a chance to the online good play site.

Faddy 说:
2021年3月08日 14:43

I can set up my new idea from this post. It gives in depth information. Thanks for this valuable information for all,.. 토토사이트

Faddy 说:
2021年3月10日 20:14

I was very impressed by this post, this site has always been pleasant news Thank you very much for such an interesting post, and I meet them more often then I visited this site. 먹튀검증업체

Faddy 说:
2021年3月16日 16:21

I can’t believe focusing long enough to research; much less write this kind of article. You’ve outdone yourself with this material without a doubt. It is one of the greatest contents. 먹튀검증

no excuses runner 说:
2021年3月17日 17:41

Many homework on the continual hunt along with offstage on the road to winning. Definitely not attached, simple to-fall as a result of wayside; And not investigation, afterward into a path travel toward the black. https://www.schedulelogin.com/

Faddy 说:
2021年3月21日 14:58

Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info. 꽁머니 사이트

buy South African ra 说:
2021年3月23日 18:02

Thanks for every other informative site. The place else may just I get that kind of information written in such an ideal means? I have a venture that I’m just now operating on, and I have been on the look out for such information.

opinion outpost revi 说:
2021年3月24日 19:41

Thanks for taking the time to discuss that, I feel strongly about this and so really like getting to know more on this kind of field. Do you mind updating your blog post with additional insight? It should be really useful for all of us.

Faddy 说:
2021年3月31日 19:16

I genuinely believed you would probably have something useful to say. All I hear is a bunch of whining about something that you can fix if you were not too busy looking for attention. After all, I know it was my choice to read.. Onewheel XR Accessories

Faddy 说:
2021年4月04日 19:30

Nothing compares to Weber using Genesis sort of grills. That is certainly the type gas grills which have been that will always make sure that they deliver a high performance part of down interval Onewheel Accessories

Faddy 说:
2021年4月07日 20:17

Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! Computer Support and Services Cheviot Hills Los Angeles, California

shah 说:
2021年4月08日 13:50

Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include. 마징가티비

shah 说:
2021年4月08日 15:49

Hello, I have browsed most of your posts. This post is probably where I got the most useful information for my research. Thanks for posting, maybe we can see more on this. Are you aware of any other websites on this subject. NovelWell

shah 说:
2021年4月10日 19:36

I exactly got what you mean, thanks for posting. And, I am too much happy to find this website on the world of Google. vet serangoon

https://ohozaa.com/ 说:
2021年4月10日 21:58

Soccer is among the greatest sports activities in the uk as well as report amounts of individuals are nevertheless likely to view fits regardless of the present condition from the UNITED KINGDOM economic climate. Wagering can also be popular right now compared to this actually may be as well as free of charge football bets tend to be a great way with regard to soccer enthusiasts to savor their own favorite activity simply a bit more.

shah 说:
2021年4月13日 14:55

Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. cenforce 120 mg in australia

Normanton Park 说:
2021年4月14日 15:53

Great post, and great website. Thanks for the information!

cat blog 说:
2021年4月17日 15:59

Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post.

revo unistaller pro 说:
2021年4月17日 19:36

Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include. revo unistaller pro

ccleaner key free do 说:
2021年4月19日 15:20

I really loved reading your blog. It was very well authored and easy to undertand. Unlike additional blogs I have read which are really not tht good. I also found your posts very interesting. In fact after reading, I had to go show it to my friend and he ejoyed it as well!

Weight Loss 说:
2021年4月20日 03:31

Our website is a place where every man will gain self-confidence, a guarantee of proven products and 100% understanding. We created the website because we want to help in the fight against what is not talked about loudly. Kamagra

legal steroids 说:
2021年4月20日 16:22

This is just the information I am finding everywhere. Thanks for your blog, I just subscribe your blog. This is a nice blog..

shah 说:
2021年4月21日 15:54

Efficiently written information. It will be profitable to anybody who utilizes it, counting me. Keep up the good work. For certain I will review out more posts day in and day out. 먹튀검증

메이저사이트 说:
2021年4月21日 17:57

I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much.

shah 说:
2021年4月22日 10:19

Wow, cool post. I’d like to write like this too – taking time and real hard work to make a great article… but I put things off too much and never seem to get started. Thanks though. meilleur souris gamer

Louise West 说:
2021年4月24日 17:04

New web site is looking good. Thanks for the great effort. masking vs clipping path

Überprüfen Sie meine 说:
2021年4月24日 19:14

Great post but I was wondering if you could write a little more on this subject? I’d be very thankful if you could elaborate a little bit further. Thanks in advance!

Weight Loss 说:
2021年4月24日 23:10

Qikink enables anyone to sell products online without inventory, processing and logistics hurdles. Anyone can signup on Qikink.com and start selling products online. Print on demand T-Shirts

https://ohozaa.com/ 说:
2021年4月25日 17:28

There are numerous procedures to find out totally free football bets. One of the most employed technique ought to be to help to make a good on the internet research together with consider the free of charge on the web betting web site. There are lots of related to football totally free bets you could acquire while using several on the web bookmakers, as well as numerous such offers are only actual in order to customers which sign up for an online site on the web.

토토사이트 说:
2021年4月26日 17:37

Nice post. I was checking constantly this blog and I’m impressed! Extremely useful info specially the last part I care for such information a lot. I was seeking this certain info for a long time. Thank you and good luck.

how to become a regi 说:
2021年4月29日 03:33

A great content material as well as great layout. Your website deserves all of the positive feedback it’s been getting. I will be back soon for further quality contents. Business studies in Canada

먹튀검증커뮤니티 说:
2021年4月29日 18:28

This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. 먹튀검증커뮤니티

asdzxc 说:
2021年5月01日 03:49

Our team is highly trained and focused on providing high-quality services at affordable prices. We take pride in keeping our customers satisfied and ensuring the longevity of their pools and hot tubs. pools winnipeg

Keira Booth 说:
2021年5月01日 15:47

Interesting topic for a blog. I have been searching the Internet for fun and came upon your website. Fabulous post. Thanks a ton for sharing your knowledge! It is great to see that some people still put in an effort into managing their websites. I'll be sure to check back again real soon. viagra

asdzxc 说:
2021年5月02日 02:38

We have years of experience in repairing fences and we can give you one that you will love! No matter what type of fence you have, we can fix it for you. cape coral fencing

Keira Booth 说:
2021年5月02日 15:36

Its a great pleasure reading your post.Its full of information I am looking for and I love to post a comment that "The content of your post is awesome" Great work. kkpoker

สล็อต joker123 说:
2021年5月02日 19:46

For a long time me & my friend were searching for informative blogs, but now I am on the right place guys, you have made a room in my heart! สล็อต joker123

메이저사이트 说:
2021年5月03日 19:21

Well-Written article. It will be supportive to anyone who utilizes it, including me. Keep doing what you are doing – can't pause to read more posts. Thanks for the precious help.

shahzaib 说:
2021年5月04日 16:14

Your work is very good and I appreciate you and hopping for some more informative posts. Thank you for sharing great information to us. kauchuk-rubber.com

shahzaib 说:
2021年5月04日 18:53

Pretty good post. I have just stumbled upon your blog and enjoyed reading your blog posts very much. I am looking for new posts to get more precious info. Big thanks for the useful info. eth to inr

shahzaib 说:
2021年5月05日 19:57

I haven’t any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us. reiboot torrent

Kian Warner 说:
2021年5月06日 17:32

It is truly a well-researched content and excellent wording. I got so engaged in this material that I couldn’t wait reading. I am impressed with your work and skill. Thanks. license key avast clean up

pussy888 download 说:
2021年5月06日 19:38

Wow, What an Outstanding post. I found this too much informatics. It is what I was seeking for. I would like to recommend you that please keep sharing such type of info.If possible, Thanks. pussy888 download

Kian Warner 说:
2021年5月07日 00:49

hi was just seeing if you minded a comment. i like your website and the thme you picked is super. I will be back. movavi activation code

Kian Warner 说:
2021年5月08日 14:43

Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place.. Mobile phone price in Bangladesh

Kian Warner 说:
2021年5月08日 19:30

I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work. Samsung Phone Price In Bangladesh

shah 说:
2021年5月09日 02:05

Cool stuff you have got and you keep update all of us. 파워볼전용사이트

shah 说:
2021年5月09日 17:25

Thanks for your insight for your fantastic posting. I’m glad I have taken the time to see this. ข่าวบันเทิงวันนี้

schlüsseldienst hitz 说:
2021年5月09日 19:18

Hello, I have browsed most of your posts. This post is probably where I got the most useful information for my research. Thanks for posting, maybe we can see more on this. Are you aware of any other websites on this subject. schlüsseldienst hitzacker

shah 说:
2021年5月10日 14:21

This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here keep up the good work best iso merchant program

zenmate premium code 说:
2021年5月10日 18:29

Friend, this web site might be fabolous, i just like it. zenmate premium code free

https://ohozaa.com/ 说:
2021年5月11日 15:59

The key reason why you will find a lot more totally free football bets when compared with almost every other type of bet is really due to the fact there is much more suits in order to bet on when compared with within the extra workout. High quality functions have thirty 7 game titles in order to perform each time period of time inside the program, together with Football Program functions perform fouthy-six game titles. In addition, there is cup competitions together with worldwide suits. This means adequate opportunity to utilize a number of bookmaker inspiration offers to create a income on football. https://ohozaa.com/

slot1234 说:
2021年5月12日 00:28

Very interesting blog. Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definately interested in this one. Just thought that I would post and let you know. slot1234

zaib 说:
2021年5月12日 16:12

I found your this post while searching for information about blog-related research ... It's a good post .. keep posting and updating information. curso de coaching

pregnancy symptoms w 说:
2021年5月19日 21:11

I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. Thanks... pregnancy symptoms week 4

moldavite dangers 说:
2021年5月21日 03:23

Moldavite Meaning, Uses, Dangers, Moldavites is of extraterrestrial orgin and contains intense energy Learn how to use it correctly, moldavite dangers, potential dangers, how to spot a fake, and where to buy.

dmt vape carts 说:
2021年5月22日 13:59

This article was written by a real thinking writer. I agree many of the with the solid points made by the writer. I’ll be back. dmt vape carts

live22 agent 说:
2021年5月22日 16:10

I really loved reading your blog. It was very well authored and easy to undertand. Unlike additional blogs I have read which are really not tht good. I also found your posts very interesting. In fact after reading, I had to go show it to my friend and he ejoyed it as well! live22 agent

nothing 说:
2021年5月22日 16:31

I really loved reading your blog. It was very well authored and easy to understand. Unlike other blogs I have read which are really not that good.Thanks alot! شراء متابعين تويتر

cherry lime haze 说:
2021年5月31日 20:06

Therefore dissertation web-sites as a result of online to set-up safe and sound ostensibly taped in the website. cherry lime haze

Buy facebooknegative 说:
2021年6月01日 01:14

Yes, I am entirely agreed with this article, and I just want say that this article is very helpful and enlightening. I also have some precious piece of concerned info !!!!!!Thanks. Buy facebooknegative review

Buy Edibles Online 说:
2021年6月01日 21:16

This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. Buy Edibles Online

order thc edibles on 说:
2021年6月05日 16:12

i am for the first time here. I found this board and I in finding It truly helpful & it helped me out a lot. I hope to present something back and help others such as you helped me. order thc edibles online

cbd for sale 说:
2021年6月06日 15:13

It is a fantastic post – immense clear and easy to understand. I am also holding out for the sharks too that made me laugh. cbd for sale

virus 说:
2021年6月09日 15:05

Awesome article, it was exceptionally helpful! I simply began in this and I'm becoming more acquainted with it better! Cheers, keep doing awesome! free novels online

get more info 说:
2021年6月09日 16:28 Awesome article! I want people to know just how good this information is in your article. It’s interesting, compelling content. Your views are much like my own concerning this subject. get more info
WatchCartoonOnline 说:
2021年6月10日 14:56

Wow, What an Outstanding post. I found this too much informatics. It is what I was seeking for. I would like to recommend you that please keep sharing such type of info.If possible, Thanks. WatchCartoonOnline

ASD 说:
2021年6月10日 18:29

Ace Clothing Alterations Gold Coast Robina. Service Gold Coast Wide and Australia Wide though our Online Shop. We are here to help you with your clothing alterations and repairs. Common jobs include, Pant hem, Suit Alterations, Dress Alterations, Wedding Dress Alterations, Zipper Repairs and much more. <a href="https://ace-alterations.com/">Ace Clothing Alterations</a>

asdzxc 说:
2021年6月12日 12:06

We proudly serve the towns and cities located in the GTA west areas mainly Mississauga, Brampton, Milton, Oakville & Etobicoke, etc. Our fleet of tow trucks is readily available to accommodate any quick pickups for scrap cars in the above areas. junk cars for cash

seoms 说:
2021年6月14日 20:57

You make so many great points here that I read your article a couple of times. Your views are in accordance with my own for the most part. This is great content for your readers. <a href="https://www.seoms12.com/">seoms</a>

aol mail login 说:
2021年6月17日 02:50

i never know the use of adobe shadow until i saw this post. thank you for this! this is very helpful. aol mail login


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter