Post date, explained

By #1313 Last update Jun 11, 2009 — Installed 541 times.

There are 6 previous versions of this script.

// ==UserScript==
// @name           Post date, explained
// @namespace      http://userscripts.org/users/26596
// @description    Date of post in textual interpretation
// @include        http://leprosorium.ru/
// @include        http://*.leprosorium.ru/
// @include        http://leprosorium.ru/pages/*
// @include        http://*.leprosorium.ru/pages/*
// @include        http://leprosorium.ru/my/
// @include        http://*.leprosorium.ru/my/
// @include        http://leprosorium.ru/users/*/posts/
// @include        http://*.leprosorium.ru/users/*/posts/
// @author         http://leprosorium.ru/users/n1313
// ==/UserScript==

var short_mode = 0;
// short mode on/off

var now = new Date();
// current time

var all_divs = document.getElementsByTagName('div');

for (var i = 0; i < all_divs.length; i++) {

  var div = all_divs[i];

  if (div.className == 'p') {
  // div with date of post

    var target = div.childNodes[2];
    // target node

    if (target.nodeValue.replace(/\s+/g,"") == 'в')
    // sub-lepra post in "my things"
      target = div.childNodes[4];
      // change target

    var post_date_orig = target.nodeValue.substr(1).replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,"").replace(/\s+/g," ");
    // trimmed original date of post

    if (!post_date_orig.match('сегодня') && !post_date_orig.match('вчера')) {
    // if changes should be done

      var post_date = post_date_orig.substr(0, post_date_orig.length - 8);
      var post_day = post_date.substr(0,2);
      var post_month = post_date.substr(3,2);
      var post_year = post_date.substr(6,4);
      var parsable_post_date = post_month+'/'+post_day+'/'+post_year;
      // date parsed to timestamp

      var delta = now.getTime()-Date.parse(parsable_post_date);
      var days = Math.round(delta/(1000*60*60*24));
      // how many days ago this was posted

      var text = '';
      if (days > 2)
        text = 'позавчера';
      if (days > 3)
        text = 'несколько дней назад';
      if (days > 6)
        text = 'почти неделю назад';
      if (days == 7)
        text = 'неделю назад';
      if (days > 7)
        text = 'больше недели назад';
      if (days == 14)
        text = 'две недели назад';
      if (days > 14)
        text = 'полмесяца назад';
      if (days > 30)
        text = 'месяц назад';
      if (days > 33)
        text = 'больше месяца назад';
      if (days > 50)
        text = 'почти два месяца назад';
      if (days > 60)
        text = 'два месяца назад';
      if (days > 65)
        text = 'больше двух месяцев назад';
      if (days > 80)
        text = 'три месяца назад';
      if (days > 100)
        text = 'больше трёх месяцев назад';
      if (days > 125)
        text = 'четыре месяца назад';
      if (days > 150)
        text = 'почти полгода назад';
      if (days > 170)
        text = 'полгода назад';
      if (days > 200)
        text = 'больше полугода назад';
      if (days > 300)
        text = 'девять месяцев назад';
      if (days > 320)
        text = 'почти год назад';
      if (days > 360)
        text = 'год назад';
      if (days > 380)
        text = 'больше года назад';
      if (days > 450)
        text = 'полтора года назад';
      if (days > 600)
        text = 'почти два года назад';
      if (days > 700)
        text = 'два года назад';
      if (days > 760)
        text = 'больше двух лет назад';
      if (days > 1000)
        text = 'очень давно';

      if (short_mode)
        target.textContent= ' '+text+' ';
      else
        target.textContent= ' '+text+', '+post_date_orig+' ';

    }

  }

}